How can I check if a Bluetooth device is still paired?

I'm trying to continuously check if my HC-05 Bluetooth module is still paired with my phone or not after initially connecting, so that I can display "Connected." or "Not connected." in a Label. I don't know how to get two things: Bluetooth address and device name. I've provided the code blocks below, but the mock up app is probably better to look at.


CheckBTPaired.aia (2.3 KB)

What do you get displayed in the listpicker? Isn't ir a list of mac addresses and device names?

Taifun

Yeah, so should I just hardcode the address into the code? Also, I tried BluetoothClient.isConnected, and it's always True even after turning off my HC-05 module. My phone is Android 11 (API level 30 I think?) and when I hover over the isConnected block it warns that phones with an API level under 14 won't give accurate output, but it should work fine for me, it doesn't.

Oh also, I've seen some people on these forums get the index is the AddressesAndNames list (something roughly like that) and, well for me there's only one index, and it's a boolean.

If I hardcoded the name and address then it would only work for one HC-05 module. Is there a way to collect the address and name from the selected bluetooth device after choosing it in the listpicker?

I tried printing listpicker.selection to a label and it gives me true even though it says in the docs that it should output a string:


What is it supposed to return? The address and name of the selected bluetooth device right?

EDIT:
This guy here had the exact same problem I'm having. You started helping him and then I don't know what happened afterwards. No one has helped him since.

EDIT 2:
Another guy asked "how to save bluetooth mac address", unanswered. See the last reply.

this


returns true or false, in your case true
then you assign that value to Listpicler.Selection ant therefore Listpicker.Selection is true in the end

usually you use an if statement like this

if BluetoothClient.Connect...
then "connected"
else "not connected"

the question is, what do you get back from Listpicker.Selection without assigning another value?
And this then will be the selected combination of mac address and device name...

just split Listpicker.Selection by space (you can find that block in the text drawer) to get a list
now select the first item from that list to get the mac address and the second item to get the device name

I now added a link to my answer to both threads...

Taifun

1 Like

Alright, so I tried this, and I still get true for index 1, and there is no second index.

This is really all I want:
After connecting to a Bluetooth device, I want to be able to always know if that device has disconnected or not. If the Bluetooth device is powered off after being paired with the app, the app should be able to see that.

With this ability, I can just create a label to tell me if the device is currently connected, or not connected based on the output of whatever logic or function is used.

It would really help if you provided a screenshot of your relevant blocks, so we can see what you are trying to do, and where the problem may be.

To get an image of your blocks, right click in the Blocks Editor and select "Download Blocks as Image". You might want to use an image editor to crop etc. if required. Then post it here in the community.

You have to constantly send (i.e. poll) something to the device. As soon as you get error 516 you know, that the device disconnected

EDIT: see an example here

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by icon24 Taifun.

It works. I apologize I said it didn't, must've done something wrong. I will now try to use the isDevicePaired block now that I can get the MAC address.


With this setup, it only says "not connected" when I turn Bluetooth off. I think I might be misunderstanding how the IsDevicePaired block works.

See the documentation Connectivity

To find out, if the device is still connected, you have to

Taifun

Hey Taifun! I solved it.

Please share this with the other Topics I mentioned that were unanswered.

The code works. Every 500 milliseconds the clock fires, checking if (call BluetoothClient1.Connect) returns True or not. When connected, the label is set to "connected". If I turn off the HC-05 Bluetooth module, (call BluetoothClient1.Connect) returns False, thus setting the label to "not connected".

Sorry, I did not realize, that you changed the IsDevicePaired method by Connect

Yes this works, but I do not know, if it is recommended to repeatedly connect again while already connected...
What do others think?

Taifun

Never tried it but, well, does not sound ideal - it could interrupt data transfer.

Usually what we want to know is whether or not data sent was received. To that end, the microcontroller can send back a confirmation (or vice versa). Therefore, we know connection is true unless confirmation is not received.

The BLE extension provides an auto re-connection.

Update, I do not recommend it. Crashes your app.

1 Like

Setup a Poling Block that you can switch off if the App is going to send actual data.

When poling, send a single character from a List - therefore sending a different character each time. Have the microcontroller send back what it received. If it does not send back what it received, the connection is lost - then if you wish you can alert the User or simply try to make the connection again.

When a connection is established, there are only a few reasons why it could get lost:

  1. One of the devices became out of range
  2. One of the devices closed down (no power?)
  3. One of the devices failed (damage, loose wire, overheating)
  4. An object moved between the devices and broke the signal
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.