采取了分包发送似乎也不行,监听返回的特征值一直显示错误指令
/**
* 向低功耗蓝牙设备特征值中写入二进制数据
* @param deviceId 蓝牙设备UUID
* @param serviceId 蓝牙特征值对应服务的 uuid
* @param characteristicId 蓝牙特征值的 uuid
* @param msg 写入的内容
*/
function write(deviceId, serviceId, characteristicId, msg) {
console.log({ deviceId, serviceId, characteristicId, msg })
return new Promise((resolve, reject) => {
const buffer = hexStringToArrayBuffer(msg);
console.log(buffer)
let bytes = buffer.byteLength;
let pos = 0;
while (bytes > 0) {
let tmpBuffer;
if (bytes > 20) {
tmpBuffer = buffer.slice(pos, pos + 20);
pos += 20;
bytes -= 20;
uni.writeBLECharacteristicValue({
deviceId,
serviceId,
characteristicId,
value: tmpBuffer,
success(res) {
console.log('分包发送成功', res)
},
fail(err) {
console.log('分包发送失败', err)
}
})
} else {
tmpBuffer = buffer.slice(pos, pos + bytes);
pos += bytes;
bytes -= bytes;
uni.writeBLECharacteristicValue({
deviceId,
serviceId,
characteristicId,
value: tmpBuffer,
success(res) {
console.log('write指令发送成功', res)
uni.onBLECharacteristicValueChange((res) => {
console.log(res)
let resHex = ab2hex(res.value)
console.log(resHex)
console.log(hexCharCodeToStr(resHex))
})
resolve(res);
},
fail(err) {
console.log('write指令发送失败', err)
reject(err);
}
})
}
}
})
}
1 个回复
dlsm
在获取服务的成功回调里调用设置mtu好像就可以了,但是分包发送的错误不懂怎么解决,有人说是错误了就继续回调函数