From 18883b695fab787a75d9b69f9dbf6799448f7244 Mon Sep 17 00:00:00 2001 From: Philip Peitsch Date: Wed, 26 Jun 2024 08:45:15 +1000 Subject: [PATCH] Clarify isConnected usage to clearly demonstrate bool return case Related to discussion on #1015 --- README.md | 15 ++++++++++++++- types.d.ts | 3 ++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b2496d69..4f0a3c81 100644 --- a/README.md +++ b/README.md @@ -838,9 +838,13 @@ Function `stopNotification` stops a previously registered notification callback. Reports the connection status. ```javascript +// Callbacks ble.isConnected(device_id, success, failure); // Or using await with promises -await ble.withPromises.isConnected(device_id); +const isConnected = await ble.withPromises + .isConnected(device_id) + .then(() => true) + .catch(() => false); ``` ### Description @@ -858,6 +862,7 @@ NOTE that for many apps isConnected is unncessary. The app can track the connect ### Quick Example ```javascript +// Callbacks ble.isConnected( 'FFCA0B09-CB1D-4DC0-A1EF-31AFD3EDFB53', function () { @@ -867,6 +872,14 @@ ble.isConnected( console.log('Peripheral is *not* connected'); } ); + +// Promises +try { + await ble.withPromises.isConnected(device_id); + console.log('Peripheral is connected'); +} catch (e) { + console.log('Peripheral is *not* connected'); +} ``` ## isEnabled diff --git a/types.d.ts b/types.d.ts index 53d12235..ef40d36a 100644 --- a/types.d.ts +++ b/types.d.ts @@ -160,7 +160,8 @@ declare namespace BLECentralPlugin { ): Promise; stopNotification(device_id: string, service_uuid: string, characteristic_uuid: string): Promise; - /* Returns a rejected promise if the device is not connected */ + /* Returns a resolved promise if the device is connected, + otherwise returns rejected promise if the device is not connected */ isConnected(device_id: string): Promise; /* Returns a rejected promise if bluetooth is not connected */