Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if BLE device is connected currently

Is there any method like device.isConnected() to know the present state of android BLE device . What I know is I need to implement some architecture like interface from GattclientCallBack class to know if device is connected,but I don't find any method like this. Also the method

 mBluetoothGatt.getConnectionState(device);

returns int . So exactly how do I know if device is connected ? Thanks :)

like image 295
Pritish Avatar asked Jan 07 '18 17:01

Pritish


1 Answers

The int returned by getConnectionState will be 0, 1, 2, or 3, corresponding to STATE_DISCONNECTED, STATE_CONNECTING, STATE_CONNECTED, or STATE_DISCONNECTING respectively. See the docs for more information. Note that this method is only available on API level 18 and higher.

However, the documentation of this method includes the following warning:

Not supported - please use getConnectedDevices(int) with GATT as argument

This refers to the method BluetoothManager.getConnectedDevices. The constant value to pass is BluetoothProfile.GATT. Again, this method is only available on API level 18 and higher. It sounds like this method will be perfect for your use-case.

like image 191
stkent Avatar answered Nov 19 '22 16:11

stkent