how to scan a specific BLE device using MAC address!!!

hi can anyone help me with this

How to select a specific BLE device using mac address in ESP32 board?

have you looked at the BLE_scan exemple that is available directly from the IDE (under ESP32 BLE Arduino) and the other client or server examples (BLE_client.ino would connect to a service) ?

1 Like

Hi
Thanks for the reply
I got the BLE scan and BLE server source code
And my scan output is (here I shared three scans with 2sec delay)

Name: MAC Address: 3a:b2:5b:e5:6a:17 ,-69 , Name: SIBIMAC Address: 24:6f:28:1a:df:5e ,-60 , Name: MAC Address: 08:2d:3d:79:1c:9d ,-92 , Name: MAC Address: 64:26:b3:ae:09:6d ,-17 , Name: MAC Address: 79:8b:43:98:66:19 ,-24 , Name: MAC Address: 5c:a4:4b:5d:9c:44 ,-79 ,Devices found: 6

Name: SIBIMAC Address: 24:6f:28:1a:df:5e ,-56 , Name: MAC Address: 64:26:b3:ae:09:6d ,-17 , Name: MAC Address: 79:8b:43:98:66:19 ,-25 , Name: MAC Address: 3a:b2:5b:e5:6a:17 ,-68 , Name: MAC Address: 08:2d:3d:79:1c:9d ,-82 , Name: MAC Address: 5c:a4:4b:5d:9c:44 ,-79 ,Devices found: 6

Name: SIBIMAC Address: 24:6f:28:1a:df:5e ,-53 , Name: MAC Address: 3a:b2:5b:e5:6a:17 ,-72 , Name: MAC Address: 79:8b:43:98:66:19 ,-26 , Name: MAC Address: 5c:a4:4b:5d:9c:44 ,-75 , Name: MAC Address: 08:2d:3d:79:1c:9d ,-80 , Name: MAC Address: 64:26:b3:ae:09:6d ,-17 , Name: MAC Address: 4b:26:70:2b:0a:a2 ,-90 ,Devices found: 7

In the above scans I’m getting many BLE devices near me
But I need only the specific BLE device (highlighted above) to be shown in serial monitor
I got the service uuid ;manufacture id; of my specific device
Can I use if statement some where in the code ?

So the question is what are the changes need to be done in code to get only the specific ble device as output??

Thanks in advance

Can I use if statement some where in the code ?

sure you can, no permission needed :slight_smile:

Of course, this is what you could do to single out one device.

once a device is detected your instance of MyAdvertisedDeviceCallbacks is notified by a call to the method void onResult(BLEAdvertisedDevice advertisedDevice)And the toString method is called on its parameter. If you look at the BLEAdvertisedDevice header you could see that tons of other stuff can be asked from that instance including a method with an interesting name like getAddress() returning a BLEAddress type.

That’s where you should start exploring.

(Post the exact code you have been using when asking question please)

BLEAddress BLEAdvertisedDevice::getAddress() {
	
    if (getAddress(),"24:6f:28:1a:df:5e") {
        return m_address;
    }
    else {
        return 0;
    }
} // getAddress

As you suggested I have modified a code little bit
But I’m getting an error in the output
Like

Scanning…
Guru Meditation Error: Core 0 panic’ed (Unhandled debug exception)
Debug exception reason: BREAK instr

i have attached my .cpp file here in this

BLEAdvertisedDevice.cpp (14 KB)

Is that supposed to compare the two elements ? if (getAddress(),"24:6f:28:1a:df:5e")

That doesn’t work this way

Check what getAddress() reruns and apply the proper way to compare

hi thanks for the previous response

here get address returns m_address

so i tried these

BLEAddress BLEAdvertisedDevice::getAddress() {
    
    if (m_address.equals(esp_bd_addr_t "24:6f:28:1a:df:5e"))
    {
        return m_address;
    }

Error:
/Users/sibi/Library/Arduino15/packages/esp32/hardware/esp32/1.0.4/libraries/BLE/src/BLEAdvertisedDevice.cpp: In member function 'BLEAddress BLEAdvertisedDevice::getAddress()':
/Users/sibi/Library/Arduino15/packages/esp32/hardware/esp32/1.0.4/libraries/BLE/src/BLEAdvertisedDevice.cpp:54:40: error: expected primary-expression before string constant
if (m_address.equals(esp_bd_addr_t "24:6f:28:1a:df:5e"))
^
exit status 1
Error compiling for board ESP32 Dev Module.

If I use this instead

BLEAddress BLEAdvertisedDevice::getAddress() {
    
    if (m_address=="24:6f:28:1a:df:5e"))
    {
        return m_address;
    }

/Users/sibi/Library/Arduino15/packages/esp32/hardware/esp32/1.0.4/libraries/BLE/src/BLEAdvertisedDevice.cpp: In member function 'BLEAddress BLEAdvertisedDevice::getAddress()':
/Users/sibi/Library/Arduino15/packages/esp32/hardware/esp32/1.0.4/libraries/BLE/src/BLEAdvertisedDevice.cpp:54:18: error: no match for 'operator==' (operand types are 'BLEAddress' and 'const char [18]')
if (m_address=="24:6f:28:1a:df:5e"))
^
/Users/sibi/Library/Arduino15/packages/esp32/hardware/esp32/1.0.4/libraries/BLE/src/BLEAdvertisedDevice.cpp:54:40: error: expected primary-expression before ')' token
if (m_address=="24:6f:28:1a:df:5e"))

exit status 1
Error compiling for board ESP32 Dev Module.

just wondering how to compare the mac address
and i also tried

BLEAddress BLEAdvertisedDevice::getAddress() {
    
    if (m_address.equals( esp_bd_addr_t "24:6f:28:1a:df:5e"))
    {
        return m_address;
    }

still no luck
i feel like i'm missing some basic syntax.......to get my desired output
guide me through it pls

Have you looked at BLEAddress.h ?

There is a methodbool equals(BLEAddress otherAddress);so if you want to compare two adresses using equals() you need to pass another BLEAddress instance. To create one you need to look at the constructor and there are two

BLEAddress(esp_bd_addr_t address);
BLEAddress(std::string stringAddress);

so either you pass a esp_bd_addr_t which is the underlying description of the address if you look at the private part

private:
  esp_bd_addr_t m_address;

so that would be the most efficient representation (see [espressif’s documentation](https://docs. espressif.com/projects/esp-idf/en/latest/esp32/api-reference/bluetooth/esp_bt_defs.html), it’s an array of byte) or you pass a string.

But there is also a toString() method in the BLEAddress class which is likely going to give you a string back, then you can compare strings.

Give it another try, you’ll get there.

sorry brother
its been difficult to understand this
can you provide me an example code
for creating a BLE instance
:frowning:
“so if you want to compare two address using equals() you need to pass another BLEAddress instance”

for your reference
my BLE scan should show me only one device which mac address is 24:6f:28:1a:df:5e
or service uuid id is 4fafc201-1fb5-459e-8fcc-c5c9c331914b

thanks in advance

Try with The callback class

 class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice advertisedDevice) {
       Serial.print("found address: ");
       Serial.println(advertisedDevice.getAddress().toString());
    }
};

I just have my smartphone so can’t test anything at the moment