.bin拆包后共拆192个数据包,蓝牙发送数据包后wx.notifyBLECharacteristicValueChange() 监听接收数据,
1)当wx.writeBLECharacteristicValue() 发送成功后,
2)wx.notifyBLECharacteristicValueChange() 监听己收到数据后,继续调用(1)发送,直到192数据包发送完成。
3)为什么当wx.notifyBLECharacteristicValueChange() 监听到第20个接收数据后就没有反应,详见附件图:
已经上传android日志。
你好,麻烦提供出现问题的具体机型、微信版本号、系统版本号,以及能复现问题的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)
麻烦在手机微信那里上传下日志: 我->设置->帮助与反馈右上角有个上报日志的入口,麻烦提供一下微信号,时间点(具体到分钟)。
a_1.21.21 微信7.0.22。 同样问题在 iphone7plus ios13.4.1 微信8.0.2 也出现,只上传了android 日志。
代码片段如下:
function notifyBLECharacteristicValueChange() {
var that = this;
......
wx.notifyBLECharacteristicValueChange({
state: true,
deviceId: getApp().globalData.currentDeviceId,
serviceId: getApp().globalData.uuid_services,
characteristicId: getApp().globalData.uuid_notify,
complete: function (res) {
//用来监听手机蓝牙设备的数据变化
wx.onBLECharacteristicValueChange(function(res) {
let buffer = res.value;
let writeBuffer = new DataView(buffer);
let dataResult = [];
...........
var valuess = new Object();
valuess['code'] ='0000';
valuess['com'] = ........;
valuess['msg'] = dataResult;
wxNotif.postNotificationName('recvWxBleOtaNotification', valuess);
})
},
fail: function (res) {
console.log('接收数据失败');
that.showNotificationMsg('9999', 'cmdNOne', '启用低功耗蓝牙设备监听失败', 'recvWxBleNotification');
}
})
}
.......................
recvWxOtaNotificationFn:function (info){
var that = this;
let code = info['code'];
if (code == '0000') {
// OTA升级发送命令成功, 接收返回
if (getApp().globalData.uploadCount == getApp().globalData.packageLength) {
console.log('OTA固件升级包传送完毕');
that.showView = false;
wx.hideLoading();
} else {
getApp().globalData.uploadCount += 1;
getApp().globalData.resentFlag = 0;
//拆分包
goPureBtCommand.splitPackage(getApp().globalData.uploadCount);
}
}
},
.....................................
function splitPackage(uploadCount) {
let that = this;
wx.writeBLECharacteristicValue({
deviceId: getApp().globalData.currentDeviceId,
serviceId: getApp().globalData.uuid_services,
characteristicId: getApp().globalData.uuid_send,
value: sendDataBuffer,
success: function (res) {
console.log('message发送成功');
............
},
fail: function (res) {
console.log('发送数据失败');
............
},
});
注意⚠️ :如下写不写没有任何作用!!!!!!!!!!!!
// /**
// * 坑就在这里了,对于安卓系统,需要添加下面这段代码。你写完数据后,
// * 还必须读一次,才能被onBLECharacteristicValueChange监听到,
// * 才会把数据返回给你,但在苹果系统里面就不能有下面这段代码了,因
// * 为如果你添加上去的话,会出现发一次指令出现两次返回值的情况
// */
// wx.readBLECharacteristicValue({
// deviceId: getApp().globalData.currentDeviceId,
// serviceId: getApp().globalData.uuid_services,
// characteristicId: getApp().globalData.uuid_notify,
// success: function (res) {
// console.log('readBLECharacteristicValue.......');
// }
// });
}
(2)但是我们设备需要同步校验notifyBLECharacteristicValueChange返回值,后再调用在 writeBLECharacteristicValue函数发送数据包。
(3)ios 和 android 原声开发通过ble蓝牙发送数据包和监听数据包都可以正常使用。实现ota升级。
(4)目前在考虑通 微信小应用实现 蓝牙指令已经通过了没有问题,ota升级目前遇到问题、如题。