From 62db23a607cd8668fffe2c657d407c506afd915d Mon Sep 17 00:00:00 2001 From: Philip Peitsch Date: Thu, 26 Sep 2019 04:37:59 +1000 Subject: [PATCH] Add types for withPromises variant of cordova-plugin-ble-central (#38584) * Improve variable naming slightly for clarity * Add types for withPromises variant of cordova-plugin-ble-central These were added some time ago in the source repo: https://github.com/don/cordova-plugin-ble-central/blob/ac55995dc9e9d19f231c13e6bbfb9ba24b076480/www/ble.js#L229 --- .../cordova-plugin-ble-central-tests.ts | 42 +++++++++++------ types/cordova-plugin-ble-central/index.d.ts | 45 +++++++++++++++---- 2 files changed, 65 insertions(+), 22 deletions(-) diff --git a/types/cordova-plugin-ble-central/cordova-plugin-ble-central-tests.ts b/types/cordova-plugin-ble-central/cordova-plugin-ble-central-tests.ts index f7830c18b2..d29d152921 100644 --- a/types/cordova-plugin-ble-central/cordova-plugin-ble-central-tests.ts +++ b/types/cordova-plugin-ble-central/cordova-plugin-ble-central-tests.ts @@ -58,10 +58,11 @@ var demoExtendedData : BLECentralPlugin.PeripheralDataExtended = { ] }; +const common : BLECentralPlugin.BLECentralPluginPromises | BLECentralPlugin.BLECentralPluginStatic = ble; //get updates about the bluethooth states -ble.startStateNotifications((state) => log(`BLE state ${state}`)); -ble.startStateNotifications((state) => log(`BLE state ${state}`), (err: string) => log(`things when wrong ${err}`)); +common.startStateNotifications((state) => log(`BLE state ${state}`)); +common.startStateNotifications((state) => log(`BLE state ${state}`), (err: string) => log(`things when wrong ${err}`)); ble.isEnabled(()=> log(`bluetooth is enabled`), err =>log(`bluetooth is not enabled: ${err}`)); @@ -76,18 +77,18 @@ ble.stopStateNotifications(() => log(`yes it worked`)) ble.stopStateNotifications(() => log(`yes it worked`), () => log(`nope it didn't work`)) //scan 5 seconds -ble.scan([], 5000, (data) => { devices.push(data); }); -ble.scan([], 5000, (data) => { devices.push(data); }, () => log(`couldn't connect`) ); -ble.scan([], 5000, (data) => { devices.push(data); }, err => log(`couldn't connect: ${err}`) ); +common.scan([], 5000, (data) => { devices.push(data); }); +common.scan([], 5000, (data) => { devices.push(data); }, () => log(`couldn't connect`) ); +common.scan([], 5000, (data) => { devices.push(data); }, err => log(`couldn't connect: ${err}`) ); //scan continously -ble.startScan([], (data) => { devices.push(data); }) -ble.startScan([], (data) => { devices.push(data); }, () => log('couldn\'t connect') ) -ble.startScan([], (data) => { devices.push(data); }, err => log(`couldn't connect: ${err}`) ); +common.startScan([], (data) => { devices.push(data); }) +common.startScan([], (data) => { devices.push(data); }, () => log('couldn\'t connect') ) +common.startScan([], (data) => { devices.push(data); }, err => log(`couldn't connect: ${err}`) ); ////scan continously -ble.startScanWithOptions([], {reportDuplicates:false }, (data) => { devices.push(data); }); -ble.startScanWithOptions([], {reportDuplicates:false }, (data) => { devices.push(data); }, err => log(`couldn't connect: ${err}`)); +common.startScanWithOptions([], {reportDuplicates:false }, (data) => { devices.push(data); }); +common.startScanWithOptions([], {reportDuplicates:false }, (data) => { devices.push(data); }, err => log(`couldn't connect: ${err}`)); //stop scanning ble.stopScan(()=> log('all good'), ()=> log('couldn\'t stop scanning')); @@ -98,7 +99,7 @@ ble.isConnected(demoDevice.id, () => log(`already connected to this device`), () //connect to a specific device var extendedData : BLECentralPlugin.PeripheralDataExtended; -ble.connect(demoDevice.id, (data)=> extendedData = data, err => log(`couldn't connect to the device: ${err}`) ); +common.connect(demoDevice.id, (data)=> extendedData = data, err => log(`couldn't connect to the device: ${err}`) ); //read some data from a characteristic var charsOfOneOfItsServices = demoExtendedData.characteristics.filter((value) => value.service == demoExtendedData.services[0]); @@ -122,9 +123,9 @@ ble.refreshDeviceCache(demoDevice.id, 10, () => log('it worked'), () => log('it var notificationsReceived : number = 0; //get notified of changes for that characteristic -ble.startNotification(demoDevice.id, charsOfOneOfItsServices[0].service, charsOfOneOfItsServices[0].characteristic, +common.startNotification(demoDevice.id, charsOfOneOfItsServices[0].service, charsOfOneOfItsServices[0].characteristic, (data)=> notificationsReceived++); -ble.startNotification(demoDevice.id, charsOfOneOfItsServices[0].service, charsOfOneOfItsServices[0].characteristic, +common.startNotification(demoDevice.id, charsOfOneOfItsServices[0].service, charsOfOneOfItsServices[0].characteristic, (data)=> notificationsReceived++, err => log(`darn: ${err}`)); //write some data @@ -147,3 +148,18 @@ ble.stopNotification(demoDevice.id, charsOfOneOfItsServices[0].service, charsOfO ble.disconnect(demoDevice.id); ble.disconnect(demoDevice.id, () => log('it worked')); ble.disconnect(demoDevice.id, () => log('it worked'), () => log('it failed')); + +async function withPromises() { + await ble.withPromises.stopScan(); + await ble.withPromises.disconnect(demoDevice.id); + await ble.withPromises.read(demoDevice.id, charsOfOneOfItsServices[0].service, charsOfOneOfItsServices[0].characteristic); + await ble.withPromises.write(demoDevice.id, charsOfOneOfItsServices[0].service, charsOfOneOfItsServices[0].characteristic, new ArrayBuffer(40)); + await ble.withPromises.writeWithoutResponse(demoDevice.id, charsOfOneOfItsServices[0].service, charsOfOneOfItsServices[0].characteristic, new ArrayBuffer(40)); + await ble.withPromises.stopNotification(demoDevice.id, charsOfOneOfItsServices[0].service, charsOfOneOfItsServices[0].characteristic); + await ble.withPromises.isConnected(demoDevice.id); + await ble.withPromises.isEnabled(); + await ble.withPromises.enable(); + await ble.withPromises.showBluetoothSettings(); + await ble.withPromises.stopStateNotifications(); + await ble.withPromises.readRSSI(demoDevice.id); +} diff --git a/types/cordova-plugin-ble-central/index.d.ts b/types/cordova-plugin-ble-central/index.d.ts index 070b62ff9e..27e876dd3d 100644 --- a/types/cordova-plugin-ble-central/index.d.ts +++ b/types/cordova-plugin-ble-central/index.d.ts @@ -3,6 +3,7 @@ // Definitions by: Gidon Junge // Philip Peitsch // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 declare namespace BLECentralPlugin { @@ -39,22 +40,51 @@ declare namespace BLECentralPlugin { reportDuplicates?: boolean; } - export interface BLECentralPluginStatic { + interface BLECentralPluginCommon { scan(services: string[], seconds: number, success : (data: PeripheralData) => any, failure? : (error: string) => any): void; startScan(services: string[], success: (data: PeripheralData) => any, failure?: (error: string|BLEError) => any): void; startScanWithOptions(services: string[], options: StartScanOptions, success: (data: PeripheralData) => any, failure?: (error: string) => any): void; + connect(device_id:string, connectCallback: (data: PeripheralDataExtended) => any, disconnectCallback: (error: string|BLEError) => any): void; + + /* Register to be notified when the value of a characteristic changes. */ + startNotification(device_id: string, service_uuid:string, characteristic_uuid:string, success: (rawData: ArrayBuffer) => any, failure?: (error: string|BLEError) => any): void; + + startStateNotifications(success: (state: string) => any, failure?: (error: string) => any): void; + } + + + + export interface BLECentralPluginPromises extends BLECentralPluginCommon { + stopScan() : Promise; + disconnect(device_id: string) : Promise; + read(device_id: string, service_uuid: string, characteristic_uuid: string) : Promise; + write(device_id: string, service_uuid: string, characteristic_uuid: string, value: ArrayBuffer): Promise; + writeWithoutResponse(device_id: string, service_uuid: string, characteristic_uuid: string, value: ArrayBuffer): Promise; + stopNotification(device_id: string, service_uuid: string, characteristic_uuid: string): Promise; + + /* Returns a rejected promise if the device is not connected */ + isConnected(device_id: string): Promise; + + /* Returns a rejected promise if bluetooth is not connected */ + isEnabled(): Promise; + + enable(): Promise; + showBluetoothSettings(): Promise; + stopStateNotifications(): Promise; + readRSSI(device_id: string): Promise; + } + + export interface BLECentralPluginStatic extends BLECentralPluginCommon { stopScan(): void; stopScan(success: () => any, failure?: () => any): void; - connect(device_id:string, success: (data: PeripheralDataExtended) => any, failure: (error: string|BLEError) => any): void; - /* Automatically connect to a device when it is in range of the phone [iOS] background notifications on ios must be enabled if you want to run in the background [Android] this relies on the autoConnect argument of BluetoothDevice.connectGatt(). Not all Android devices implement this feature correctly. */ - autoConnect(device_id:string, success: (data: PeripheralDataExtended) => any, failure: (error: string|BLEError) => any): void; + autoConnect(device_id:string, connectCallback: (data: PeripheralDataExtended) => any, disconnectCallback: (error: string|BLEError) => any): void; disconnect(device_id:string, success?: () => any, failure?: (error: string|BLEError) => any): void; @@ -66,9 +96,6 @@ declare namespace BLECentralPlugin { The success callback is be called when the characteristic is written.*/ writeWithoutResponse(device_id: string, service_uuid:string, characteristic_uuid:string, data: ArrayBuffer, success?: () => any, failure?: (error: string) => any): void; - /* Register to be notified when the value of a characteristic changes. */ - startNotification(device_id: string, service_uuid:string, characteristic_uuid:string, success: (rawData: ArrayBuffer) => any, failure?: (error: string|BLEError) => any): void; - stopNotification(device_id: string, service_uuid:string, characteristic_uuid:string, success?: () => any, failure?: (error: string|BLEError) => any): void; /* Reports if bluetooth is enabled. */ @@ -86,8 +113,6 @@ declare namespace BLECentralPlugin { [iOS] refreshDeviceCache is not supported on iOS. */ refreshDeviceCache(device_id: string, timeout_millis: number, success?: (data: PeripheralDataExtended) => any, failure?: (error: string|BLEError) => any): void; - startStateNotifications(success: (state: string) => any, failure?: (error: string) => any): void; - stopStateNotifications(success?: () => any, failure?: () => any): void; /* Opens the Bluetooth settings for the operating systems. @@ -113,6 +138,8 @@ declare namespace BLECentralPlugin { /* Find the bonded devices. [iOS] bondedDevices is not supported on iOS. */ bondedDevices(success: (data: PeripheralData[]) => any, failure: () => any): void; + + withPromises: BLECentralPluginPromises; } }