Question

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?

Was it helpful?

Solution

Well, it turns out, that my code was correct after all. Apparently the Bluetooth Stack had entered a faulty state that could only be recovered by switching the device off and then turning it on again.

FYI: I have Nexus 7 devices. One of the first edition (I think it came out at the end of 2012) and a recent one.

Bluetooth Low Energy does not work on the 2012 Nexus 7. (Not even after switching it off and on again).

Both devices are running Android 4.4.2.

I conclude, that Bluetooth Low Energy does not work on Nexus 7 2012 and is unstable on the current Nexus 7.

How very sad this is.

OTHER TIPS

The problem is that android will only allow 6 maximum bluetooth connections. You must call mBluetoothGatt.close() when the device disconnects

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top