How does bluetooth device address work (bd_addr)

zazas321
Posts: 231
Joined: Mon Feb 01, 2021 9:41 am

How does bluetooth device address work (bd_addr)

Postby zazas321 » Wed Oct 06, 2021 5:04 am

Hello. I would like to understand a little bit more about how bd_addr work and how they are created. I have built ble_security_server example.

After I connect my phone to ESP32 device, I get the following messages on serial output:

Code: Select all

I (17405) SEC_GATTS_DEMO: ESP_GATTS_CONNECT_EVT
I (17745) SEC_GATTS_DEMO: remote BD_ADDR: 6a1513dc691a
I (17745) SEC_GATTS_DEMO: address type = 1
I (17745) SEC_GATTS_DEMO: pair status = success
I (17755) SEC_GATTS_DEMO: auth mode = ESP_LE_AUTH_REQ_SC_MITM_BOND
I (17755) SEC_GATTS_DEMO: Bonded devices number : 1

I (17765) SEC_GATTS_DEMO: Bonded devices list : 1

I (17765) SEC_GATTS_DEMO: 6a 15 13 dc 69 1a 

Can someone help me undertand how

Code: Select all

remote BD_ADDR: 6a1513dc691a


are created? Is there any way to know the unique bluetooth address before I connect anything ( after the bluetooth initialization ). I would like to send unique bluetooth address to the server and trying to think of what would be the best way. Any ideas are appreaciated

chegewara
Posts: 2371
Joined: Wed Jun 14, 2017 9:00 pm

Re: How does bluetooth device address work (bd_addr)

Postby chegewara » Wed Oct 06, 2021 5:21 am

You can read about bluetooth RPA:
https://www.electronicdesign.com/techno ... ignspart-2

It is about BLE, but idea is the same or similar.

zazas321
Posts: 231
Joined: Mon Feb 01, 2021 9:41 am

Re: How does bluetooth device address work (bd_addr)

Postby zazas321 » Wed Oct 06, 2021 5:34 am

Thanks for the reading material. i have went through the page that you have suggested, it is still not fully clear to me. I have just found a function that returns the ble address

Code: Select all

esp_bt_dev_get_address()
I have executed the following code after esp_bluedroid_enable as the espressif documentation suggesting ( https://docs.espressif.com/projects/esp ... evice.html )

Code: Select all

    const uint8_t* point = esp_bt_dev_get_address();
    for (int i = 0; i < 6; i++) {
        char str[3];
        sprintf(str, "%02X", (int)point[i]);
        printf(str);
        if (i < 5){
            printf(":");
        }
    }
   
The result is as following:

Code: Select all

I (1386) SEC_GATTS_DEMO: BLE_setup init bluetooth
AC:67:B2:2E:38:36I (1476) SEC_GATTS_DEMO: The number handle = 4
I (1506) SEC_GATTS_DEMO: advertising start success
As you can see, it has returned an address:

Code: Select all

AC:67:B2:2E:38:36
I have tried to restart the device a few times and this address does not seem to change.


Could someone clarify to me what are the differences between these 2 addresses?

chegewara
Posts: 2371
Joined: Wed Jun 14, 2017 9:00 pm

Re: How does bluetooth device address work (bd_addr)

Postby chegewara » Wed Oct 06, 2021 5:51 am

This function return your own (esp32) BT address:

Code: Select all

esp_bt_dev_get_address()
This is remote address, which means it belongs to device that is connected to esp32:

Code: Select all

remote BD_ADDR: 6a1513dc691a // 6a:15:13:dc:69:1a

zazas321
Posts: 231
Joined: Mon Feb 01, 2021 9:41 am

Re: How does bluetooth device address work (bd_addr)

Postby zazas321 » Wed Oct 06, 2021 7:04 am

Oh thank you. That makes sense now. I have been further investigating advertisement structure:

Code: Select all

static esp_ble_adv_params_t heart_rate_adv_params = {
    .adv_int_min        = 0x100,
    .adv_int_max        = 0x100,
    .adv_type           = ADV_TYPE_IND,
    .own_addr_type      = BLE_ADDR_TYPE_RANDOM,
    .channel_map        = ADV_CHNL_ALL,
    .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};
.own_addr_type = BLE_ADDR_TYPE_RANDOM,

I can choose between

BLE_ADDR_TYPE_RANDOM and BLE_ADDR_TYPE_PUBLIC

If I choose to use BLE_ADDR_TYPE_PUBLIC, the the device address displayed on my bluetooth app (lightblue) will be whatever I get from esp_bt_dev_get_address().

If I choose to use BLE_ADDR_TYPE_RANDOM, then the device address shown on my bluetooth app (lightblue) will be random.

There is one more thing that I got confused about. All of a sudden, my remote BT address changed:

Code: Select all

I (8135) SEC_GATTS_DEMO: remote BD_ADDR: 6569ba5c95f1
I (8135) SEC_GATTS_DEMO: address type = 1
I (8135) SEC_GATTS_DEMO: pair status = success
I (8135) SEC_GATTS_DEMO: auth mode = ESP_LE_AUTH_REQ_SC_MITM_BOND
I (8145) SEC_GATTS_DEMO: Bonded devices number : 1

I (8155) SEC_GATTS_DEMO: Bonded devices list : 1

I (8155) SEC_GATTS_DEMO: 65 69 ba 5c 95 f1 

I have connected to the ESP32 with the same phone as before. Could you explain why would the remote bt address change?

chegewara
Posts: 2371
Joined: Wed Jun 14, 2017 9:00 pm

Re: How does bluetooth device address work (bd_addr)

Postby chegewara » Wed Oct 06, 2021 8:03 am

1) i think you have to add this function to make esp32 advertising private address

Code: Select all

esp_ble_gap_config_local_privacy(true);
2) this is exactly how it works; every some time each device which is using private address, RPA or random, is advertising/connecting with new address
There is one more thing that I got confused about. All of a sudden, my remote BT address changed:
3) there is few nice tutorials how to use BT/BLE on esp32:
https://github.com/espressif/esp-idf/bl ... through.md
https://github.com/espressif/esp-idf/bl ... through.md
https://github.com/espressif/esp-idf/bl ... through.md
https://github.com/espressif/esp-idf/bl ... through.md

dont even try this one:
https://github.com/espressif/esp-idf/bl ... through.md

zazas321
Posts: 231
Joined: Mon Feb 01, 2021 9:41 am

Re: How does bluetooth device address work (bd_addr)

Postby zazas321 » Wed Oct 06, 2021 9:11 am

I have esp_ble_gap_config_local_privacy(true); in my code.

I can control what ID is being advertised by modifying parameter own_addr_type in this structure as I have mentioned above.

static esp_ble_adv_params_t heart_rate_adv_params = {
.adv_int_min = 0x100,
.adv_int_max = 0x100,
.adv_type = ADV_TYPE_IND,
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
.channel_map = ADV_CHNL_ALL,
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};

BLE_ADDR_TYPE_RANDOM will advertise it with a random ID

BLE_ADDR_TYPE_PUBLIC with advertise with a BLE predefined ID



I have went through these code explanations multiple times. I do not think they cover all the aspect in as much details as I would like to. I have a basic understanding of how BLE supposed to work and I got it working already. I am now trying to learn other slightly more "advanced" things of BLE

Who is online

Users browsing this forum: kaxx1975 and 157 guests