Skip to content

Commit

Permalink
feat(android): Add getBondedDevices method to retrieve bonded devices…
Browse files Browse the repository at this point in the history
… on Android (#716)
  • Loading branch information
emanueletoffolon authored Nov 21, 2024
1 parent 8e99165 commit 91a4d00
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Below is an index of all the methods available.
- [`requestLEScan(...)`](#requestlescan)
- [`stopLEScan()`](#stoplescan)
- [`getDevices(...)`](#getdevices)
- [`getBondedDevices()`](#getbondeddevices)
- [`getConnectedDevices(...)`](#getconnecteddevices)
- [`connect(...)`](#connect)
- [`createBond(...)`](#createbond)
Expand Down Expand Up @@ -588,6 +589,20 @@ On Android, you can directly connect to the device with the deviceId.

---

### getBondedDevices()

```typescript
getBondedDevices() => Promise<BleDevice[]>
```

Get a list of currently bonded devices.
Only available on **Android**.
Uses [getBondedDevices](<https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#getBondedDevices()>) on Android

**Returns:** <code>Promise&lt;BleDevice[]&gt;</code>

---

### getConnectedDevices(...)

```typescript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,30 @@ class BluetoothLe : Plugin() {
call.resolve(result)
}

@PluginMethod
fun getBondedDevices(call: PluginCall) {
assertBluetoothAdapter(call) ?: return

val bluetoothManager = activity.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
val bluetoothAdapter = bluetoothManager.adapter

if (bluetoothAdapter == null) {
call.reject("Bluetooth is not supported on this device")
return
}

val bondedDevices = bluetoothAdapter.bondedDevices
val bleDevices = JSArray()

bondedDevices.forEach { device ->
bleDevices.put(getBleDevice(device))
}

val result = JSObject()
result.put("devices", bleDevices)
call.resolve(result)
}

@PluginMethod
fun connect(call: PluginCall) {
val device = getOrCreateDevice(call) ?: return
Expand Down
1 change: 1 addition & 0 deletions ios/Plugin/Plugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
CAP_PLUGIN_METHOD(connect, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(createBond, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(isBonded, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getBondedDevices, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(disconnect, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getServices, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getMtu, CAPPluginReturnPromise);
Expand Down
4 changes: 4 additions & 0 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ public class BluetoothLe: CAPPlugin {
call.unavailable("isBonded is not available on iOS.")
}

@objc func getBondedDevices(_ call: CAPPluginCall) {
call.unavailable("getBondedDevices is not available on iOS.")
}

@objc func disconnect(_ call: CAPPluginCall) {
guard self.getDeviceManager(call) != nil else { return }
guard let device = self.getDevice(call, checkConnection: false) else { return }
Expand Down
3 changes: 3 additions & 0 deletions src/bleClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ jest.mock('./plugin', () => {
getConnectedDevices: jest.fn(() => {
return Promise.resolve({ devices: [] });
}),
getBondeddDevices: jest.fn(() => {
return Promise.resolve({ devices: [] });
}),
connect: jest.fn(),
createBond: jest.fn(),
isBonded: jest.fn(),
Expand Down
14 changes: 14 additions & 0 deletions src/bleClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ export interface BleClientInterface {
*/
getDevices(deviceIds: string[]): Promise<BleDevice[]>;

/**
* Get a list of currently bonded devices.
* Only available on **Android**.
* Uses [getBondedDevices](https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#getBondedDevices()) on Android
*/
getBondedDevices(): Promise<BleDevice[]>;

/**
* Get a list of currently connected devices.
* Uses [retrieveConnectedPeripherals](https://developer.apple.com/documentation/corebluetooth/cbcentralmanager/1518924-retrieveconnectedperipherals) on iOS,
Expand Down Expand Up @@ -460,6 +467,13 @@ class BleClientClass implements BleClientInterface {
});
}

async getBondedDevices(): Promise<BleDevice[]> {
return this.queue(async () => {
const result = await BluetoothLe.getBondedDevices();
return result.devices;
});
}

async connect(deviceId: string, onDisconnect?: (deviceId: string) => void, options?: TimeoutOptions): Promise<void> {
await this.queue(async () => {
if (onDisconnect) {
Expand Down
1 change: 1 addition & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ export interface BluetoothLePlugin {
stopLEScan(): Promise<void>;
getDevices(options: GetDevicesOptions): Promise<GetDevicesResult>;
getConnectedDevices(options: GetConnectedDevicesOptions): Promise<GetDevicesResult>;
getBondedDevices(): Promise<GetDevicesResult>;
addListener(
eventName: 'onEnabledChanged',
listenerFunc: (result: BooleanResult) => void
Expand Down
4 changes: 4 additions & 0 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ export class BluetoothLeWeb extends WebPlugin implements BluetoothLePlugin {
return { devices: bleDevices };
}

async getBondedDevices(): Promise<GetDevicesResult> {
return {} as Promise<GetDevicesResult>;
}

async connect(options: DeviceIdOptions & TimeoutOptions): Promise<void> {
const device = this.getDeviceFromMap(options.deviceId);
device.removeEventListener('gattserverdisconnected', this.onDisconnectedCallback);
Expand Down

0 comments on commit 91a4d00

Please sign in to comment.