Gatt Callback Fails to Register

GATT callback fails to register

I finally figured this problem out. The device I am using is a Samsung Galaxy S4 and the actual problem (thanks Wibble for guidance in your answer, but you are slightly off in your conclusion) appears to be a threading issue.

In Wibble's answer, he stated that adding a button to connect fixed his issue. I started wondering why that matters, and I also can connect and disconnect fine during an entire session without a GUI button using background worker threads. As soon as I force close my application, restart it, and try to connect, I start getting the error "Failed to register callback." and nothing works any more. I almost pulled my hair out over this one :)

See my post in Samsung's forums for more detail on my exact issues.

Solution:
To get around this issue, just make sure you run any BLE interaction code (device#connectGatt, connect, disconnect, etc) code in the UIThread (with a handler, local service, or Activity#runOnUiThread). Follow this rule of thumb and you will hopefully avoid this dreadful problem.

Deep in our library, I only had access to the application context. You can create a handler from a context that will post to the main thread by using new Handler(ctx.getMainLooper());

If you face other connection problems, deploy the sample app in samples\android-18\legacy\BluetoothLeGatt and see if that application works. That was kind of my baseline for realizing BLE does actually work with my peripheral, and gave me hope that if I dug enough in our library I would eventually find the answer.

EDIT: I did not see this 'Failed to register callback' issue on the Nexus 4, Nexus 5, or Nexus 7 2013 when using background threads to perform BLE operations. It may just be an issue in Samsungs 4.3 implementation.

Android BluetoothGatt - status 133 - register callback

Alright I have figured it out. The issue was mainly an oversight of when I was reading through the BluetoothGatt documentation. I was calling .disconnect(), but not .close(). Since the Galaxy s4 can only handle 6 connections at a time, my service was only running 6 times. Adding the .close() to my code allowed it to properly shut down the connection and freed up those used connections.

Source that made me re-read the docs more carefully!

So remember to use .close() on your BluetoothGatt object if you have a recurring connection to the same device(s)!!

register with gatt stack failed Error BLE android Samsung

You need to call close() on the BluetoothGatt object when you're done with it. Otherwise you will eventually run out of available BluetoothGatt objects.

Android BluetoothDevice connectGatt always fails with error 133 on certain devices

I figured what the problem actually was and how to solve it. I'm adding my answer as another check for the generic gatt error (133) thrown on connection attempts because I didn't find one hinting to the same issue.

I have been led to a wrong conclusion by answers like this. I think that bug was long fixed on Android. The fact that I was getting connection failed callback is an indication that the thread issue was irrelevant since the callbacks were registered.

The problem I was facing was related to the fact that I kept the bluetooth adapter scanning while attempting to connect to the BLE device. It appears that some phones have a problem with that.

The requirements of my application dictate continuous scanning for the time the service is running and all the test phones I had around didn't have problems with it. This model of HUAWEI though, would refuse to connect.

TL;DR Stop scanning before attempting to connect, restart scanning if required after disconnect.



Related Topics



Leave a reply



Submit