I'm using an ESP32 module. I want to program the module to be a touchless garage door opener. Obviously, I want to ignore my neighbor's Bluetooth signal and only trigger on mine. Is that possible and if so, what commands do I need to use? My code works so far but for every Bluetooth signal it encounters rather than selective Bluetooth signals.
void loop() {
BLEScan *scan = BLEDevice::getScan();
scan->setActiveScan(true);
BLEScanResults results = scan->start(1);
int best = CUTOFF;
for (int i = 0; i < results.getCount(); i++) {
BLEAdvertisedDevice device = results.getDevice(i);
int rssi = device.getRSSI();
if (rssi > best) {
best = rssi;
}
}
digitalWrite(PIN, best > CUTOFF ? HIGH : LOW);
if (PIN, best > CUTOFF) {delay(2000);digitalWrite(PIN, LOW);delay(30000);}
}
Each device in the scan result can give you an RSSI (signal strength) value. Find the definition of the BLEAdvertisedDevice object class to see what other information is available from scanned BLE devices. Print out the data to see if you can recognize which is your device. If you can't recognize 'your' device from the advertised data, you may be out of luck.
Thank You johnwasser! I may have just now found an example... I think this is what you are talking about, right?
Using the example below, in order to print out the UUID, would I just need to do this?
println m_serviceDataUUID?
/**
@brief Get the service data UUID.
@return The service data UUID.
*/
BLEUUID BLEAdvertisedDevice::getServiceDataUUID() {
return m_serviceDataUUID;
} // getServiceDataUUID
/**
@brief Get the Service UUID.
@return The Service UUID of the advertised device.
*/
BLEUUID BLEAdvertisedDevice::getServiceUUID() { //TODO Remove it eventually, is no longer useful
return m_serviceUUIDs[0];
} // getServiceUUID
/**
@brief Check advertised serviced for existence required UUID
@return Return true if service is advertised
*/
bool BLEAdvertisedDevice::isAdvertisingService(BLEUUID uuid){
for (int i = 0; i < m_serviceUUIDs.size(); i++) {
if (m_serviceUUIDs[i].equals(uuid)) return true;
}
return false;
}
No, the Service UUID is the UUID of the service that you are looking at. Those are common to all devices that support that service. What may be more useful to you is the device name or MAC address.
Thank You cedarlakeinstruments for your reply. I'm just a little confused. I downloaded an iOS app called "Hidden Bluetooth" and it shows all of my Bluetooth devices near me along with the unique UUID for each one. If that app is asking for and receiving the UUID from those devices, then why can't my sketch ask for the same information and receive it? All of the Bluetooth apps show a lot of information about each Bluetooth device, but never the MAC address. For my application, I'm afraid to use the device name because it can be changed and due to security issues since it is not as unique as the UUID.
Especially on iOS, you should not trust anything that appears to be a unique identifier to always be unique. Starting a few years ago, Apple has been working to prevent their devices from being used to track people. ISTR that this including spoofing the returned BLE MAC address that was displayed to the user. i.e., for the same device, over time you may see different MAC addresses displayed.
I remember running into this problem a few years ago and I had a solution for it, but unfortunately I don't remember exactly what the solution was.
That UUID displayed may be the advertiser address, but it is allowed to be random and dynamic which means it can also change over time.
Thanks again, cedarlakeinstruments. You have been more than helpful. I confirmed that you are absolutely correct. For my application though, I'm going to use the UUID and if and/or when it changes, I'm going to simply change my sketch and re-upload it. Your help has been very much appreciated.