Essential Android Bluetooth Low Energy (LE) edit forum

Connecting to a GATT Server

Once you have discovered a desired BluetoothDevice object, you can connect to it by using its connectGatt() method which takes as parameters a Context object, a boolean indicating whether to automatically connect to the BLE device and a BluetoothGattCallback reference where connection events and client operations results will be delivered:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        device.connectGatt(context, false, bluetoothGattCallback, BluetoothDevice.TRANSPORT_AUTO);
    } else {
        device.connectGatt(context, false, bluetoothGattCallback);
    }

Override onConnectionStateChange in BluetoothGattCallback to receive connection an disconnection events:

BluetoothGattCallback bluetoothGattCallback =
    new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status,
        int newState) {
    if (newState == BluetoothProfile.STATE_CONNECTED) {
        Log.i(TAG, "Connected to GATT server.");

    } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
        
        Log.i(TAG, "Disconnected from GATT server.");
    }
}
};

Table Of Contents
39 ACRA
64 Menu
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
248 Bluetooth Low Energy (LE)
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close