This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Android direct connection takes time after first error 133

Hello,

i'm developing an interface based on nrf52. The app does a direct connection, performs some exchanges with the peripheral, and tells the peripheral to disconnect.

On Android I only use direct connection because i need to connect as fast as i can, and i make disconnection from peripheral side by sending a command to nrf52.

This works as expected until i get the first error 133. After that, the direct connection starts taking seconds (up to 6 s) before i get OnConnectionStateChanged callback. A lot of times i also get error OnConnectionStateChanged with error 133 right after connectGatt command was issued, and even if Bluetooth is off.

Performing a background connection seems to "fix it" in some way, after that i can perform direct connection again with no errors, untill i get error 133 again.

Do you have any advise on that?

Thanks

UPDATE:

By direct connection i mean connectGatt with autoConnect = 0. I believe it is something related to the Android stack because i don't see any delay in the trace sniffed.

The trace always shows the CONNECT_REQ whitout any delay after, and the whole procedure (connect / authentication / data trasfert / disconnect) always takes around 3.5 seconds.

The delay i experience is always before the actual connection is made and is always between the call to connectGatt function and the receive of onConnectionStateChanged callback, as you can see here:

08-01 14:57:13.006 D/BluetoothGatt: connect() - device: CB:89:36:71:EC:32, auto: false
08-01 14:57:13.006 D/BluetoothGatt: registerApp()
08-01 14:57:13.006 D/BluetoothGatt: registerApp() - UUID=2f611549-ccb6-4478-b3f6-b94efa047519
08-01 14:57:13.046 D/BluetoothGatt: onClientRegistered() - status=0 clientIf=10
08-01 14:57:16.046 D/BluetoothUtils: getBtEnabledContainers(): btContainers = []
08-01 14:57:16.086 D/BluetoothUtils: getBtEnabledContainers(): btContainers = []
08-01 14:57:16.096 D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=10 device=CB:89:36:71:EC:32
Parents Reply Children
  • I already checked sniffer trace, the delay is all at the Android side. From the call to connectGatt() to reception of CONNECT_REQ on the sniffer, i get seconds of delay, which is not what should be since i'm not using background connection. Direct connection should be really fast, and i see this on iOS devices which connects superfast everytime. Same behaviour i get with the nRF Connect app.

  • It's not only about time but also about frequency of advertisement and scanning parameters you use in Android API (as far as I know there are 3 or 4 levels in the latest release). What is the adv. interval of your peripheral? Is it using all 3 adv. channels on every interval or just rotates the channel and uses one at the time? And in the end how are you synchronizing the clock between sniffer and Android phone so you are sure how long it took to really issue CONNECT_REQ?

  • Peripheral advertising time is 21.875 ms for all 3 channels, while Android is scanning with SCAN_MODE_LOW_LATENCY. Scan is always in background, it's stopped before performing connection and retrieved after disconnection. I can't get Wireshark to print epoch, so i place log and sniffer close together. Because some times it takes really long to connect, it can be easily seen that the delay is before the sniffer captures the CONNECT_REQ. With Android log, by comparing the time between connect() and onClientConnectionState it can be seen that in normal condition connection takes ~400ms. When it's delayed, it takes ~3000ms

Related