2

目前我正在使用这个模块进行角度:Angular web bluetooth

我可以找到一个如何阅读特征并使其正常工作的示例。但是没有样本可写(或通知)。我很确定这个模块能够完成这些任务,请参见此处,但我对 Angular(甚至 JS?)的了解还不够。

有人知道在哪里可以找到示例或/并且可以提供示例吗?

读取电池电量的片段:

getBatteryLevel() {
console.log('Getting Battery Service...');

try {
  return this.ble
    .discover$({
      acceptAllDevices: true,
      optionalServices: [BatteryLevelService.GATT_PRIMARY_SERVICE]
    })
    .mergeMap((gatt: BluetoothRemoteGATTServer) => {
      return this.ble.getPrimaryService$(
        gatt,
        BatteryLevelService.GATT_PRIMARY_SERVICE
      );
    })
    .mergeMap((primaryService: BluetoothRemoteGATTService) => {
      return this.ble.getCharacteristic$(
        primaryService,
        BatteryLevelService.GATT_CHARACTERISTIC_BATTERY_LEVEL
      );
    })
    .mergeMap((characteristic: BluetoothRemoteGATTCharacteristic) => {
      return this.ble.readValue$(characteristic);
    })
    .map((value: DataView) => value.getUint8(0));
} catch (e) {
  console.error('Oops! can not read value from %s');
}

}

谢谢你。

编辑:感谢 Aluan Haddad 的帮助,我能够使用正确的服务和特征 UUID 执行写入请求

            .mergeMap((characteristic: BluetoothRemoteGATTCharacteristic) => {
            let value = new Uint8Array(1);
            value[0] = 2;
          return this.ble.writeValue$(characteristic, value);
        }).subscribe();
4

0 回答 0