Bluetooth Device Discovery

  • I have been trying to get device discovery to work for a week or two, on Android, with no luck. I am, basically, using the example from here. The device I am trying to connect to is an HC-05 module. If I use the BT from the phone, it finds it, but my app reports nothing found. I was thinking it might be a runtime permission thing, until earlier when I was talking about my frustration with someone, and ran the app. The app found several devices, I assume other phones. Returning home, I tried to find my device, but nothing turns up, even though the phone still finds it.

    Any ideas as to were to look for a solution to my problem would be greatly appreciated. Considering that it mysteriously worked a couple hours ago, and does not now, I do not know were to begin looking for a solution; phone, code, etc.

  • Moderators

    There are several things that could help you:

    • make sure your phone supports Bluetooth LE (see for example if "nRF Connect for Mobile" app from Play Store can detect and connect to your device)
    • in your AndroidManifest, make sure you request these (coarse location is not a mistake! Modern Androids require it for Bluetooth discovery):
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    • if your phone is a Nexus device - forget it, their bluetooth module is completely unreliable when used with Qt
    • you may want to add some delay between sending any data to BT device (80-120ms), otherwise some phones start dropping packets
    • if any BLE API call fails - repeat it a few times. Sometimes it works after 2-5 tries

    In general, a word of warning - BLE is a mess on Android. And what's worse, on each platform (Win, mac, iOS, Linux) it works differently :-(

  • Qt Champions 2016

    @sierdzio said in Bluetooth Device Discovery:

    In general, a word of warning - BLE is a mess on Android. And what's worse, on each platform (Win, mac, iOS, Linux) it works differently :-(

    I'm using Qt 5.9, 5.10, 5.11 with BT LE on Android and iOS devices without problems:

    • using an Addimat Waiter Lock to LogIn
    • using a BT LE NFC Reader to get NFC functionality for iOS (to get the ID Apple hides) or Android devices without NFC Chip
    • using custom BT LE device to measure mobile network signals

    Also there's my example app "ekke's BT LE Example App" at github and also in Apple iOS AppStore and Google Play Store
    perhaps it helps you @admkrk ?

  • @sierdzio I can only assume the phone supports BLE since the device is discovered by the phone. I tried installing that app and got the same results as I do with mine; no devices found, however. The permissions are set in the manifest, and I was able to discover devices yesterday. I am unsure if it is good or bad that the other app could not find the device either.

    Unfortunately, I only have the one phone, a Samsung, to work with and the one device. I ordered a different device, but that is on a slow boat from China so I will not see it for a while. In the mean time, I will take a look at ekkescorner's code and see if I am overlooking something simple.

    Thanks for the ideas, it is hard to find a solution when I cannot figure out what the problem is.

  • Moderators

    @ekkescorner said in Bluetooth Device Discovery:

    @sierdzio said in Bluetooth Device Discovery:

    In general, a word of warning - BLE is a mess on Android. And what's worse, on each platform (Win, mac, iOS, Linux) it works differently :-(

    I'm using Qt 5.9, 5.10, 5.11 with BT LE on Android and iOS devices without problems

    Well, good for you and good to know :-) On my side, I've run into a myriad of issues:

    • bugs in my code (obviously) :-)
    • bugs in Qt (reported ofc. Some have been fixed already)
    • bugs in operating systems (esp. Windows here, but also some Android implementations)
    • bugs on specific devices (like the mentioned Nexuses, and a few others)
    • bugs on specific devices with specific system and when specific Android SDK was used

    Annoying to the point that I'd be reluctant to do any BLE-related projects in the future. I hope things improve in the future.

  • Qt Champions 2016

    @sierdzio oooh. so I was in luck that my BT LE Apps are working well at customer site ;-)

  • Moderators

    I had some serious issues (with android) with subscribing to BTL services of my target device. Turns out, on android it's a 2 step process on iOS 1 step.
    Took me way to long to get behind that issue.

    But besides that initial challenge, no problems with Qt BTLE, Its a good bit slower on android than on iOS (discovery and connect). But, I learned to accept that as part of android ;-)

  • I think I have partially solved my problem. My thorough reading of the example (cough) missed this little bit.

    Note: It is a prerequisite that the remote devices actively advertise their presence.

    I did not set up the device, so I do not know if that is actually the problem or not, but I think it is. Since the guy who did has not answered my email, I tried hard coding a simple socket. I got the address from a previous attempt that failed discovery, but did give me an address. That brought up the pairing dialog from my phone, which changed the activity on the device (LED), even if it did not seem to communicate.

    It might be slow, but at least I am making progress.

  • Now I feel really stupid. It helps greatly if you you actually handle the button clicks. It is now working with everything hard coded, so I should not have a problem making it work right.

  • Lifetime Qt Champion

    @admkrk happens to all of us ;)

    that's why it's helpful to post actual code, most often someone else is able to spot the error.

    Good luck progressing.

  • Thanks, I normally would have posted code, but I could not narrow the problem down and posting a huge wall of text would not have helped much, especially since it is pretty much the example I linked. For this last bit, I did not realize the code was an issue until I checked for errors. I did write the functions in C++, I just forgot to call them in the QML.

  • Well my solution was not acceptable. I can understand why, as the user would have to manually enter the address and uuid, I did not care for it either. I ran the desktop example, and it found the device. I made two changes and my copy of the original example is now finding the device. I do not think the first one made a difference, I started the agent without arguments. The one that made a difference was a no brainer once I reread the add device function

    void Device::addDevice(const QBluetoothDeviceInfo &info)
       //if(info.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration)
            DeviceInfo *d = new DeviceInfo(info);

    Anyway, I thought I should update this with the real solution to fixing the example I was using.

Log in to reply