question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

establishConnection fails sometimes with status 133 GATT_ERROR

See original GitHub issue

Summary

I get the following exception when calling establishConnection(false): status 133 GATT_ERROR about 1/4 times on my Android 8.0. (strange that it is a DisconnectedException while trying to connect …)

I tried to get around with a retry after such connection failure. But this doesn’t work: the next exception tells me that the device is already connected. It seems that rxandroidble got into an inconsistent state?

If I try again the code block always succeeds (the code is triggered by a button click)

Library version

1.8.0

Preconditions

Sometimes it happens when the application is just restarted (from the debugger).

Steps to reproduce actual result

Minimum code snippet reproducing the issue

return device.establishConnection(false)
    .retry(2, { throwable ->
        Thread.sleep(500). # retry with some patience
        warn { "establishConnection() failed: " + throwable.message}
        # retry if the connection fails
        throwable is BleDisconnectedException &&
                throwable.message?.contains("status 133 (GATT_ERROR)") ?: false
    }).flatMap { rxBleConnection ->
 ...

Logs from the application running with setting RxBleLog.setLogLevel(RxBleLog.VERBOSE)

D/RxBle#ClientOperationQueue: QUEUED   ConnectOperation(266688704)
D/RxBle#ClientOperationQueue: STARTED  ConnectOperation(266688704)
V/RxBle#BleConnectionCompat: Connecting without reflection
D/BluetoothGatt: connect() - device: F5:14:59:CB:18:99, auto: false
    registerApp()
D/BluetoothGatt: registerApp() - UUID=df67839d-5d9d-43fc-b086-3f62899251d6
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=7
D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=7 device=F5:14:59:CB:18:99
D/RxBle#BluetoothGatt$1: onConnectionStateChange newState=0 status=133
I/RxBle#ConnectionOperationQueue: Connection operations queue to be terminated (F5:14:59:CB:18:99)
D/RxBle#Executors$RunnableAdapter: Terminated.
W/Ble: establishConnection() failed: Disconnected from F5:14:59:CB:18:99 with status 133 (GATT_ERROR)
W/Ble: establishConnection() failed: Already connected to device with MAC address F5:14:59:CB:18:99
W/MainActivity: ### FM SetupNotification failed: Already connected to device with MAC address F5:14:59:CB:18:99, cause: null
D/RxBle#ClientOperationQueue: QUEUED   DisconnectOperation(83660552)
D/RxBle#ClientOperationQueue: FINISHED ConnectOperation(266688704) in 5694 ms
D/RxBle#ClientOperationQueue: STARTED  DisconnectOperation(83660552)
D/BluetoothManager: getConnectionState()
    getConnectedDevices
D/BluetoothGatt: close()
    unregisterApp() - mClientIf=7
D/RxBle#ClientOperationQueue: FINISHED DisconnectOperation(83660552) in 20 ms

Actual result

Expected result

Would it be possible for the ‘device’ instance to not think it is connected when retrying the connection? I have a feeling that the retry might then succeed …

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
aptly-iocommented, Jan 20, 2019

@dariuszseweryn Based on your suggestion I tried .retryWhen with a delay mechanism. The log traces indicate that the timer is executed but the actual retry never happens. When commenting out the delays, the retry happens! Strange but Luckily, after much trying, the delay seems unnecessary.

I tried these:

                    .retryWhen({
                        val counter = AtomicInteger()
                        warn { "### retrying ${counter}" }
                        it.takeWhile { error ->
                                warn { "### takeWhile error: ${error}" }
                                counter.getAndIncrement() != 3
                            }
                        .flatMap { error ->
                            warn {"### delay retry by " + counter.get() + " second(s) for error: ${error}" }
                            Observable.timer(counter.get().toLong(), TimeUnit.SECONDS)
                        }
                    })

and this variant

                    .retryWhen({
                        val counter = AtomicInteger()
                        warn { "### retrying ${counter}" }
                        it
                            .takeWhile { error ->
                                warn { "### takeWhile error: ${error}" }
                                counter.getAndIncrement() != 3
                            }
                        .doOnNext { debug { "### before delay on thread ${Thread.currentThread().getName()}" } }
                        .delay(1, TimeUnit.SECONDS)
                        .doOnNext { debug { "### after delay on thread ${Thread.currentThread().getName()}" } }
                    })

I see status 133 occurs frequently but I’ve never seen in fail again after a first retry. I finally stick with this simple:

                    .retry(4, { throwable ->
                        warn { "### within doFinally establishConnection() failed: " + throwable.message}
                        true    // force a retry
                    })

Hope it will help others.

0reactions
TyFancaoHuynhcommented, May 16, 2019

@aptly-io Hi aptly-io, I have same issues. I try to connect ble device and it have error 133 GATT code. Have you fix it done? Lets show me code to connect ble with RxAndroidBle. Thank you so much 😄 .

Read more comments on GitHub >

github_iconTop Results From Across the Web

GATT error with 133 on onConnectionStateChange #18 - GitHub
I managed to fix my "status 133" error. In my case, it was happening in case when I was trying to connect to...
Read more >
Android 10: GATT error with 133 - how fix? - Stack Overflow
As seen in the AOSP constants, this error is the most generic error you can get. Generally, it happens when the device is...
Read more >
Android GATT 133 error - Nordic Q&A
Connection : Andoird phone hardly connect my device. It returns GATT error 133. After application try more(3~4 times more), it connects. 2.
Read more >
Connection establishment fails with GATT ERROR 133 ...
When I try connecting my device with nRF mobile app, it disconnects immediately throwing Error 133 - GATT ERROR.
Read more >
BLUETOOTH LE (LOW ENERGY) CONNECTION MANAGEMENT ...
9 CHALLENGES TO ESTABLISH CONNECTION. 9.1 Failed to discover services. 9.2 Gatt Error. 9.3 Failed to discover Proprietary service or service is null....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found