Skip to content

Commit

Permalink
Clarify isConnected usage to clearly demonstrate bool return case
Browse files Browse the repository at this point in the history
Related to discussion on #1015
  • Loading branch information
peitschie committed Jun 25, 2024
1 parent 1dcafeb commit 18883b6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 () {
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ declare namespace BLECentralPlugin {
): Promise<void>;
stopNotification(device_id: string, service_uuid: string, characteristic_uuid: string): Promise<void>;

/* 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<void>;

/* Returns a rejected promise if bluetooth is not connected */
Expand Down

0 comments on commit 18883b6

Please sign in to comment.