Android BluetoothGatt - status 133 - register callback

63,902

Solution 1

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

Solution 2

After months of research and pulling my hair out, I have found a solution that is not normally talked about.

Your normal connect request looks something like this:

cGatt.connectGatt(this, false, gattCallback);

There is another version of the connectGatt command, with a 4th parameter. This parameter specifies what type of bluetooth device you are connecting to. I added a "2" to specify that I am connecting via Bluetooth LE. (it's called "transport", forgive me if my explanation is incorrect, but it solved all my problems)

Try this:

cGatt.connectGatt(this, false, gattCallback, 2);

And BAM, now my #133 nightmare is over (I pray)!

Solution 3

Android OS < 6.0:

mBluetoothDevice.connectGatt(context, false, callback);

Android OS >= 6.0:

mBluetoothDevice.connectGatt(context, false, callback, BluetoothDevice.TRANSPORT_LE);

Ultimately, hardware equipment is needed to completely solve this problem.

Solution 4

  • On some devices, it got fixed with mBluetoothDevice.connectGatt(context, false, callback, BluetoothDevice.TRANSPORT_LE);

  • on some devices like Samsung S7, A8, it was a problem with the ScanSettings.Builder().setReportDelay(400) // or 500ms. it should not be 0 or more like 1000ms. ScanSettings settings = new ScanSettings.Builder() .setScanMode(ScanSettings.SCAN_MODE_BALANCED) .setReportDelay(400) .build();

Share:
63,902

Related videos on Youtube

ck1221
Author by

ck1221

Updated on June 08, 2020

Comments

  • ck1221
    ck1221 about 4 years

    First of all I read SOLVED: GATT callback fails to register and took the steps suggested in that post to solve this issue with no success. The recommended fix in there if you haven't read it is to make all BLE calls from the main thread directly or with a handler.

    I am working on a BLE app want to run a service (called from activity once every 10 seconds) that performs these tasks:

    1)Gets list of our products available to connect to (done, works)
    
    2)For each available device:
    
              2a)connect to device
              2b)discover services
              2c)read 5 characteristics in this fashion:
                 2c1)read characteristic
                 2c2)onCharacteristicRead parse data
                 2c3)when finished with data read next characteristic
                 2c4)repeat until all are read (this is done using a state var and switch statement)
             2d)disconnect from device
             2e)connect to next device
             2f)repeat until all devices are read from
             2g)stopSelf()
    

    So the issue... Everything works great for a little bit. I can perform the entire service start {startService(...); in mainActivity} to finish {stopSelf(); in Service} 6 times.

    On the 7th time I get BluetoothGatt Failed to register callback. I'm not sure why I can run it 6 times successfully and then fail on the 7th time.

    Keep in mind I am making all BLE calls from the main thread, and that has been confirmed in the log cat from multiple locations.

    Here is an outline of my code:

    SERVICE.JAVA

    private Handler handler = new Handler();
    private BluetoothGatt cGatt = null;
    private int unitIndex = 0; // keep track of currently connected unit
    private int state = 0; //used to keep track of which characteristic to read next
    
    public int onStartCommand(Intent intent, int flags, int startId) 
    {
        Log.i(TAG, "Service Started...");
        //get ArrayList of units
    
        if(units.size > 0)
            handler.post(connectNextRunnable); //calls connectNextDevice()
        else
            stopSelf();   
    }
    
    private Runnable discoverServices = new Runnable()
    {
        public void run()
        {
            cGatt.discoverServices();
        }
    }
    
    private Runnable readNextValue = new Runnable()
    {
        public void run()
        {
            BluetoothGattCharacteristic c = null;
            switch(state)
            {
                //set c to appropriate characteristic
            default: // all characteristics read
                unitIndex++;
                handler.post(connectNextRunnable)
                return
            }
    
            cGatt.readCharacteristic(c);
        }
    }
    
    private void connectNextDevice()
    {
        if(unitIndex == 0)
            store System.nanoTime in variable
    
        if(unitIndex >= units.size) //finished will all units
            stopSelf();
    
        if(unitIndex < units.size)
            cGatt.disconnect //if null
            cGatt.connectGatt(this, false, gattCallback)
    }
    
    private BluetoothGattCallback gattCallback = new BluetoothGattCallback() 
    {
        public void onConnectionStateChange() 
        {
            handler.post(discoverServices);
        }
    
        public void onServicesDeiscovered() 
        {
            handler.post(readNextValue);
        }
    
        public void onCharacteristicRead() 
        {
            ParseData();
        }
    
        private void ParseData()
        {
            //do stuff with data
            handler.post(readNextValue);
        }
    }
    

    So, like I have said, all BLE stuff is called from the main thread through a handler. The service successfully runs 6 times from start to finish. On the 7th time I get that dumb failed to register callback.

    I can provide more logcat information if you think it is relevant. I did not in the original post because I am output a lot of information to it to verify data received etc..

    The information below is the logcat information for the 7th run of my service from start to finish.

    08-15 12:00:10.746: I/PMIQ BTS(32027): Service Started...
    08-15 12:00:10.746: I/PMIQ BTS(32027): Units: 1
    08-15 12:00:10.746: D/AbsListView(32027): unregisterIRListener() is called 
    08-15 12:00:10.766: I/PMIQ BTS(32027): Connecting to next device...
    08-15 12:00:10.766: I/PMIQ BTS(32027): Unit index = 0
    08-15 12:00:10.766: I/PMIQ BTS(32027): Connecting to pmIQ-IQ130_D93A
    08-15 12:00:10.766: I/System.out(32027): main
    08-15 12:00:10.766: D/BluetoothGatt(32027): connect() - device: 00:1E:C0:19:D9:3A, auto: false
    08-15 12:00:10.766: D/BluetoothGatt(32027): registerApp()
    08-15 12:00:10.766: D/BluetoothGatt(32027): registerApp() - UUID=e9d10870-4b09-451c-a9fa-c6b5f3594a77
    08-15 12:00:10.766: I/BluetoothGatt(32027): Client registered, waiting for callback
    08-15 12:00:10.766: D/BluetoothGatt(32027): onClientRegistered() - status=133 clientIf=0
    08-15 12:00:10.766: I/PMIQ BTS(32027): CONECTION STATE CHANGED...Binder_2
    **08-15 12:00:10.766: E/BluetoothGatt(32027): Failed to register callback**
    08-15 12:00:10.766: I/PMIQ BTS(32027): Could not connect to null ... 257
    08-15 12:00:10.766: I/PMIQ BTS(32027): Connecting to next device...
    08-15 12:00:10.766: I/PMIQ BTS(32027): Unit index = 1
    08-15 12:00:10.766: I/PMIQ BTS(32027): ******************************
    08-15 12:00:10.766: I/PMIQ BTS(32027): Start Time: 4360642409647
    08-15 12:00:10.766: I/PMIQ BTS(32027): End Time: 4360648970925
    08-15 12:00:10.766: I/PMIQ BTS(32027): Difference: 6561278
    08-15 12:00:10.766: I/PMIQ BTS(32027): Time to complete: 6
    08-15 12:00:10.766: I/PMIQ BTS(32027): ******************************
    08-15 12:00:10.876: I/PMIQ BTS(32027): ...Service Destroyed
    

    If you have made it here, thanks! I could not find ANY information on what status=133 means?! It only happens when the callback fails. Every other time it is status=0.

    08-15 12:00:10.766: D/BluetoothGatt(32027): onClientRegistered() - status=133 clientIf=0
    

    If anyone could even answer this.. it may help me greatly. Or if anyone can tell me why it only runs 6 times. Any insight or hunch could be helpful!

    Thanks everyone!

    • ck1221
      ck1221 almost 10 years
      There is a possible answer to this here code.google.com/p/android/issues/detail?id=68538 However I won't be able to test until Monday, it seems I'm not disconnecting properly from the device.
    • prasanthMurugan
      prasanthMurugan over 6 years
      it does work for me i still get error 133 after calling close in android
  • PaulT
    PaulT about 9 years
  • CoDe
    CoDe over 8 years
    if I do .close() then it close BT of device... :| Am I missing anything !!
  • Deko
    Deko over 8 years
    @shubh make sure you do disconnect and close on bluetoothgatt
  • CoDe
    CoDe over 8 years
    Yes, Your are right. But that also not helping, after some sequence Gatt_Error occur (Error Code: 133). Any suggestion!
  • 4ntoine
    4ntoine over 8 years
    what's the difference between disconnect() and close() ? should i use disconnect() + close() or just one of them? Which one?
  • Marian Paździoch
    Marian Paździoch almost 8 years
    @4ntoine this may help you stackoverflow.com/questions/23110295/…
  • andrea.rinaldi
    andrea.rinaldi almost 8 years
    The answer is correct, but it doesn't resolve all the possible error cases.. at least for the experience I had with the beloved 133.
  • IgorGanapolsky
    IgorGanapolsky about 7 years
    @andrea.rinaldi With error 133, the only thing you can do is retry multiple times. That is the only known solution.
  • Bryan Bryce
    Bryan Bryce about 7 years
    @ck1221 Where is the documentation that talks about the S4 only handling 6 devices? Should be 7, right?
  • prasanthMurugan
    prasanthMurugan over 6 years
    it does work for me i still get error 133 after calling close in android
  • Bubu
    Bubu over 6 years
    Take care, the connectGatt method with transport parameter requires API level 23. And you can use the constant BluetoothDevice.TRANSPORT_LE instead of hardcoded value 2.
  • vinay shetty
    vinay shetty over 3 years
    Thanks for providing such a valuable answer. It woked for me. But It will be very helpfull if you provide more documentation about this logic.