Bluetooth Low Energy Service Discovery with Android 4.3 on Nexus 4
Asked Answered
A

6

7

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

Ament answered 28/9, 2013 at 18:57 Comment(8)
What is the remote device?Emory
bluegiga.com/en-US/products/bluetooth-4.0-modules/…Ament
android.googlesource.com/platform/external/bluetooth/bluedroid/… Status 129 (0x81) is GATT_ERROR, though I am sure this information is not going to help you.Heffernan
Actually, some indication of what it means is way more than I had yesterday. I'm not sure what the solution is yet, but at least I know where it's coming from.Ament
it's very flaky. Try Bluetooth on then off first. Try after a fresh boot. Once you get it in a state it just dose not recover sometimes without these.Kiker
I've actually put a startup process that turns off bluetooth and the restarts it, just to reliably find the device. I have noticed however, that the apps available from play detect the device much much faster than mine does. Not sure why.Ament
I have the same problem with a Nordic Semiconductor BLE dev kit and Android 4.3 on the Galaxy Nexus (with BLE enabled) and a reboot of the phone fixed it. Annoying.Cram
I think we're at the mercy of google / phone manufacturers on this one.Ament
P
6

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.

Purkey answered 25/10, 2013 at 13:20 Comment(1)
I feel like you are correct because this error only seems to occur to me when I leave the device at the edge of the peripheral's reception. It needs to be connecting / disconnecting for 1 hr before I start seeing this problem. Unfortunately when the device is at the edge of the reception, that's exactly when I want to try to reconnect aggressively without timeouts.Bund
G
4

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

Gazette answered 5/2, 2014 at 19:59 Comment(0)
B
2

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.

Ballesteros answered 25/12, 2013 at 23:10 Comment(1)
That's some interesting information, but disabling other features (such as wifi), or restarting / rebooting everytime I want to use this is not a valid solution. I need something that works with wifi, and doesn't require resets / cycling Bluetooth radios, etc. I have since posting this successfully implemented a version on iOS that works without any issues. I think this needs to be fixed by google or android or the phone manufacturers, so that we don't have to lose features or jump though hoops to get a reliable system running.Ament
R
1

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

Rajiv answered 25/10, 2013 at 11:56 Comment(0)
H
1

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.

Hel answered 9/2, 2015 at 11:38 Comment(0)
T
0

i got the same error using the TI-CC2541 Chip. The solution was to use the 128kB Chip instead of the 256kB. For instance, the "SimpleBLEPeripheral" is made for the 128kB Chip.

Tad answered 10/2, 2015 at 11:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.