My app talks to a BLE peripheral. Sometimes the app is started with that peripheral already connected. I can retrieve the device by calling:
BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
List<BluetoothDevice> connectedDevices = manager.getConnectedDevices(BluetoothProfile.GATT);
I can then filter connectedDevices based on address or UUID. However, BluetoothDevice has no disconnect method. To disconnect, I need a BluetoothGATT instance. But the only way I can see to get a BluetoothGATT instance is to call
connectedDevice.connectGatt(Context, boolean, BluetoothGattCallback)
Which takes a very long time. On top of this, the BluetoothGatt instance I get back after calling connectGatt doesn't seem to actually disconnect the peripheral when I call disconnect().
So my questions are:
Thank you
Here are my 2 cents for your queries.
Is there a way to disconnect a connected BluetoothDevice without
calling connectGatt?
you need to call bluetoothGatt.disconnect();
why do you need to call connectGatt for disconnection? If it is because you need gatt instance, then save it when the device is connected already. Do not call connectGatt on connectedDevice.
Why does connectGatt take so long for a device that's already
connected?
Cause to make a connection both device needs to be in connectable mode(there are modes like advertisement, connectable, non-connectable). Once the connection is made, the device is no longer in connectable mode. Thats the reason for its taking longer. Dont call it, or disconnect before connecting again.
Is it even valid to call connectGatt on a connected
BluetoothDevice?
Everything in coding is valid and legal, but read my answer of second point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With