From 0b1ccfec98164252a5fe85f9fae0d33317d5d257 Mon Sep 17 00:00:00 2001 From: pwespi Date: Sat, 21 Aug 2021 12:36:18 +0200 Subject: [PATCH] fix(): fix type of ScanResult --- src/bleClient.ts | 15 ++++++++++----- src/definitions.ts | 8 ++++---- src/web.spec.ts | 2 +- src/web.ts | 6 +++--- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/bleClient.ts b/src/bleClient.ts index 6dbbb730..7a494f5e 100644 --- a/src/bleClient.ts +++ b/src/bleClient.ts @@ -218,11 +218,16 @@ class BleClientClass implements BleClientInterface { async requestLEScan(options: RequestBleDeviceOptions, callback: (result: ScanResult) => void): Promise { await this.queue(async () => { await this.scanListener?.remove(); - this.scanListener = await BluetoothLe.addListener('onScanResult', (result: ScanResultInternal) => { - result.manufacturerData = this.convertObject(result.manufacturerData); - result.serviceData = this.convertObject(result.serviceData); - result.rawAdvertisement = result.rawAdvertisement ? this.convertValue(result.rawAdvertisement) : undefined; - callback(result as ScanResult); + this.scanListener = await BluetoothLe.addListener('onScanResult', (resultInternal: ScanResultInternal) => { + const result: ScanResult = { + ...resultInternal, + manufacturerData: this.convertObject(resultInternal.manufacturerData), + serviceData: this.convertObject(resultInternal.serviceData), + rawAdvertisement: resultInternal.rawAdvertisement + ? this.convertValue(resultInternal.rawAdvertisement) + : undefined, + }; + callback(result); }); await BluetoothLe.requestLEScan(options); }); diff --git a/src/definitions.ts b/src/definitions.ts index 8b07d7e7..1717f73c 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -109,8 +109,8 @@ export interface ReadResult { export interface ScanResultInternal { device: BleDevice; localName?: string; - rssi: number; - txPower: number; + rssi?: number; + txPower?: number; manufacturerData?: { [key: string]: T }; serviceData?: { [key: string]: T }; uuids?: string[]; @@ -131,11 +131,11 @@ export interface ScanResult { /** * Received Signal Strength Indication. */ - rssi: number; + rssi?: number; /** * Transmit power in dBm. A value of 127 indicates that it is not available. */ - txPower: number; + txPower?: number; /** * Manufacturer data, key is a company identifier and value is the data. */ diff --git a/src/web.spec.ts b/src/web.spec.ts index c5ef6ab1..51dc70df 100644 --- a/src/web.spec.ts +++ b/src/web.spec.ts @@ -10,7 +10,7 @@ interface BluetoothLeWithPrivate extends BluetoothLePlugin { deviceMap: Map; discoverdDevices: Map; scan: BluetoothLEScan | null; - onAdvertisemendReceived: (event: BluetoothAdvertisementEvent) => void; + onAdvertisemendReceived: (event: BluetoothAdvertisingEvent) => void; onDisconnected: (event: Event) => void; } diff --git a/src/web.ts b/src/web.ts index 9fe9f603..006b6970 100644 --- a/src/web.ts +++ b/src/web.ts @@ -73,7 +73,7 @@ export class BluetoothLeWeb extends WebPlugin implements BluetoothLePlugin { }); } - private onAdvertisemendReceived(event: BluetoothAdvertisementEvent): void { + private onAdvertisemendReceived(event: BluetoothAdvertisingEvent): void { // do not use `this` in event listener const deviceId = event.device.id; BluetoothLe.deviceMap.set(deviceId, event.device); @@ -196,8 +196,8 @@ export class BluetoothLeWeb extends WebPlugin implements BluetoothLePlugin { await characteristic?.stopNotifications(); } - private getFilters(options?: RequestBleDeviceOptions): BluetoothRequestDeviceFilter[] { - const filters: BluetoothRequestDeviceFilter[] = []; + private getFilters(options?: RequestBleDeviceOptions): BluetoothLEScanFilter[] { + const filters: BluetoothLEScanFilter[] = []; for (const service of options?.services ?? []) { filters.push({ services: [service],