Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disconnecting a BluetoothDevice without BluetoothGATT

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:

  • Is there a way to disconnect a connected BluetoothDevice without calling connectGatt?
  • Why does connectGatt take so long for a device that's already connected?
  • Is it even valid to call connectGatt on a connected BluetoothDevice?

Thank you

like image 555
James Whong Avatar asked Nov 20 '15 22:11

James Whong


1 Answers

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.

like image 76
AAnkit Avatar answered Sep 28 '22 06:09

AAnkit