Skip to content

Commit

Permalink
More cleanly support isConnected usage with a bool return value
Browse files Browse the repository at this point in the history
Related to discussion on #1015
  • Loading branch information
peitschie committed Jun 26, 2024
1 parent 8be79ef commit 028f2df
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -838,9 +838,14 @@ 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);

// Promises with boolean return
const isConnected = await ble.withPromises.isConnected(device_id, false);

// Promises with rejection
await ble.withPromises.isConnected(device_id); // throws if not connected
```

### Description
Expand All @@ -858,6 +863,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 +873,22 @@ ble.isConnected(
console.log('Peripheral is *not* connected');
}
);

// Promises with boolean return
const isConnected = await ble.withPromises.isConnected(device_id, false);
if (isConnected) {
console.log('Peripheral is connected');
} else {
console.log('Peripheral is *not* connected');
}

// Promises with rejection
try {
await ble.withPromises.isConnected(device_id);
console.log('Peripheral is connected');
} catch (e) {
console.log('Peripheral is *not* connected');
}
```

## isEnabled
Expand Down
7 changes: 6 additions & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,14 @@ 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 promise that resolves to true if the device is connected,
otherwise resolves to false if the device is not connected or an error occurs */
isConnected(device_id: string, rejectWhenDisconnected: false): Promise<boolean>;

/* Returns a rejected promise if bluetooth is not connected */
isEnabled(): Promise<void>;

Expand Down
12 changes: 10 additions & 2 deletions www/ble.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,17 @@ module.exports.withPromises = {
});
},

isConnected: function (device_id) {
isConnected: function (device_id, rejectWhenDisconnected) {
return new Promise(function (resolve, reject) {
module.exports.isConnected(device_id, resolve, reject);
if (rejectWhenDisconnected === false) {
module.exports.isConnected(
device_id,
() => resolve(true),
() => resolve(false)
);
} else {
module.exports.isConnected(device_id, resolve, reject);
}
});
},

Expand Down

0 comments on commit 028f2df

Please sign in to comment.