Bluetooth listpicker with active instead of paired devices

Dear experts
I have been using my app for a while controlling several ESP32 devices using Bluetooth classic.

When I switch between different devices (selection on listpicker) the project (see below) lists all paired bluetooth devices. However it would be much more user-friendly if only the active devices could be listed instead of all paired bluetooth devices (like e.g. my paired headset which has no link to this project and is switched off most of the time but it appears on the list anyway).

Is there a way I could list only active devices (means powered and BT in the air) instead of all paired bluetooth devices on my mobile?

Thank you for your help.

Check this one :


For that, you need an extension that can take the list of available devices.
Take a look at these extensions :

App Inventor Extensions: Bluetooth | Pura Vida Apps

BT: An extension to work with Bluetooth - #6 by vknow360

Extension : Send message/documents via Bluetooth (FAST) || OPEN SOURCE

1 Like

Thanks for your hints and tips. I have tried out now some of these extensions. As far as I understand these extensions, there is no such straight forward feature I kept in mind. I assume the only way to do that is to build a scan function for available devices myself (using one of these extensions), right?

1 Like

thats right, so has your problem been resolved?
if your problem has been resolved please close this topic by pressing the Solution button. Thank you :wink:

Since you are using ESP32, you already know the names of the Bluetooth devices.

So simply add the names of each one to the ListPicker.
Just change each ESP32Name1 and 2 to your names, add as many more as you need.

That way the user only sees the devices that are relevant.

Like This:

Mike.

1 Like

Dear Mike

Thank you very much. This looks very promissing. I will try and adapt my project as suggested within the next few days and will report here.

Remo

You’re welcome. I think it should work well for your scenario.

Mike.

Dear Mike

I have reviewed my project and (re-)defined or extended some features or functionality:

  1. In a first step I would like to register the/all ESP32's to an App internal database where I can rename, categorize, administer the ESP32 and also add some parameters. So at this point I need to see all paired Bluetooth devices in order to register/set-up the device in the database. I will therefore create a Setup Page/Menu to do so.

  2. However once the ESP32 is defined in the database and the setup menu has been quit I would like to call all ESP's the way you mentioned above.

To conclude: I will now try first to implement the database described and only then adapt your code to my project for normal usage within the app. I don't really know how to create this database yet but this would be off-topic to discuss here anyway.

Will Keep you updated.

Remo

Cervino,

I’m not sure how many ESP32’s your project is using or what the end goal is.

But maybe you should look into using the ESP-NOW protocol. With this protocol, it is very easy to exchange data between all connected ESP32’s. It is fully automatic and will detect instantly when an ESP comes/goes on/off line.

For example, I use ESP-NOW in my Aquarium controller app, and I need only to connect to the Main controller using Bluetooth Classic.

After that any ESP32’s the go on/off line are automatically detected by the First connected ESP, which then can send data back and forth to any or all other ESP32’s. The Main controller then can send any updated information back to the App, for user interaction and display.

Here is a link that I used to get started using the ESP-NOW protocol:
https://randomnerdtutorials.com/esp-now-esp32-arduino-ide/

Just a side note:
The demos show how to get the MAC Address of each of the ESP32’s. You can avoid doing this and make it automatic by simply adding a few lines in your code to set the device to your own unique Mac Address on startup. Then in you know what address each board will always have.

You can do it like this:
#include <esp_now.h>
#include <WiFi.h>
#include <esp_wifi.h>

uint8_t Main_Adr[] = {0x20, 0xAE, 0xA4, 0x20, 0x0D, 0x10};
uint8_t Device1_Adr[] = {0x20, 0xAE, 0xA4, 0x20, 0x0D, 0x11};
uint8_t Device2_Adr[] = {0x20, 0xAE, 0xA4, 0x20, 0x0D, 0x12};
uint8_t Device3_Adr[] = {0x20, 0xAE, 0xA4, 0x20, 0x0D, 0x13};

Then in your setup:
If this is the Main Controller set it to the Main_Adr[]

Like this:
void setup() {
Serial.begin(115200);
btSerial.begin(9600);
WiFi.mode(WIFI_STA);

// Set this address as the Main Controller
esp_wifi_set_mac(ESP_IF_WIFI_STA, &Main_Adr[0]);
if (esp_now_init() != 0) {ESP.restart();}

// Setup peers for sending data to
esp_now_peer_info_t peerInfo;
peerInfo.channel = 0;
peerInfo.encrypt = false;

// Add the peers
memcpy(peerInfo.peer_addr, Device1_Adr, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {ESP.restart();}

memcpy(peerInfo.peer_addr, Device2_Adr, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {ESP.restart();}

memcpy(peerInfo.peer_addr, Device3_Adr, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {ESP.restart();}

}

And for your other ESP32’s your Device1, 2 and 3
You would us the same code above, only set THIS ADDRESS to which ever device it is:

// Set this address as the Device1
esp_wifi_set_mac(ESP_IF_WIFI_STA, & Device1_Adr[0]);
if (esp_now_init() != 0) {ESP.restart();}

Then add as many peers, as you need for sending data to.

To send data back to the Main controller add it as a peer:
memcpy(peerInfo.peer_addr, Main_Adr _Adr, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {ESP.restart();}

To send data to Device2, then add it and so on.
memcpy(peerInfo.peer_addr, Device2_Adr, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {ESP.restart();}

Hope that helps,
Mike.

1 Like

Dear Mike

Once again thank you very much for your time and your comprehensive help. For me – still a beginner with ESP32 and MIT – my hobby project for model railway trains is quite a challenge for me.

In fact my ESP-Now project seems quite similar to yours however I used the “One-to-many” approach in my project:

On the ESP32 side it works quite to my satisfaction so far however I just started the app implementation.

One of the difficult parts is that I haven’t assigned a particular sender, instead I would like to select/change the sender in the android app. In other words a receiver should be reconfigurable to the sender or receiver and vice versa. It is even possible that the previously active sender isn’t powered so a receiver needs to be reconfigured as the sender. I plan to use up to 20 ESP’s which is as far as I remember the limitation of ESP-Now. But let me explain my idea in more detail:

Set-up process in the MIT App

First I would like to setup and group a paired ESP32 in my app, e.g. in group A I have 4 ESP’s, in group B 3 ESP’s and in group C for example 5 ESP’s. The main key in the database or called tag will be the ESP32 Mac adress. Not all ESP’s are necessary powered/available at the same time. But in any case one needs to be assigned as the sender ESP and is connected with the app using Bluetooth classic. This selection can either be done manually in the app (or maybe even better auto selected when available). As mentioned in my previous message I would like to add some parameters and information to the database of ESP’s in the APP. This editable pieces of information will then be sent to the ESP’s within the ESP-Now network.

Normal usage of app/system

In use I am sending a string of commands to the sender ESP (via Bluetooth classic) which is then relayed by the sender using ESP-Now to all MAC addresses listed in the groups (A, B, C,...) as specified in the set-up above. So all devices in group A will receive the same command string and all ESP32’s in group B will get another command if fired in the app.

Change of group

In my app it should also be possible to switch/re-group one or several ESP’s from one group to another e.g. one ESP is moved from group A to group C and e.g. two ESP’s from group C to group A.

I hope that explains a little of what I am trying to achieve. I am aware that there might be more efficient concepts to achieve my goals but that is for now what I consider realistic for me to implement.

Kind regards,

Remo

Remo,

It looks like you have a pretty good idea of what you are trying to accomplish. With some trial and error testing I am sure you will come up with a working model.

Good luck with your project, and keep us updated on your progress.

Mike.