Bluetooth Low Energy Service Discovery with Android 4.3 on Nexus 4

jnewt picture jnewt · Sep 28, 2013 · Viewed 11.7k times · Source

I'm trying to use a BLE module (bluegiga BLE112) with my nexus 4 (android 4.3). I can connect, get the name of the device, connect to GATT, but service discovery fails.

Here's how do the initial gatt connection (which seems to work successfully:

dev.connectGatt(getBaseContext(), true, btGattCB);

Here's the GATT callback:

private BluetoothGattCallback btGattCB = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        super.onConnectionStateChange(gatt, status, newState);
        if(newState == BluetoothProfile.STATE_CONNECTED){
            Log.i(TAG, "Gatt Connected");
            gatt.discoverServices();
        }
        else if(newState == BluetoothProfile.STATE_DISCONNECTED){
            Log.i(TAG, "Gatt Disconnected");
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status){
        Log.i(TAG,"Status onServiceDiscovered: "+status);   //status code i'm getting here is 129
        List<BluetoothGattService> btServices = gatt.getServices();//try anyway
    }
};

And Here's my Log:

09-28 12:58:37.611    4118-4130/com.jnewt.btFive I/PDU? Scan Callback
09-28 12:58:37.611    4118-4130/com.jnewt.btFive I/PDU? Device is: 00:07:80:67:2F:63
09-28 12:58:37.611    4118-4130/com.jnewt.btFive I/PDU? Device Name: BGT GPIO Test
09-28 12:58:43.607    4118-4118/com.jnewt.btFive I/PDU? Scan Timeout
09-28 12:59:13.539    4118-4129/com.jnewt.btFive I/PDU? Gatt Connected
09-28 12:59:43.561    4118-4190/com.jnewt.btFive I/PDU? Service Discovery Failed
09-28 12:59:43.581    4118-4130/com.jnewt.btFive I/PDU? Gatt Disconnected
09-28 13:00:00.920    4118-4129/com.jnewt.btFive I/PDU? Gatt Connected
09-28 13:00:30.902    4118-4130/com.jnewt.btFive I/PDU? Service Discovery Failed
09-28 13:00:30.922    4118-4190/com.jnewt.btFive I/PDU? Gatt Disconnected
09-28 13:01:20.265    4118-4129/com.jnewt.btFive I/PDU? Gatt Connected
09-28 13:01:50.277    4118-4190/com.jnewt.btFive I/PDU? Service Discovery Failed
09-28 13:01:50.297    4118-4130/com.jnewt.btFive I/PDU? Gatt Disconnected
09-28 13:01:56.113    4118-4129/com.jnewt.btFive I/PDU? Gatt Connected
09-28 13:02:26.115    4118-4190/com.jnewt.btFive I/PDU? Service Discovery Failed
09-28 13:02:26.125    4118-4130/com.jnewt.btFive I/PDU? Gatt Disconnected

From the https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html page, I don't see 129 mentioned as a possible status (none of the constants match 129).

I'm all out of ideas at this point. I have isolated the issue to the android phone by testing with an similar example for iphone. I've also tried several of the apps available at the play store, and they have a similar issue (can connect, get name, etc, but no services).

Answer

Michael Heiser picture Michael Heiser · Oct 25, 2013

Actually got the same errorcode today while testing my App on Nexus 7. My problem was, that I called 2 X Gatt.connect in short time. Maybe this helps you. Be aware, that you don't connect twice in a short time to your sensor device.