HarmonyOS鸿蒙Next中蓝牙BLE的服务端监听characteristicWrite方法只执行一次
HarmonyOS鸿蒙Next中蓝牙BLE的服务端监听characteristicWrite方法只执行一次
问题方法:
// 注册写入监听
this.gattServer.on('characteristicWrite', (characteristicRequest: ble.CharacteristicWriteRequest) => {
if(characteristicRequest.characteristicUuid === this.characteristicUuid){
console.log(`===write_Wifi`,characteristicRequest.deviceId);
this.handleWriteEvent(characteristicRequest.deviceId,characteristicRequest);
} else {
console.log(`===write_IP`,characteristicRequest.deviceId);
this.handleWriteEvent(characteristicRequest.deviceId,characteristicRequest)
}
});
为什么该方法只执行一次,执行了多次写入方法 该监听方法只执行了一次
更多关于HarmonyOS鸿蒙Next中蓝牙BLE的服务端监听characteristicWrite方法只执行一次的实战教程也可以访问 https://www.itying.com/category-93-b0.html
2 回复
在HarmonyOS中,BLE服务端的characteristicWrite
回调只触发一次可能原因:
- 未正确设置特征值的WRITE属性;
- 客户端未发送新的写入请求;
- 服务端特征值未通过
setValue
更新数据状态。
需确保:
- 特征值配置包含WRITE权限,
- 使用
gattService.addCharacteristic()
添加特征时PROPERTY_WRITE
和PERMISSION_WRITE
标志位正确设置。 - 每次写入后服务端应保持可写状态。
更多关于HarmonyOS鸿蒙Next中蓝牙BLE的服务端监听characteristicWrite方法只执行一次的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
从代码来看,characteristicWrite
监听只执行一次的问题可能由以下几个原因导致:
-
未正确设置
characteristic
的properties
属性。代码中properties
被注释掉了,需要确保write
属性已启用:properties: {write: true} // 确保包含 write 属性
-
客户端可能使用了
writeNoResponse
方式写入,但服务端未配置相应属性。建议同时启用:properties: {write: true, writeNoResponse: true}
-
每次写入后需要调用
sendResponse
确认写入完成:this.gattServer.sendResponse(characteristicRequest.deviceId, characteristicRequest.transId, ble.GattStatus.SUCCESS);
-
检查客户端是否每次都建立了新的连接,可能导致服务端监听失效。
建议先检查 properties
配置并添加 sendResponse
调用,这是 BLE 服务端处理多次写入的关键步骤。