esp32-snippets
esp32-snippets copied to clipboard
BLEScan can't find all Bluetooth devices around me
Hi there,
I'm using Arduino IDE do code my ESP32 and I'm having problems in identifying all the Bluetooth devices around me, it seems that ESP32 is not able to find, for example, my phone.
Note: For context, I'm trying to scan Bluetooth devices to save the Mac Addresses from the 3 devices with the strongest signal in order to send those Mac Addresses through an http request to verify if they're authorized devices to connect or not.
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
const int buttonPin = 36; // the number of the pushbutton pin
//const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int scanTime = 20;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.printf("*** Advertised Devices ***\n");
Serial.printf("Name: %s // Address: %s // RSSI: %d \n\n", advertisedDevice.getName(),
advertisedDevice.getAddress().toString().c_str(), advertisedDevice.getRSSI());
}
};
void setup() {
pinMode(buttonPin, INPUT);
Serial.begin(115200);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
//Serial.println(buttonState);
if (buttonState == HIGH) {
Serial.begin(115200);
Serial.println("Scanning...");
BLEDevice::init("");
BLEScan* pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
BLEScanResults foundDevices = pBLEScan->start(scanTime);
Serial.print("Devices found: ");
Serial.println(foundDevices.getCount());
Serial.println("Scan done! \n");
}
}
Only to play it safe: You are sure your device is advertising itself and isn't "just" a BT-Classic device? Are you able to scan and find it with another device via BLE?
@Donderda thank you for answering. Yes, I think it+s advertising. As I read here and in other places, it seems that ESP32 can't scan classic Bluetooth devices, scanning only the BLE devices, is that right? Is there any way to go around that? I'm trying to catch smartphones mac addresses in order to check if the user is authorized to enter the building by unlocking the door.
Thanks in advance
Sorry, I'm not specialised in BT-Classic stuff :(. I'm also not sure whether it's a good idea to fetch your smartphone's BT MAC address for unlocking doors :p. AFAIK e.g. Apple changes the MAC of the BT(LE?) Controller...
But maybe someone else knows more about this topic...
Thank you :)
There may be many reasons for it. For debugging, I suggest you to use NRFConnrct, LightBlue or BlueCap apps to scan and cross check with your ESP32's results. Addition: Even though you are sure there is a working BLE device around, they might be using directed advertising or disabled their discovery. From my smartphone or ESP32, I can not discover some BLE peripheral devices like watches or heart rate monitor for example since those are personal devices.
BLE-scan can will only detect BLE-devices (Bluetooth Low Energy). Your phone must be configured as a beacon to be detectable with BLE-functions. There are apps which will let your phone simulate a beacon but the more elegant way to achieve your goal is using the classic Bluetooth serial libraries and start from there.
BLE-scan can will only detect BLE-devices (Bluetooth Low Energy). Your phone must be configured as a beacon to be detectable with BLE-functions. There are apps which will let your phone simulate a beacon but the more elegant way to achieve your goal is using the classic Bluetooth serial libraries and start from there.
To prevent any confusion, do you mean iBeacon? Phones do not necessarily have to simulate beacons but can simulate regular Bluetooth peripherals.
Nope, not an iBeacon. Any phone with a bluetooth chip 4.0 or higher should be capable of simulating Bluetooth Low Energy beacons. I am using Beacon simulator. You can simulate a beacon and in the settings make it "resilient", so it stays on even if you restart the phone
Its not beacon simulating. Since android 5.0 every android device with ble 4.0 chip should be capable of advertising like any any other peripheral devices can do. Best start is with nRF connect.
Its not beacon simulating. Since android 5.0 every android device with ble 4.0 chip should be capable of advertising like any any other peripheral devices can do. Best start is with nRF connect.
You are right in saying that they are capable of advertising. They just don´t do it. And that is why a Beacon simulator is a useful tool. I was not capable of reading out the MAC address of my phone with any BLE-lib based sketch. You would need a classic bluetooth library but the only one which I have seen so far is BluetoothSerial.h and that is not good for anything, really. See https://www.esp32.com/viewtopic.php?f=19&t=7728&p=34802&hilit=bluetooth#p34802 (does not suppoort values like signal strength, MAC address, etc)
If you have any better ideas to scan and recognize the MAC of an android phone with the ESP32, I would be interested to learn how
That's not true. I can read my Samsung s4 and s9+ with this exactly library every single time. Like I said, use nRF connect as starting point.
Interesting. My OnePlus 3t is not being recognized. Now I do have nRF connect installed but would it find my own phone? Improbable.
No, you can't scan for your own advertising.
Hello All,
I'm facing the same issue over here. I'd like to build simple presence sensor based on BT, but my Honor 9 (neither iPhone of my gf) aren't recognized.
As far as I understand it correctly it is caused by the fact the smartphones aren't advertising? Or Am I missing smth? (I'm using BTE_scan example without any modification).
Is there any way how to solve it?
PS: my FitBit band is recognized everytime without any issues.
Thx.
Y
Hello All,
I'm facing the same issue over here. I'd like to build simple presence sensor based on BT, but my Honor 9 (neither iPhone of my gf) aren't recognized.
As far as I understand it correctly it is caused by the fact the smartphones aren't advertising? Or Am I missing smth? (I'm using BTE_scan example without any modification).
Is there any way how to solve it?
PS: my FitBit band is recognized everytime without any issues.
Thx.
Y
Smartphones are not specialized Bluetooth consumer products. Therefore, they would not advertise in BLE even though you enabled Bluetooth via settings. You might want to use various BLE applications that mimic different BLE profiles. For example, LightBlue app for IOS can create virtual peripherals(such as Heart Rate) that would probably allow you to scan with ESP-32.
@MehmetCagriK
thank you for your swift response. You're right, indeed with nRF Connect app I'm able to create advertisment profile and then BLE_scan basic example is able to find my phone. Unfortunately that isn't much confortable wasy as it only works while App is running, and so doesn't really met the expectation.
Do I understand it correctly, the problem here is the ESP32 is looking for BLE devices but smartphones are in default not set to behave like that? Is there any option to scan for "Normal" Bluetooth devices?
As far as I remember correctly I was able to detect my smartphone without any issue with python and pybluez lib, but I suppose is different kind of story.
Y
Yes, smartphones does not automatically advertise in BLE since they are not BLE product like a HeartRateMonitors, so it would not make sense that a smartphone advertise in BLE.
According to the datasheet of ESP-32, it does only have BLE Radio(hardware) and is not hybrid hardware like phones have. Without classic Bluetooth Radio, you can not sense Bluetooth devices.
Your best bet is using your phone and ordering another ESP-32, they are really cheap(I have 5 of them from Banggood) and use spare ones as peripherals.
Your best bet is using your phone and ordering another ESP-32, they are really cheap(I have 5 of them from Banggood) and use spare ones as peripherals.
Did you mean just use spare ESP-32 as some kind of "BLE key fob"? Or maybe there is some?
Y
You can use second ESP-32 as advertiser device, like NRF Connect allows you. The second one would be always working, so better option than trying to use phone if you need a constantly working BLE advertiser. I assume you need a device that is always on so you can easily develop your software.
Your assumption is right, but I didn't get it. Do you mean to develop small moveble ESP-32 with battery? Wouldn't be easy to use smth like: https://www.global-tag.com/portfolio/keyfob-beacony-ble-keychain/ ?
You can buy it but with another ESP-32 you would be more flexible, since you can not program BLE keychain. But if you only want to develop Presence Sensor, BLE keychain would work jsut fine.
That's exactly my point. I'm looking for presense sensor and therefore some cheep BLE keychain (can buy for 1-2$) will be enough.
Most probably I'll go this way. Anyway thanks a for explanation about how BLE works on ESP-32.
Much appreciated.
Y
You are welcome.
It seems there is great interest in scanning for classic bluetooth devices. But I still haven´t found out on how to get it work. My idea is to unlock a door as long as my phone is in range without any need to constantly run a beacon on the phone
Why would you use classic bluetooth devices? Should not low energy option be great for that kind of operation, by the name implying lower energy consumption? And your phone must be always advertising IF you want it to be automatically recognized by the door. Otherwise, you have to manually use your phone to activate Bluetooth advertisement.
Mehmet, some people have classic Bluetooth activated all the time as they might use their phone constantly in the car or so. Lower energy consumption is not my concern. Phone gets recharged anyhow at night.
What do you mean with "the phone must be always advertising IF"?
I do not know specifics of Classic Bluetooth honestly. Per my research, car bluetooth connects to your smartphone, so it must be periodically scanning bluetooth devices after car power is on. But ESP-32 is not the device that supports Classic Bluetooth protocol, if that is your purpose.
Mehmet, the ESP32 actually does support Classic Bluetooth and there is a library. I have managed to compile a test-sketch on the IDF but my programming skills are rudimentary and working with the espressif IDF is not easy for me.
Anyhow, I do appreciate your effort here. Thanks for your clarifications!
Hello Is there a limitations on how many beacons esp32 can scan.i have some 100beacons. Esp32 crashes when all are made on
Probably you are run out of heap. This is very good example to study the case: https://github.com/tobozo/ESP32-BLECollector
@tobozo did great job