蓝牙控制器连接成功,第一次通讯是正常的,但是在连接状态下再次跟蓝牙通讯,就会出现,写数据成功,但是接收不到设备返回给我的数据
(不知道是不是跟写的顺序有关,但是他是有时候可以接受到,有时候接收不到,有没那个哥们遇到过同样的问题呢)
getTargetCharacteric: function (serviceid) {
var that = this
wx.getBLEDeviceCharacteristics({
deviceId: that.data.connectedDeviceId,
serviceId: serviceid,
success: function (res) {
console.log('设置写监听:', res.characteristics[0].uuid)
console.log("判断是否支持notify和write操作");
console.log(res.characteristics[0].properties);
that.setData({
serviceId: serviceid,
writecharacId: res.characteristics[0].uuid
})
// //写入开始命令
var code = that.getstate_params();
wx.notifyBLECharacteristicValueChange({
state: true, // 启用 notify 功能
deviceId: that.data.connectedDeviceId,
serviceId: serviceid,
characteristicId: res.characteristics[0].uuid,
success: function (res) {
console.log('监听蓝牙notify服务成功 characteristicId')
that.data.mJianHuStatus = 2;
setTimeout(function () {
that.writeBleData(code);
that.recieveequ();
}, 1000)
},
fail: function (res) {
console.log('监听notify服务失败')
that.data.mJianHuStatus = 0;
}
})
}
})
},
/**
* 监听蓝牙获取的数据
* */
recieveequ: function () {
var that = this
wx.onBLECharacteristicValueChange(function (characteristic) {
that.data.mJianHuStatus = 0;
let buffer = characteristic.value
console.log("接收解密前的数据为:hex==" + Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join(''))
var tmpStrr = Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
that.dataHandle(tmpStrr);
})
},