HarmonyOS ble蓝牙接收数据如何实现同步操作

ble 蓝牙模块通过writeCharacteristicValue 写数据,通过on(‘BLECharacteristicChange’, (characteristicChangeReq: ble.BLECharacteristic) => {}接收返回的数据,我如何实现数据同步,一收对应一发,接收完成然后发下一条重复如此。现在模式是通过回调,接收,但是有时回调太多,造成程序混乱,比如这数据没接收完,就进行下个数据的发送,堵死ble通道

HarmonyOS
2024-12-28 08:35:53
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa

前暂未提供同步方法能力获取参数能力,可考虑通过async…await修饰将异步回调转换成同步形式在监听事件中获取:

if (this.gattClient) {
  this.gattClient.on('BLEConnectionStateChange', async (state: ble.BLEConnectionChangeState) => {
    console.log('BluetoothPage bluetooth connect state changed');
    let deviceId = state.deviceId;
    console.log('BluetoothPage deviceId connectState ' + deviceId);
    this.connectState = state.state;
    if (this.connectState == constant.ProfileConnectionState.STATE_DISCONNECTED) {
      this.connectStateSwitch = false;
    }

    if (this.connectState == constant.ProfileConnectionState.STATE_CONNECTED) {
      this.connectStateSwitch = true;
    }
    console.log('BluetoothPage bluetooth connectState ' + JSON.stringify(this.connectState));
  })
  this.stateListenSwitch = true;
}
分享
微博
QQ
微信
回复
2024-12-28 11:24:49
相关问题
HarmonyOS ble蓝牙问题
409浏览 • 1回复 待解决
BLE蓝牙开发如何实现对智能灯的控制?
7079浏览 • 1回复 待解决
HarmonyOS 蓝牙ble相关demo
327浏览 • 1回复 待解决
HarmonyOS 蓝牙BLE开发 Dome
821浏览 • 1回复 待解决
HarmonyOS BLE蓝牙发送数据量大的问题
508浏览 • 1回复 待解决
HarmonyOS Ble蓝牙demo
455浏览 • 1回复 待解决
HarmonyOS 如何开发低功耗蓝牙ble
272浏览 • 1回复 待解决
HarmonyOS 蓝牙ble写入失败 2900099
347浏览 • 1回复 待解决
HarmonyOS 蓝牙ble模块getServices失败
324浏览 • 1回复 待解决
数据操作同步的方法
2150浏览 • 1回复 待解决
HarmonyOS 蓝牙BLE使用是否需要定位
313浏览 • 1回复 待解决
如何实现鸿蒙应用的离线数据同步?
398浏览 • 0回复 待解决
数组列表如何实现数据的双向同步
783浏览 • 1回复 待解决
HarmonyOS BLE数据问题
867浏览 • 1回复 待解决
HarmonyOS BLE无法正常收发数据
403浏览 • 1回复 待解决
HarmonyOS有没有蓝牙相关的操作demo
654浏览 • 1回复 待解决