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

Improvement add new params timeout for disconnect #646

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions src/bleClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,9 @@ class BleClientClass implements BleClientInterface {
return isBonded;
}

async disconnect(deviceId: string): Promise<void> {
async disconnect(deviceId: string, options?: TimeoutOptions): Promise<void> {
await this.queue(async () => {
await BluetoothLe.disconnect({ deviceId });
await BluetoothLe.disconnect({ deviceId, ...options });
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ export interface BluetoothLePlugin {
connect(options: DeviceIdOptions & TimeoutOptions): Promise<void>;
createBond(options: DeviceIdOptions & TimeoutOptions): Promise<void>;
isBonded(options: DeviceIdOptions): Promise<BooleanResult>;
disconnect(options: DeviceIdOptions): Promise<void>;
disconnect(options: DeviceIdOptions & TimeoutOptions): Promise<void>;
getServices(options: DeviceIdOptions): Promise<BleServices>;
discoverServices(options: DeviceIdOptions): Promise<void>;
getMtu(options: DeviceIdOptions): Promise<GetMtuResult>;
Expand Down