Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify isConnected usage to clearly demonstrate bool return case #1018

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>;
peitschie marked this conversation as resolved.
Show resolved Hide resolved

/* 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
Loading