一、data 里面定义一个参数:
isNotifyBLECharacteristic:false, //是否启用蓝牙设备服务特征 notify
二、启用蓝牙设备特征值变化时的notify功能
//启用低功耗蓝牙设备特征值变化时的 notify 功能
notifyBLECharacteristicValueChange()
{
var that=this;
let deviceId = that.data.deviceId;
let serviceUUID = that.data.serviceUUID;
let notifyCharacteristicUUID = that.data.notifyCharacteristicUUID;
Bluetooth.notifyBLECharacteristicValueChange(deviceId, serviceUUID, notifyCharacteristicUUID).then(
function(res){
that.seeConsole('启用蓝牙设备特征值变化时的 notify 功能成功');
if(res)
{
that.setData({isNotifyBLECharacteristic:true});
//监听蓝牙低功耗设备的特征值变化事件
Bluetooth.onBLECharacteristicValueChange();
}
},
function(err){
that.seeConsole('启用蓝牙设备特征值变化时的 notify 功能失败');
that.setData({isNotifyBLECharacteristic:false});
}
);
},
//启用低功耗蓝牙设备特征值变化时的 notify 功能 按钮事件
notifyBLECharacteristicValueChangeClick()
{
var that=this;
let notifyCharacteristicUUID = that.data.notifyCharacteristicUUID;
if(notifyCharacteristicUUID!='')
{
Bluetooth.CheckSystemInfo().then(res=>{
if(res)
{
that.notifyBLECharacteristicValueChange();
}
});
}
else
{
wx.showModal({
content: '请先选择连接设备选择的服务的特征值(notify)UUID',
showCancel:false,
success: function (res) {
if (res.confirm) {
console.log('用户点击确定');
}
}
});
}
},
三、页面代码
<view class="event-bar" list-item>
<view class="view_left">
<button class="btn_bg" bindtap="notifyBLECharacteristicValueChangeClick">启用设备特征值变化时的notify</button>
</view>
</view>
注意:这里的serviceUUID、notifyCharacteristicUUID 我会在后面写入数据时做重点说明。
下一篇 将继续介绍 微信小程序蓝牙设备写入数据