NimBLE-Arduino icon indicating copy to clipboard operation
NimBLE-Arduino copied to clipboard

Assigning a new NimBLEAddress at a later stage das not "equal"

Open NorbertRoller opened this issue 2 months ago • 4 comments

Hi, I tried everything I could think of, but I do not get it working. I compare devices by MAC to connect and this works flawless.

class scanCallbacks : public NimBLEScanCallbacks // BLEAdvertisedDeviceCallbacks
{
    void onResult(const NimBLEAdvertisedDevice* advertisedDevice) override 
      {
      Serial.printf("NimBLE_scan: Discovered Device: %s Type: %d \n", advertisedDevice->toString().c_str(),advertisedDevice->getAddressType());

      // Checke for LiFePo4 MAC discovered
      if (advertisedDevice->getAddress().equals(MAC_LiPo4))

changing the MAC is where I struggle to get the .equals working let me show you what I tried

This works: NimBLEAddress MAC_LiPo4("a4:c1:37:01:22:5f", 0); and in Setup NimBLEAddress MAC_LiPo4("38:3b:26:b5:15:a2", 0);

Since I have to change it , I added this at the top (before setup) and this works. char MAC_lithium[18] = {0x61,0x34,0x3A,0x63,0x31,0x3A,0x33,0x37,0x3A,0x30,0x31,0x3A,0x32,0x32,0x3A,0x35,0x66,0x00}; NimBLEAddress MAC_LiPo4(MAC_lithium, 0);

If I change the MAC_lithium[] in Setup and run NimBLEAddress MAC_LiPo4(MAC_lithium, 0); the .equals will always be false.

The snipped looks like this:

char MAC_lithium[18] = {0x61,0x34,0x3A,0x63,0x31,0x3A,0x33,0x37,0x3A,0x30,0x31,0x3A,0x32,0x32,0x3A,0x35,0x66,0x00};
char fake_MAC_lithium[18] = {0x6**4**,0x34,0x3A,0x63,0x31,0x3A,0x33,0x37,0x3A,0x30,0x31,0x3A,0x32,0x32,0x3A,0x35,0x66,0x00};
NimBLEAddress MAC_LiPo4(fake_MAC_lithium, 0); // 0=Public, 1= Random --- BLE_ADDR_PUBLIC (0) BLE_ADDR_RANDOM (1)

void setup(){
NimBLEAddress MAC_LiPo4(MAC_lithium, 0); // 0=Public, 1= Random --- BLE_ADDR_PUBLIC (0) BLE_ADDR_RANDOM (1)

Any help is appreciated

NorbertRoller avatar Oct 25 '25 06:10 NorbertRoller

This compare works as well: if(strcmp(advertisedDevice->getAddress().toString().c_str(), MAC_lithium) == 0)

Just the .equal will not

NorbertRoller avatar Oct 25 '25 13:10 NorbertRoller

You cannot set a public address, when you call setOwnAddress it will assign the address as random, so the address type is different and therefore the comparison fails. To fix it, change the types to 1 in your code.

h2zero avatar Oct 25 '25 15:10 h2zero

The device sends a private AddressType. Therefore I can not change the address type in the MAC I am searching for. So that is not a solution, or am I mistaken?

For reference I am using the following compare:

if(strcasecmp(advertisedDevice->getAddress().toString().c_str(), MAC_lithium) == 0)

NorbertRoller avatar Oct 27 '25 06:10 NorbertRoller

Please see #777

The answer is in the comment from you code:

NimBLEAddress MAC_LiPo4(MAC_lithium, 0); // 0=Public, 1= Random --- BLE_ADDR_PUBLIC (0) BLE_ADDR_RANDOM (1)

That should be: NimBLEAddress MAC_LiPo4(MAC_lithium, 1);

h2zero avatar Oct 27 '25 13:10 h2zero