0

我有 rPi 设置,节点运行 bleno 来宣传一个特性。使用 evothings 我正在测试 Android 和 iOS,并且在读取特性时我都遇到了错误。

在 evothings 中读取特征的代码是:

device.readServiceCharacteristic(
            'ff51b30e-d7e2-4d93-8842-a7c4a57dfb07',
            'ff51b30e-d7e2-4d93-8842-a7c4a57dfb08',

            function(data)
            {
              console.log('characteristic data: ' + evothings.ble.fromUtf8(data));
            },
            function(errorCode)
            {
              console.log('readCharacteristic error: ' + errorCode);
            });

传入的第一个 uuid 是我可以在控制台日志中看到的服务 ID。已从服务器(rpi)端的代码检查了第二个 uuid。

当我运行它时,控制台会在 iOS 上记录一个不太可能发生的错误。在 Android 上它记录:错误 1

对于服务器上的参考,我遵循了本教程: https ://www.hackster.io/inmyorbit/build-a-mobile-app-that-c​​onnects-to-your-rpi-3-using-ble-7a7c2c

我正在尝试使用它来了解 BLE,但无法通过谷歌搜索解决此类一般错误。

4

1 回答 1

0

在读取特征之前,您必须执行以下步骤:

  1. 扫描设备
  2. 连接到设备
  3. 获取服务
  4. 读取特征

...看来您正在尝试执行 #4 而没有完成前 3 个。这是一个源自Evothings API 指南的示例:

var service = [];
var readData;

// Start scanning for BLE devices
evothings.ble.startScan(
    onDeviceFound,
    onScanError);

// This function is called when a device is detected, here
// we check if we found the device we are looking for.
function onDeviceFound(device)
{
    // Stop scanning.
    evothings.ble.stopScan();

    // Connect.
    evothings.ble.connectToDevice(
        device,
        onConnected,
        onDisconnected,
        onConnectError);
}

// Called when device is connected.
function onConnected(device)
{
    console.log('Connected to device');

    // Get service
    service = evothings.ble.getService(device, <insert service UUID>);

    // Read characteristic data
    evothings.ble.readCharacteristic(
        device,
        <insert characteristic UUID>,
        readSuccess,
        readError);
}

// Called if device disconnects.
function onDisconnected(device)
{
    console.log('Disconnected from device');
}

// Called when a connect error occurs.
function onConnectError(error)
{
    console.log('Connect error: ' + error)
}

// Read success callback
var readSuccess = function (data) {
   readData = evothings.ble.fromUtf8(data);
};

// Read error callback
var readError = function (error) {
    console.log('Read error: ' + error);
}
于 2017-12-01T05:29:46.247 回答