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;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
分享
微博
QQ
微信
回复
2024-12-28 11:24:49
相关问题
HarmonyOS ble蓝牙问题
953浏览 • 1回复 待解决
HarmonyOS Ble蓝牙demo
1022浏览 • 1回复 待解决
HarmonyOS BLE蓝牙发送数据量大的问题
1196浏览 • 1回复 待解决
HarmonyOS 蓝牙BLE开发 Dome
1284浏览 • 1回复 待解决
HarmonyOS 蓝牙ble相关demo
830浏览 • 1回复 待解决
BLE蓝牙开发如何实现对智能灯的控制?
7563浏览 • 1回复 待解决
HarmonyOS 如何开发低功耗蓝牙ble
812浏览 • 1回复 待解决
HarmonyOS esp BLE蓝牙配网?
304浏览 • 1回复 待解决
HarmonyOS 蓝牙ble模块getServices失败
945浏览 • 1回复 待解决
HarmonyOS 蓝牙ble写入失败 2900099
946浏览 • 1回复 待解决
数据操作同步的方法
2738浏览 • 1回复 待解决
HarmonyOS 蓝牙BLE使用是否需要定位
695浏览 • 1回复 待解决
HarmonyOS BLE数据问题
1322浏览 • 1回复 待解决
HarmonyOS BLE无法正常收发数据
822浏览 • 1回复 待解决
数组列表如何实现数据的双向同步
1203浏览 • 1回复 待解决
如何实现鸿蒙应用的离线数据同步?
720浏览 • 0回复 待解决
HarmonyOS 实现蓝牙连接打印机发送数据
1521浏览 • 1回复 待解决