angular-web-bluetooth
angular-web-bluetooth copied to clipboard
Example does not work: MergeMap Errors
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'.
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 :)