蓝牙读数据
发布于 5 年前 作者 lei08 5104 次浏览 来自 问答

  readBLECharacteristicValue: function (deviceId, serviceId, characteristicId, callback) {

    wx.onBLECharacteristicValueChange(function (characteristic) {

      console.log(‘characteristic value comed:’);

      let buffer = characteristic.value;

      let dataView = new DataView(buffer);

      console.log(“接收字节长度:” + dataView.byteLength);

      console.log(dataView.getUint8(0));

      console.log(dataView.getUint8(1));

      callback(dataView);

    });

    wx.readBLECharacteristicValue({

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

      deviceId: deviceId,

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

      serviceId: serviceId,

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

      characteristicId: characteristicId,

      success: function (res) {

        console.log(‘readBLECharacteristicValue:’, res)

      }

    })

  }

  1. 设备一直在广播数据   ,这里的readBLECharacteristicValue 要轮询么  譬如1s钟调用一次 ,还是不需要轮询,调用一次后,只要有数据就会进wx.onBLECharacteristicValueChange

  2. wx.onBLECharacteristicValueChange 这个函数的回调函数参数characteristic.value 该如何显示

9 回复

不是很能明白你的意思。 按你的要求与特征值支持的属性,传入对应的特征值id调用接口即可。

console.log()打印信息.

app.json里debug: ture;

在手机微信里扫码预览小程序.

手机右上角有三个小点点,点击,选择打开调试.重新预览小程序.就可以在vConsole按钮上查看控制台信息

notifyBLECharacteristicValueChanged 这个也能进入success 回调

说明已经setNotify成功了,那就是设备没有发送数据过来,是不是需要怎么触发呢?

  //获取蓝牙设备所有 characteristic(特征值)

  getBLEDeviceCharacteristics: function (deviceId, serviceId, callback) {

    wx.getBLEDeviceCharacteristics({

      deviceId: deviceId,

      serviceId: serviceId,

      success: function (res) {

        // success

        console.log(res.characteristics);

        wx.notifyBLECharacteristicValueChanged({

          state: true, // 启用 notify 功能

          deviceId: deviceId,

          serviceId: serviceId,

          characteristicId: res.characteristics[1].uuid,

          success: function (res) {

            console.log(‘notifyBLECharacteristicValueChanged success’, res)

          },

          fail: function (res) {

            console.log(‘notifyBLECharacteristicValueChanged error’, res)

          }

        })

        callback(res.characteristics);

      },

      fail: function (res) {

        // fail

      },

      complete: function (res) {

        // complete

      }

    })

  },

characteristics[1] 支持notify  但不支持read

characteristics[2] 支持read 但不支持notify 

所以

notifyBLECharacteristicValueChanged   传的参数 characteristicId  是 characteristics[1]

readBLECharacteristicValue  传的参数 characteristicId  是 characteristics[2]

这两个需要传的 characteristicId   一致么



为什么需要同时满足呢?

notifyBLECharacteristicValueChange  是用于持续监听设备某个特征值发送来的数据的。

readBLECharacteristicValue  是用于一次性读取某个特征值的数据的

按你的应用场景需求来对某个特征值调用相关接口就行了。

明白了 这是两种读数据接口

我的设备是支持notify的

 wx.onBLECharacteristicValueChange(function (characteristic) {

          let buffer = characteristic.value;

          let dataView = new DataView(buffer);

          var temp = “”;

          for (var i = 0; i < dataView.byteLength; i++) {

            temp += “第” + (i + 1) + “个字节:” + dataView.getUint8(i) + “;\r\n”;

          }

          console.log(temp);

          /*

          const base64 = wx.arrayBufferToBase64(characteristic.value)

          console.log(base64);

          */

        });

    wx.notifyBLECharacteristicValueChanged({

          state: true, // 启用 notify 功能

          deviceId: deviceId,

          serviceId: serviceId,

          characteristicId: res.characteristics[1].uuid,

          success: function (res) {

            console.log(‘notifyBLECharacteristicValueChanged success’, res)

          },

          fail: function (res) {

            console.log(‘notifyBLECharacteristicValueChanged error’, res)

          }

        });

notifyBLECharacteristicValueChanged 这个也能进入success 回调

但是 onBLECharacteristicValueChange 这个还是没有值

你好,你是怎么知道小程序连接设备成功了

notifyBLECharacteristicValueChange

readBLECharacteristicValue

这两个api 都需要  characteristicId 这个参数

notifyBLECharacteristicValueChange 来说 characteristicId 需支持notify 

readBLECharacteristicValue 来说 characteristicId 需支持read

但是我获取的  characteristicId list  就没有一个 characteristicId 这两个属性同时满足


你好,按你的需求,应该是让设备特征值支持 notify 或者 indicate 属性,然后在小程序端对特征值调用 notifyBLECharacteristicValueChange, 就会在设备特征值notify数据时回调进 onBLECharacteristicValueChange。

characteristic.value 为arrayBuffer类型,可用相应的接口对其进行解析。 小程序侧有提供arrayBufferToBase64拓展接口。

回到顶部