Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bluetooth Low Energy Service Discovery with Android 4.3 on Nexus 4

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

like image 420
jnewt Avatar asked Sep 28 '13 18:09

jnewt


5 Answers

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.

like image 123
Michael Heiser Avatar answered Nov 04 '22 01:11

Michael Heiser


from BleConstants.java public static final int GATT_INTERNAL_ERROR = 129;

like image 38
user1603602 Avatar answered Nov 04 '22 01:11

user1603602


I did several tests on my Nexus 7 with TI sensor tag.

  1. Nexus 7 (Android 4.3) --> 0x81 GATT_INTERNAL_ERROR
  2. Upgraded Nexus 7 to Android 4.4.2 --> 0x81 GATT_INTERNAL_ERROR
  3. Turn off WiFi, onServicesDiscovered was never called.
  4. Turn off and turn on Bluetooth, it works!!!
  5. Turn on WiFi, onServicesDiscovered failed again.
  6. Turn off WiFi, it works again.

I also test the same program on Galaxy S4 (Android 4.3) and it works well at all time.

Therefore, I believe that the BLE stack on Nexus 7 is not good.

If you can live with no WiFi, it may be OK, but if you can find some other Android 4.3 device, you shall try other devices.

like image 32
Chih-Wei Chang Avatar answered Nov 04 '22 00:11

Chih-Wei Chang


Well, I had the same Problem.

Inspecting the binary values of the constants within BluetoothGATT I suggest that simply 129 Means Failure. Maybe, while defining the Constants someone mis-typed a 0 too much and it ended up being 257 instead of 129. (If it's a Status code always look for the binary representation, it reveals more than a decimal value)

I will definitely not look into the Android source code just to find out if it is inserted in binary in there or in decimals.

   Binary    Dec
-----------  ---
0 1000 0001  129
1 0000 0001  257
0 0000 0101  5
0 0000 1111  15
0 0000 1101  13
0 0000 0111  7
0 0000 0110  6
0 0000 0000  0
0 0000 0011  3

Still, you need a solution to this ? For me rebooting the phone -twice- helped. During second reboot I first turned off Bluetooth manually.

Until the error occured I had programmatically shut-down and reactivated Bluetooth on every test run of my app. It worked flawless for almost 2 Months. Then I removed the "waste of time" BT-restart-code , and this error showed up and cost me half a day to check out.

Dedection time was never an issue for me, devices show up instantly.

Development Phone : Nexus 4 / Android 4.3 (updated constantly) BLE Device : Texas instruments CC2541

like image 37
chippy Avatar answered Nov 04 '22 00:11

chippy


I get this error if my BLE device is already paired with my phone. Error gets resolved once I un-pair the device and discover service again.

like image 24
Sudhee Avatar answered Nov 04 '22 02:11

Sudhee