BUG: APP端BLE写入失败 (code: 10007)
问题描述
在APP端(Android)连接BLE设备后,调用 uni.writeBLECharacteristicValue 写入数据时报错:
writeBLECharacteristicValue:fail property not support (code: 10007)小程序端正常,只有APP端出现此问题。
错误日志
21:16:10.314 找到命令特征值:, 6E400002-B5A3-F393-E0A9-E50E24DCCA9E, 属性:, {"write":true}
21:16:10.323 写入失败:, {"errMsg":"writeBLECharacteristicValue:fail property not support","code":10007}问题根因
- BLE写入时序问题: uni-app在APP端调用
notifyBLECharacteristicValueChange启用通知后,需要等待一段时间才能进行写入操作 - Android BLE协议栈特性: Android系统对BLE操作有严格的时序要求,通知启用和写入操作之间需要有延时
解决方案
在 subscribeNotify(启用通知)成功后,添加延时等待再执行写入操作:
typescript
// 订阅通知
await subscribeNotify(deviceId, (hexValue) => {
console.log('收到通知:', hexValue);
});
// 等待通知启用稳定(APP端需要)
await new Promise(resolve => setTimeout(resolve, 500));
// 请求初始状态
await writeCommand(deviceId, packRequestStatus());相关代码
修改文件
software/uniapp/src/pages/scan/scan.vue
修改点
在连接设备的流程中,订阅通知成功后添加500ms延时:
typescript
// 连接设备
async function connectDevice(device: BLEDevice) {
// ... 连接流程 ...
// 订阅通知
await subscribeNotify(device.deviceId, (hexValue) => {
console.log('收到通知:', hexValue);
});
// 【关键】等待通知启用稳定(APP端需要)
await new Promise(resolve => setTimeout(resolve, 500));
// 请求初始状态
await writeCommand(device.deviceId, packRequestStatus());
// ...
}参考
- uni-app官方文档: writeBLECharacteristicValue
- 注意点: "安卓平台上,在调用 notifyBLECharacteristicValueChange 成功后立即调用 writeBLECharacteristicValue 接口,在部分机型上会发生 10008 系统错误"
修复时间
2025-03-31
