manekinekko / angular-web-bluetooth

The missing Web Bluetooth module for Angular
https://manekinekko.github.io/angular-web-bluetooth/
MIT License
195 stars 56 forks source link

Example does not work: MergeMap Errors #89

Open TheOd1n opened 1 year ago

TheOd1n commented 1 year ago

Example 2.b) generates the following error message when copied to the Angular project:

Argument of type 'OperatorFunction<BluetoothRemoteGATTService, void | BluetoothRemoteGATTCharacteristic>' is not assignable to parameter of type 'OperatorFunction<BluetoothRemoteGATTService, BluetoothRemoteGATTCharacteristic>'. Type 'void | BluetoothRemoteGATTCharacteristic' is not assignable to type 'BluetoothRemoteGATTCharacteristic'. Type 'void' is not assignable to type 'BluetoothRemoteGATTCharacteristic'.

batchi92 commented 1 year ago

There are several bugs in the library. The Promise.reject(...) parts in bluetooth.service.ts line 162, 255 and 264 must be returned. In the else branch of line 165 a Promise.reject(...) must also be returned. I use the following hot fix:

  private readonly fixMap = <T>(t: T | void) => {
    if (!t) {
      throw new Error();
    }
    return t;
  };

  private readonly device$ = this.bluetooth
    .discover$({
      optionalServices: [this.serviceUuid],
      filters: [{ services: [this.globalServiceUuid] }],
    })
    .pipe(
      // Hot fix for bug in bluetooth core
      map(this.fixMap),
      mergeMap(server =>
        this.bluetooth.getPrimaryService$(server, this.serviceUuid)
      ),
      mergeMap(service =>
        this.bluetooth.getCharacteristic$(service, this.characteristicUuid)
      ),
      // Hot fix for bug in bluetooth core
      map(this.fixMap)
    );

@manekinekko you should enable strict mode to avoid such mistakes. Best greetings :)