How do I check if a bluetooth device is connected?

I'd like to open a portal automatically when I'm near it with my car or my bike.
I use an esp32 connected in the portal remote access which check my android BLE RSSI and opening the portal when rssi is more than -80dbm, it is not already in use but tested with pieces of program and should work.
Unfortunately my home is near this portal and the rssi is sometimes stronger from a room than I'm in my car in front of it.
So I would like to check if my phone is connected with my car's or helmet's bluetooth to authorize the phone's BLE to advertise, then if I'm not with car or bike, the portal will not open.
I think it is possible to check if car Bluetooth is visible, but I'd like to check my phone is connected with it.
Thanks in advance.

1 Like

Hello Laurent

A simple search for "BLE" on this forum will find you code that checks for connection via a simple Block provided by the MIT BLE Extension for App Inventor.

Bluetooth Info on my site:

Thanks ChrisWard,

it is not what I'm looking for, but I certainly missed something in my searches.

I need to check if a specific classic bluetooth named device is connected (car audio or helmet inter-phone) with my phone and only at this condition, my phone will BLE advertise.
So the esp32 BLE client will check the BLE RSSI of my phone ID and open the portal.

I just founded AddressesAndNames and IsConnected .
By testing if visible, it seems possible to cut and test names and test if isconnected, I'm afraid it will drain battery with bluetooth searching and it will check only if classic device is visible and if classic bluetooth is connected but not if this specific device is connected.

Thanks for your site link

That is what you can check against - the device ID. I would not overly worry about running down the battery, BLE is a more conservative user of battery power. If it turns out to be an issue, you could have a power supply in your car or on your bike.

Helmet's and car's Bluetooth is not BLE.
The only way that I founded to check device is a discover witch drain battery and limit Bluetooth performances.
To be more precise, the application need to run classic Bluetooth to check connected devices, and BLE to communicate with ESP32.
So, is it possible to check connected devices properties on classic Bluetooth ?

But they still have an address? They can be found by scanning? If that wasn't true, you could find several devices and not know which to connect to.

This is a basic template project for Classic BT. It's for Users working with microcontrollers, most often an Arduino. You can try it to see if your BT devices are actually found.

BT_Basic_Setup_Receive.aia (8.4 KB)

I'm already OK for communicate between esp32 and android.
But I need to check if my phone is bluetooth connected with my car and it is here where I need help.

I know that laurent.

To know if you are connected to your car's BT, you need an ID from the car - that's why I asked you to try the template App - to see if your car BT can be found by it, as I described in my last post. When I use it to find my Arduino, it finds other BT devices in the house too, inc laptops, tablets and phones, but it does not find a BT wireless mouse nor a BT wireless headphone. If it can't find the device, it can't connect to it.

My car is visible with device scanning, I can have ID and name, I do not need to connect it, the phone does it automatically when it see it.

therefore my goal was avoiding scanning.
First it drains battery and limit bluetooth performances
Second it just tells that the device is near but not that I'm connected with.

This is for that I asked if it is possible to use classic bluetooth connected device properties, I cannot find any references about this.

Sorry for my English, I'm not able to explain as I'd like.

Hi laurent.

If you just leave the connection to the phone's system, it will simply connect to any BT signal available. You can give the App specific IDs to connect with, but if you don't scan for devices you do not know whether or not they are in range or even available. Less of an issue if all devices are BLE but in your scenario, problem.

If this set up is only for personal use (you are not going commercial with it), then corners can be cut, but you can't get away from the fact that a device has to be found before it can be connected to.

What about approaching your design in a different way. The esp32 has a constant power supply? If so, that can scan 24/7, right? That would make the need to use the car or bike's BT redundant. Just have a connection between the phone (App) and the esp32. If you approach the portal in your car, it will open if the esp32 connects with your Phone App.

Edit: By the way, any Bluetooth solution to automation in this sort of scenario will drain the battery. The only way to avoid that is to switch the device BT on when nearing the portal, or do not use Bluetooth.

Hi Christopher,

The esp32 as a constant power and scan all the time (in BLE), if it is detecting the phone that advertising, then no need to connect, it just measure rssi and if it is more than -80dBm, it's open the portal.
The battery drain is very small with this approach.

But, (there is a but) if I'm near the portal with my phone (in my home or in the garden), it will open too :frowning:

So I'd like to check if I'm in my car, and my idea is to simply check if my phone is connected with my car's Bluetooth system, and only at this condition the phone will BLE advertise.

As you can see, there is not need to connect at anytime, just to check if the phone is connected (easy) with my car (that's I can't do).

It is just for fun and personal use, and if not possible, the portal remote control does the job (but not easy with motorbike when coming back from a ride), i

Would you have your Phone Bluetooth switched on then? I wouldn't as there would be no purpose.

Yes, I have heard of a few mishaps with motorbikes and automatic doors.

I'm going to assign your topic to a fellow MIT Power User, to give a different perspective on the problem.

Yes, it is always on, for headset, I love forgot bluetooth state and have it ready when I need it.
And if I do all things manually, no need anymore for programming :wink:

Thanks a lot.

Any ideas:
App Inventor does not work with Services, if you want to check if your mobile is connected to the car, you will have to open an application.
You can check if it is connected to the car by typing the car's Bluetooth Mac.

Here you have some information about advertisement with BLE:

The ESP32 can work with classic Bluetooth and with BLE, I have not tested if it can work with both at the same time.

Another idea would be to use Radio Frequency devices to get more range and open from further away.

Thanks for your answer.

Why kind of application ?

The application made with App Inventor to check if there is a Bluetooth connection with the car.

OK, I think that tasker can do the job, and call the BLE advertiser if the car is connected.

Thanks for your help and your time.