如何从 capacitor-community / bluetooth-le 中的异步写入函数获得响应

How to get a response from the async write function in capacitor-community / bluetooth-le

我正在使用 ESP32 芯片并尝试创建一个 Android 应用程序(使用 Ionic),它允许用户通过 BLE 将 wifi 凭据发送到 ESP32 芯片。我希望能够在 UI 中为用户更新 wifi 发送过程的状态(我正在使用 Angular 进行开发,然后将其转换为 Android webapp使用离子)。为此,我还使用了 capacitor-community/bluetooth-le 库。

任何人都可以向我解释 this.queue 在异步写入函数(代码如下)中的作用吗?我认为此函数 returns 是远程 BLE 设备在写入 GATT 特性后的响应,但我完全没有得到任何响应。

async write(deviceId: string, service: string, characteristic: string, value: DataView): Promise<void> {
    service = validateUUID(service);
    characteristic = validateUUID(characteristic);
    return this.queue(async () => {
      if (!value?.buffer) {
        throw new Error('Invalid data.');
      }
      let writeValue: DataView | string = value;
      if (Capacitor.getPlatform() !== 'web') {
        // on native we can only write strings
        writeValue = dataViewToHexString(value);
      }
      await BluetoothLe.write({
        deviceId,
        service,
        characteristic,
        value: writeValue,
      });
    });
  }

下面是我如何使用上面显示的写入函数:

this.bleConnectService.getBLE().then(resp => {
  this.deviceID = resp});
}

BleClient.write(this.deviceID, '021a9004-0382-4aea-bff4-6b3f1c5adfb4', '021aff54-0382-4aea-bff4-6b3f1c5adfb4', bytesOfStuff).then(resp => {
      const result2 = resp;
}

我知道写入功能可以正常工作,因为我的芯片正在以正确的格式获取我发送的数据。我只是没有从写入函数中得到客户端的响应。此外,我可以确认芯片每次对我发送给它的数据进行处理时都会发送响应。

以下是我如何使用 BleClient.write 将信息传输到 BLE 设备(接收方):

BleClient.write(this.connectedDevice.deviceId, this.baseUUID, this.characteristicUUID, data)
.then(console.log('BleWrite was successful.  Now let/'s do something else')
.catch(error => console.log('BleWrite fail error: ', error))

BleClient.write returns undefined (promise) 解决后。因此,当它解析时(即 BleClient.write 成功),它不会从它写入的接收设备中获取和 return 任何确认数据。你可以说没有错误意味着 BleClient.write 是成功的。如果 BleClient.write 拒绝,它将 return 一个错误。

但仅仅因为 BleClient.write 解析并不意味着接收方设备在其末端接收并记录了信息。如果发起设备和接收设备之间出现连接故障,BleClient.write 很可能已经完成了它的传输份额并得到解决,但接收设备可能仍未收到数据。为确保您的接收设备按预期获取数据,接收设备端必须有一些逻辑来处理传入数据并将某些内容写入其自身的 GATT 特性,以指示成功从发起设备接收数据。然后,发起设备可以使用 BleClient.read() 从接收设备的 GATT 特性中读取此信息,以确认接收设备确实获取了数据。