NordicSemiconductor / Android-BLE-Library

A library that makes working with Bluetooth LE on Android a pleasure. Seriously.
BSD 3-Clause "New" or "Revised" License
2.04k stars 419 forks source link

disconnect the device #411

Closed LucasLeung1024 closed 2 years ago

LucasLeung1024 commented 2 years ago

hi, can u explain to disconnect a connected device, I'm trying to use disconnect() in

public final DisconnectRequest disconnect() {
   return Request.disconnect().setRequestHandler(requestHandler);
}

but nothing happend,

niklasdahlheimer commented 2 years ago

In your class which extends BleManager you can call

disconnect().enqueue();

which triggers an async disconnect. you can add callbacks when disconnect succeeds or fails by calling

  disconnect()
                .done(this::onDisconnected)
                .fail(this::onConnectionOperationFailed)
                .enqueue();

where onDisconnected and onConnectionOperationFailed are functions in your class which have a signature like

protected void onDisconnected(BluetoothDevice device){
// your callback code
}
philips77 commented 2 years ago

Thank you @niklasdahlheimer for excellent answer!