Pritish
Pritish

Reputation: 1368

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 :)

Upvotes: 6

Views: 10640

Answers (1)

stkent
stkent

Reputation: 20128

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.

Upvotes: 11

Related Questions