接上一篇(原生微信小程序蓝牙开发(六))
当我们获取到蓝牙特征值后,就能根据当前蓝牙的特征值来进行下一步操作
第七步:启用 notify 功能(wx.notifyBLECharacteristicValueChange)
注意:必须设备的特征支持 notify 或者 indicate 才可以成功调用。
启用蓝牙低功耗设备特征值变化时的 notify 功能,订阅特征。
另外,必须先启用 wx.notifyBLECharacteristicValueChange 才能监听到设备 characteristicValueChange
事件
参数
Object object
属性 | 类型 | 默认值 | 必填 | 说明 | 最低版本 |
---|---|---|---|---|---|
deviceId | string | 是 | 蓝牙设备 id | ||
serviceId | string | 是 | 蓝牙特征对应服务的 UUID | ||
characteristicId | string | 是 | 蓝牙特征的 UUID | ||
state | boolean | 是 | 是否启用 notify | ||
type | string | indication | 否 | 设置特征订阅类型,有效值有 notification 和 indication | 2.4.0 |
success | function | 否 | 接口调用成功的回调函数 | ||
fail | function | 否 | 接口调用失败的回调函数 | ||
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
官方文档:wx.notifyBLECharacteristicValueChange(Object object)
下面上代码:
// 创建连接,发送指令
startNotice(uuid) {
wx.notifyBLECharacteristicValueChange({
state: true, // 启用 notify 功能
deviceId: this.data.deviceId,
serviceId: this.data.services,
characteristicId: uuid,
success: (res) => {
this.setData({
"setInfo.notifyValueChange": "启用成功",
});
this.sendMy("getsid");
},
fail: () => {
this.setData({
"setInfo.notifyValueChange": "启用失败",
});
},
});
},
notify 功能启用成功后进行下一步操作——接收蓝牙返回的信息
欢迎大家补充评论,我们一起学习探索。