mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-07-01 15:50:13 +00:00
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:
ac55995dc9/www/ble.js (L229)
This commit is contained in:
committed by
Michael Crane
parent
fa036bbb48
commit
62db23a607
@@ -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);
|
||||
}
|
||||
|
||||
45
types/cordova-plugin-ble-central/index.d.ts
vendored
45
types/cordova-plugin-ble-central/index.d.ts
vendored
@@ -3,6 +3,7 @@
|
||||
// Definitions by: Gidon Junge <https://github.com/gjunge>
|
||||
// Philip Peitsch <https://github.com/peitschie>
|
||||
// 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<void>;
|
||||
disconnect(device_id: string) : Promise<void>;
|
||||
read(device_id: string, service_uuid: string, characteristic_uuid: string) : Promise<ArrayBuffer>;
|
||||
write(device_id: string, service_uuid: string, characteristic_uuid: string, value: ArrayBuffer): Promise<void>;
|
||||
writeWithoutResponse(device_id: string, service_uuid: string, characteristic_uuid: string, value: ArrayBuffer): Promise<void>;
|
||||
stopNotification(device_id: string, service_uuid: string, characteristic_uuid: string): Promise<void>;
|
||||
|
||||
/* Returns a rejected promise if the device is not connected */
|
||||
isConnected(device_id: string): Promise<void>;
|
||||
|
||||
/* Returns a rejected promise if bluetooth is not connected */
|
||||
isEnabled(): Promise<void>;
|
||||
|
||||
enable(): Promise<void>;
|
||||
showBluetoothSettings(): Promise<void>;
|
||||
stopStateNotifications(): Promise<void>;
|
||||
readRSSI(device_id: string): Promise<number>;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user