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

How to set address type RANDOM

Open paulhamsh opened this issue 3 years ago • 7 comments

Hi I have a device I am trying to emulate and in Wireshark scan response and advertising response it shows as having TxAdd: RANDOM with address 0xdb, 0xab, 0x7f, 0xe0, 0x2f, 0x5c. How can I change the settings on the ESP32 to achieve that? I don't need a random address, I just want it to show as RANDOM, not as PUBLIC.

I think it can only be done with esp calls, but the code below seems not to work - either the call to esp_ble_gap_set_rand_addr or esp_ble_gap_config_local_privacy The esp_base_mac_addr_set doesn't fail, the other two do. But in all cases it stops Lightblue from scanning the ESP32. I'm not sure where in the code to put the esp calls, either.

BLEDevice::init("XXXXXXXX"); // 
BLEDevice::setPower(ESP_PWR_LVL_P9); /** +9db */


esp_bd_addr_t addr = {0xdb, 0xab, 0x7f, 0xe0, 0x2f,  0x5c}; 

// if (esp_ble_gap_set_rand_addr(addr) != ESP_OK) { Heltec.display->clear(); Heltec.display->drawString(0, 30, "FAIL RAND"); Heltec.display->display();}; // if (esp_ble_gap_config_local_privacy(true) != ESP_OK) { Heltec.display->clear(); Heltec.display->drawString(0, 30, "FAIL PRIV"); Heltec.display->display();}; if (esp_base_mac_addr_set(addr) != ESP_OK) { Heltec.display->clear(); Heltec.display->drawString(0, 30, "FAIL MAC"); Heltec.display->display();}; // esp_ble_gap_clear_rand_addr();

pSCServer = BLEDevice::createServer();
pSCServer->setCallbacks(new SCServerCallbacks());

paulhamsh avatar May 21 '22 14:05 paulhamsh

There is no API to do this but I believe if you modify the library slightly it may work.

change https://github.com/h2zero/NimBLE-Arduino/blob/8ff8f0fc35afed5db8276bd2f41afce22732e1f7/src/NimBLEDevice.cpp#L87 to BLE_OWN_ADDR_RANDOM

h2zero avatar May 21 '22 15:05 h2zero

When I do that it doesn't show in LightBlue scan - just vanishes. Should I change the address as well to meet the 'random' spec?

paulhamsh avatar May 21 '22 18:05 paulhamsh

What is really weird is if I make that change, load it, then undo the change, and re-load it - it works with the app that I needed it to work with, Without making the change it needs LightBlue (or other) to scan / connect first time then it is fine, with this change it doesn't need that. And it doesn't seem to be a ios caching thing because I just erased the iphone and it works fine. So it seems to make a difference to the ESP to have, at some point, been run with the RANDOM setting. Could that be true?

Update: tried on M5Stack and not the same effect, so perhaps just a fluke

paulhamsh avatar May 21 '22 19:05 paulhamsh

Interesting, unfortunately the way the esp32 handles the address and address type makes it somewhat difficult due to needing host based privacy. In addition to that the functions needed in NimBLE to set this aren't available without some code hacks to the core files.

If you're adventurous you can walk through the code call stack of NimBLEDevice::setOwnAddrType to find the required calls to set the address and type there.

h2zero avatar May 21 '22 22:05 h2zero

It looks like it works if you change the NimBLEDevice.cpp as you suggested, and also add the following, just before advertising:

    ble_addr_t blead;
    int rc;

    rc = ble_hs_id_gen_rnd(1, &blead);
    if (rc != 0) Serial.println("Rand failed");

    rc = ble_hs_id_set_rnd(blead.val);
    if (rc != 0) Serial.println("Addr failed");

    pAdvertising->setScanResponse(true);
    pAdvertising->start();

Thank you for your help!!!

paulhamsh avatar May 22 '22 00:05 paulhamsh

Ha, well done! I thought you were trying to set a specific address not just a random address of type random?

Awesome that this worked :smile:

h2zero avatar May 22 '22 01:05 h2zero

I thought I needed to (or, at least, that that would be easier) but this worked fine.

On 22 May 2022, at 02:38, h2zero @.***> wrote:

 Ha, well done! I thought you were trying to set a specific address not just a random address of type random?

Awesome that this worked 😄

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.

paulhamsh avatar May 22 '22 06:05 paulhamsh