请大佬们帮我看看,就是发送给蓝牙数据后无法接受到回调值,之前是用app对接蓝牙设备的(app上设备正常回调),现在要应用到微信小程序上,数据发送了,但一直没有接收到回调的值,需要回调的值进行解密。以下是网下的一个dome,做了一些调整,弄了一个星期了求大佬大神指点
//发送
lanya8: function () {
var that = this;
// 这里的回调可以获取到 write 导致的特征值改变
var buf = new ArrayBuffer(16)
var dataView = new DataView(buf)
wx.request({
url: globalData.serveUrl + '*******.do',//后台加密返回请求
success: function (data) {
var str = data.data.infoData;
var arr = str.split(",");
console.log(arr);
for (var i = 0; i < arr.length; i++) {
dataView.setInt8(i, arr[i]);
}
console.log('str', buf);//例如["12", "88", "68", "118", "52", "16", "1", "0", "0", "-13", "-65", "-28", "114"]
wx.writeBLECharacteristicValue({
deviceId: that.data.connectedDeviceId,
// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
serviceId: that.data.notifyServicweId,
// 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取
characteristicId: that.data.notifyCharacteristicsId,
// 这里的value是ArrayBuffer类型
value: buf,
success: function (res) {
console.log('writeBLECharacteristicValue success', res.errMsg) //这里发送成功,公司的蓝牙设备显示了绿灯代表能接收到
}
})
},
fail: function (res) {
console.log(that.data.notifyServicweId);
console.log(that.data.notifyCharacteristicsId);
console.log('writeBLECharacteristicValue Error', res.errMsg)
}
})
},
//启用低功耗蓝牙设备特征值变化时的 notify 功能
lanya9: function () {
var that = this;
wx.notifyBLECharacteristicValueChange({
type: "indication",
state: true, // 启用 notify 功能
// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
deviceId: that.data.connectedDeviceId,
// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
serviceId: that.data.notifyServicweId,
// 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取
characteristicId: that.data.notifyCharacteristicsId,
success: function (res) {
console.log('notifyBLECharacteristicValueChange success', res.errMsg);//这里开启成功 返回了ok
that.lanya10();//执行监听
},
fail: function (res) {
console.log(that.data.notifyServicweId);
console.log(that.data.notifyCharacteristicsId);
console.log('notifyBLECharacteristicValueChangeError', res.errMsg)
},
})
},
//接收消息
lanya10: function () {
var that = this;
// 必须在这里的回调才能获取
wx.onBLECharacteristicValueChange(function (characteristic) {
console.log("66666666666");
let hex = Array.prototype.map.call(new Uint8Array(characteristic.value), x => ('00' + x.toString(16)).slice(-2)).join('');
console.log("hexhexhexhexhexhex:" + hex)
wx.request({
url: globalData.serveUrl + '***.do',
data: { hexString: hex },
method: "POST",
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success: function (data) {
var res = data.data.infoData;
console.log(res)
that.setData({
jieshou: res
})
},
fail: function (res) {
console.log('onBLECharacteristicValueChangeError', res.errMsg)
}
})
})
}
本文网址:http://www.91bianli.com/kaifazhinan/76133.html