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

Using Android BLE library, getting into a connecting -> disconnecting (timeout) situation. nRF app seems to help.

Hi,

We're using the Android BLE library from Nordic. It really helped us handle a lot of the things we didn't want to think about handling on the bluetooth stack. (Thank you)

We've been experiencing some weird issues where the sensors will get into a connecting -> disconnecting issues, effectively timing out on connect.

The flow is : 

  • we scan for devices using the compact scanner (nordic)
  • we try to connect to one or more devices
  • the connection attempt would time out

The interesting thing we've found is that if we use nRF connect application (Android) on the same device (phone), and try to connect to the device it works. And surprisingly, right after connecting and disconnecting using nRF connect app, the devices will connect to the Android app (also using BLE library from Nordic) successfully as well. Are we missing to do anything properly. What are the steps on nRF connect app on connect and disconnect? We are thinking it might be that we got the driver or the sensor into a weird state and nRF connect app connection fixes it somehow?

Running out of ideas because we're tried many different things such as putting delays in possible places. Thanks!

Parents
  • Hi,

    Logs would be great for establishing what happens.

    nRF Connect does not use the Android BLE Library. The main difference is probably that in nRF Connect for Android, the cache is cleared on disconnection. You can get that same behavior in the BLE Library by changing shouldClearCacheWhenDisconnected() to return true instead of false. The issue may have to do with caching, and if so then that might be a solution.

    How do you recover from the error state? Do you use bonding? Pairing?

    Regards,
    Terje

  • This issue still happens even when I override `shouldClearCacheWhenDisconnected()` to return true. 

    As I've mentioned before, it seems like I we just launch nRF connect (Android) app in the same android device - this timeout issue will go away. The important distinction is that we are not doing anything on the nRF app, we just launch it (no connect/disconnects).

    Any thoughts as to why? If nRF connect code was open source, I would look at what it does and test those out but seems like it's not. Slight smile

    Thanks!

  • Hi,

    As you are getting "Cancelling connection..." in the log, that means it may be a library issue. It is printed in "internalDisconnect()" method which may be called in 3 cases:

    1. If your device does not support required services,

    2. If there was an error during connection,

    3. If a connection timeout occurred.

    I think, it 1. and 2. you would get something more in the log, which would indicate that you are setting a very short timeout for connection request, possibly in seconds instead of milliseconds.

    Could you put a breakpoint in "internalDisconnect()" method, where it does print this message, and check the stack trace to verify from where you end up there?

    Also, check if you are not setting too small timeout in "enqueue" method.

  • Thanks. The timeout is 10 seconds. It is indeed coming from the timeout, as I've mentioned in my previous messages. What is very puzzling to me is that by launching the nRF connect app all of these issues will start to flow perfectly. 

    Scenario : I have a few peripherals (all running the same firmware) advertising. Nordic's compact scanner found it on my app. Then we try to connect to the peripherals. They all get stuck in a timeout loop (times out -> next time I find out it's not connected - I try again in the app). I was using the nRF connect app to see if the peripherals were having advertising or other issues. I would launch the nRF connect app and they will all start connecting successfully. If someone could tell me what other things I maybe missing that nRF connect is doing i'd be very grateful. (Note that I didn't try to manually connect using nRF app).

    Thanks!

  • nRF Connect doesn't do any magic. I guess it just scanned for the devices, and for some reason it helps. To be sure you may check other scanner app, for example nRF Toolbox -> UART profile (which scans without a filter), scan for few seconds and check your app. I have no idea why that would be helpful.

    Coming back to the timeouts, I didn't try the library with multiple devices connecting at the same time. I don't think they could impact one another on library level, as managers are separate, but perhaps Android has an issue, where allows only single connection at a time? But if the timeout in a library gets called, that indicates an issue in BLE Library. Could you debug why the timeout is being fired before 10 seconds?

Reply
  • nRF Connect doesn't do any magic. I guess it just scanned for the devices, and for some reason it helps. To be sure you may check other scanner app, for example nRF Toolbox -> UART profile (which scans without a filter), scan for few seconds and check your app. I have no idea why that would be helpful.

    Coming back to the timeouts, I didn't try the library with multiple devices connecting at the same time. I don't think they could impact one another on library level, as managers are separate, but perhaps Android has an issue, where allows only single connection at a time? But if the timeout in a library gets called, that indicates an issue in BLE Library. Could you debug why the timeout is being fired before 10 seconds?

Children
  • Thanks. I'll test using nRF toolbox UART profile as well. But that's not necessarily a long-term solution. It's a bit weird that a third party would help alleviate issues in the app I'm building Slight smile. My goal is to figure out what's those third party app are doing. If UART profile worked, I'll try to dissect the code to find out what the difference is.

    In terms of timeouts, the timeout is firing on 10 seconds (see logs above). The library has been working for me with multiple bluetooth connections since I started using it a few months ago.

    The only significant changes I've made recently when we started noticing the timeout issues is when we added the Compat Scanner library (https://github.com/NordicSemiconductor/Android-Scanner-Compat-Library) and some performance improvements between scanning and connection attempts. 

    In the mean time, I'm going to try to revert the changes I made with compat library (effectively removing it) to see if that might have introduced the issue.

    Thanks!

  • I'm able to use nRF toolbox UART profile and it behaved just like how nRF connect. And I found the fix to my issue because toolbox is open-source Slight smile

    I was using the compact scanner library from Nordic as well. The difference for me was that I had :

    .setUseHardwareBatchingIfSupported(true)

    So after I set it to false (as is what toolbox seems to do as well) it fixed my issue. I wonder why this would cause this timeout issue. Can you help me understand it? The device I was testing on is a Nexus 5X.

    But all in all, I'm happy it solved my issue. Thank you guys!

Related