ios 使用蓝牙功能与蓝牙设备交互,写入数据后蓝牙设备回复数据,收到回复后继续写入,大概在写入5、6次之后,再调用uni.writeBLECharacteristicValue接口,回调会越来越慢,导致数据获取加载很慢,有无解决方案?
示例逻辑:
// 写入命令
writeCommand(value, deviceId) {
let time = new Date().getTime();
console.log('开始调用写入命令api=====', time)
return new Promise((resolve, reject) => {
uni.writeBLECharacteristicValue({
deviceId: deviceId,
serviceId: service_uuid,
characteristicId: characteristic_uuid,
writeType: 'writeNoResponse',
value: value.buffer,
success: function(res) {
resolve(true)
let sec = new Date().getTime();
console.log("===成功写入命令===", value, sec - time)
},
fail: function(res) {
console.log('===写入命令失败===', res)
uni.hideLoading()
reject(false)
}
})
})
},
setTimeout(function() {
writeCommand(value, deviceId.value)
}, 200);
uni.onBLECharacteristicValueChange(function(res) {
// 监听成功
let hex16 = _this.ab2hex(res.value);
// 监听成功后继续调用
setTimeout(function() {
writeCommand(value, deviceId.value)
}, 200);
})
1、调用上述api向低功耗蓝牙写入指令,间隔200ms,监听蓝牙设备回复; 监听成功后继续调用;
2、sec - time:进入调用写入api方法的时间到走success回调之间的时间间隔,发现一开始写入时间间隔正常,后面写入越来越慢,具体见附件