详细问题描述
使用HBuildX构建uni-app,使用ble相关的接口,在微信小程序中可以顺利完成特征值的订阅和监听到特征值的变化,但是ios真机调试时,订阅特征值变化成功后会收到设备的一条数据(微信小程序不会),但是后续再也收不到特征值变化的通知。
[内容]
代码片段
/**
* 订阅操作成功后需要设备主动更新特征值的 value,才会触发 uni.onBLECharacteristicValueChange 回调。
*/
notifyBLECharacteristicValueChange() {
console.log('订阅消息')
uni.notifyBLECharacteristicValueChange({
state: true, // 启用 notify 功能
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId: this.deviceId,
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
serviceId: this.serviceId,
// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
characteristicId: this.notifyCharacteristicId,
success(res) {
console.log('notifyBLECharacteristicValueChange success:' + JSON.stringify(res));
console.log('onBLECharacteristicValueChange...')
uni.onBLECharacteristicValueChange(function (characteristic) {
console.log('监听低功耗蓝牙设备的特征值变化事件成功');
console.log(JSON.stringify(characteristic));
var ret = "";
let dataView = new DataView(characteristic.value);
for (var i = 0; i < characteristic.value.byteLength; i++) {
var value = dataView.getUint8(i).toString(16).toUpperCase();
if (value.length < 2) {
value = "0" + value;
}
ret = ret + value + " ";
}
var msg = ret.substr(0, ret.length - 1)
console.log("设备返回完整数据: " + msg);
});
}
});
}
[步骤]
[结果]
IOS真机调试日志:(功能异常)
...
20:18:56.136 [LOG] : 订阅消息 at pages\test\index.vue:251
20:18:56.155 [LOG] : notifyBLECharacteristicValueChange success:{"errMsg":"notifyBLECharacteristicValueChange:ok"} at pages\test\index.vue:261
20:18:56.178 [LOG] : onBLECharacteristicValueChange... at pages\test\index.vue:262
20:18:56.197 [LOG] : 监听低功耗蓝牙设备的特征值变化事件成功 at pages\test\index.vue:264
20:18:56.217 [LOG] : {"deviceId":"A93B13A8-551C-12F9-EF79-12ECCFF926CB","serviceId":"00001900-0000-1000-8000-00805F9B34FB","characteristicId":"00001901-0000-1000-8000-00805F9B34FB","value":{}} at pages\test\index.vue:265
20:18:56.239 [LOG] : 设备返回完整数据: 03 00 03 00 03 00 03 01 00 00 00 00 00 00 00 00 02 00 04 03 05 03 05 01 03 00 02 00 03 02 03 00 02 00 03 00 00 00 00 00 00 00 00 00 04 00 04 00 00 00 00 00 00 00 at pages\test\index.vue:276
20:19:52.222 [LOG] : 要发送的消息: 30 30 30 31 at pages\test\index.vue:162
20:19:52.243 [LOG] : writeBLECharacteristicValue success{"errMsg":"writeBLECharacteristicValue:ok"} at pages\test\index.vue:64
=======================接收不到数据==============================
微信小程序日志(功能正常)
...
index.vue:251 订阅消息
index.vue:261 notifyBLECharacteristicValueChange success:{"errCode":"0","errMsg":"notifyBLECharacteristicValueChange:ok"}
index.vue:262 onBLECharacteristicValueChange...
index.vue:162 要发送的消息: 30 30 30 31
index.vue:64 writeBLECharacteristicValue success{"errCode":0,"errMsg":"writeBLECharacteristicValue:ok"}
index.vue:264 监听低功耗蓝牙设备的特征值变化事件成功
index.vue:265 {"deviceId":"A93B13A8-551C-12F9-EF79-12ECCFF926CB","serviceId":"00001900-0000-1000-8000-00805F9B34FB","characteristicId":"00001901-0000-1000-8000-00805F9B34FB","value":{}}
index.vue:276 设备返回完整数据: 30 30 30 31 0D 0A 0D 0A 2B 43 53 51 3A 20 32 30 2C 30 0D 0A 0D 0A 4F 4B 0D 0A 00
[期望]
[如果语言难以表述清晰,拍一个视频或截图,有图有真相]
IDE运行环境说明
[HBuilderX]
[IDE版本号]
1.9.3.20190422
[windows版本号]
10
[mac版本号]
uni-app运行环境说明
[运行端是h5或app或某个小程序?]
[运行端版本号]
[项目是cli创建的还是HBuilderX创建的?如果是cli创建的,请更新到最新版cli再试]
[编译模式是老模板模式还是新的自定义组件模式?]
App运行环境说明
[Android版本号]
[iOS版本号]
11.4
[手机型号]
ip 6s plus
[模拟器型号]
附件
[IDE问题请提供HBuilderX运行日志。菜单帮助-查看运行日志,点右键打开文件所在目录,将log文件压缩成zip包上传]
马小力
楼主解决了这个问题吗
2019-06-14 09:03