I am working with a BT Low Energy capable baggage alert device (Link) and have successfully paired it with my Nexus 7.
Following the docs I now would like to connect to the device using the following code:
private BluetoothGattCallback callback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status,
int newState) {
Log.i(TAG, "le onConnectionStateChange ["+newState+"]");
if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.i(TAG, "le device connected");
onConnect(gatt.getDevice());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.i(TAG, "le device disconnected");
onDisconnect(gatt.getDevice());
}
}
@Override
public void onServicesDiscovered (BluetoothGatt gatt, int status) {
Log.i(TAG, "onServicesDiscovered");
}
};
for (BluetoothDevice device : BluetoothAdapter.getDefaultAdapter().getBondedDevices()) {
int type = device.getType();
if (type == BluetoothDevice.DEVICE_TYPE_LE || type == BluetoothDevice.DEVICE_TYPE_DUAL) {
List<BluetoothDevice> connectedDevices =
bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
if (!connectedDevices.contains(device)) {
BluetoothGatt gatt = device.connectGatt(App.getContext(), false, callback);
gatt.connect();
gatt.discoverServices();
List<BluetoothGattService> services = gatt.getServices();
}
}
}
However no connection can be initiated. After a while (a few seconds) the connection state changes to BluetoothProfile.STATE_DISCONNECTED - that is even though BluetoothProfile.STATE_CONNECTED was never reached. Am I doing something wrong here?