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

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

阅读 656
1 个回答

前暂未提供同步方法能力获取参数能力,可考虑通过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;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进