部分安卓 writeBLECharacteristicValue发生1008错误
发布于 6 年前 作者 fangtang 7988 次浏览 来自 问答

部分安卓机型执行 writeBLECharacteristicValue 发生 1008 错误,如小米5,尝试过忽略这个错误,但蓝牙没有接受到信息

下面是写入的代码,做了3秒的等待,但 onBLECharacteristicValueChange 没有接受到返回。只有部分安卓机型会这样

  wx.writeBLECharacteristicValue({

    // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取 

    deviceId: app.bleOptions.targetDevice.deviceId,

    // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取 

    serviceId: serUuid,

    // 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取 

    characteristicId: cmdUuid,

    // 这里的value是ArrayBuffer类型 

    value: buffer,

    success: function (res) {

      console.log(‘写入特征信息成功’, res.errMsg);

    },

    fail: function (res) {

      console.log(‘写入特征信息失败’, JSON.stringify(res));

      if (app.bleOptions.scanLogCallBack) {

        app.bleOptions.scanLogCallBack(null, JSON.stringify(res));

      }

    },

    complete: function () {

      //等待响应

      sendCommandCallBackHandle = setTimeout(function () {

        if (failHandle) {

          failHandle();

        }

      }, 3000);

    }

  })

5 回复

你可以试试在notify成功后延迟几百毫秒调用write,目前发现部分安卓系统在notify调用后立刻调用write会发生系统错误。

现在是再连接蓝牙成功后,注册监听事件后,进行握手连接,代码如下:

//注册监听事件

          wx.notifyBLECharacteristicValueChange({

            state: true, // 启用 notify 功能 

            // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取 

            deviceId: app.bleOptions.targetDevice.deviceId,

            // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取 

            serviceId: serUuid,

            // 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取 

            characteristicId: cmdUuid,

            success: function (res) {

              console.log(‘notifyBLECharacteristicValueChange success’, res.errMsg);

              handShake();//《------------------在这里

            },

@王翊夫if

如果是notify就报错10008,该如何解决呢

好的,我试试

你好,请问在什么时机下调用 wx.writeBLECharacteristicValue

回到顶部