From f1323e7e43bbd57673f56c03d10f36e24d37fba1 Mon Sep 17 00:00:00 2001 From: Corentin Recanzone Date: Wed, 17 Apr 2024 16:38:17 +0200 Subject: [PATCH 1/2] perf: improvement add new params timeout for disconnect --- src/bleClient.ts | 4 ++-- src/definitions.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bleClient.ts b/src/bleClient.ts index 34de6b06..65642e86 100644 --- a/src/bleClient.ts +++ b/src/bleClient.ts @@ -488,9 +488,9 @@ class BleClientClass implements BleClientInterface { return isBonded; } - async disconnect(deviceId: string): Promise { + async disconnect(deviceId: string, options?: TimeoutOptions): Promise { await this.queue(async () => { - await BluetoothLe.disconnect({ deviceId }); + await BluetoothLe.disconnect({ deviceId, ...options }); }); } diff --git a/src/definitions.ts b/src/definitions.ts index b32b217d..65986b4e 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -295,7 +295,7 @@ export interface BluetoothLePlugin { connect(options: DeviceIdOptions & TimeoutOptions): Promise; createBond(options: DeviceIdOptions & TimeoutOptions): Promise; isBonded(options: DeviceIdOptions): Promise; - disconnect(options: DeviceIdOptions): Promise; + disconnect(options: DeviceIdOptions & TimeoutOptions): Promise; getServices(options: DeviceIdOptions): Promise; discoverServices(options: DeviceIdOptions): Promise; getMtu(options: DeviceIdOptions): Promise; From 8cbb4630493363313e50f5a5224fa62d7454f7aa Mon Sep 17 00:00:00 2001 From: Corentin Recanzone Date: Wed, 17 Apr 2024 16:47:40 +0200 Subject: [PATCH 2/2] fix: fixed disconnect timeout --- .../community/plugins/bluetoothle/Device.kt | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/Device.kt b/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/Device.kt index f09e6a0b..60b266b7 100644 --- a/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/Device.kt +++ b/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/Device.kt @@ -423,16 +423,28 @@ class Device( return device.bondState == BluetoothDevice.BOND_BONDED } - fun disconnect( - timeout: Long, callback: (CallbackResponse) -> Unit - ) { + fun disconnect(timeout: Long, callback: (CallbackResponse) -> Unit) { val key = "disconnect" callbackMap[key] = callback + + // Add a listener for Bluetooth status to dynamically handle status changes + val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter() + if (!bluetoothAdapter.isEnabled) { + // If Bluetooth is disabled, immediately inform the callback of the disconnection failure + resolve(key, "Failed: Bluetooth is turned off.") + return + } + + // If bluetoothGatt is null, this means that the connection is no longer active or was never established if (bluetoothGatt == null) { - resolve(key, "Disconnected.") + resolve(key, "Disconnected: No active connection.") return } + + // Proceed with disconnection bluetoothGatt?.disconnect() + + // Set a timeout for disconnection setTimeout(key, "Disconnection timeout.", timeout) }