esp-idf BLE scanning

Viewed 2235

I am working ESP-idf to program my ESP32 board. My Goal is to make it scan for a specific BLE (Bluetooth Low Energy) Device. I figured out how to make it scan by using one of the examples that are provided by "Espressif" called "ibeacon_demo.c" the thing is I don't know how to extract the address of any searched device and have it as string so I can compare it with the my BLE device address.

ibeacon_demo.c: https://github.com/pycom/pycom-esp-idf/blob/master/examples/bluetooth/ble_ibeacon/main/ibeacon_demo.c

I am pretty sure there is something has to be done in this statement:

 if (esp_ble_is_ibeacon_packet(scan_result->scan_rst.ble_adv, scan_result->scan_rst.adv_data_len)){
            esp_ble_ibeacon_t *ibeacon_data = (esp_ble_ibeacon_t*)(scan_result->scan_rst.ble_adv);
            ESP_LOGI(DEMO_TAG, "----------iBeacon Found----------");
            esp_log_buffer_hex("IBEACON_DEMO: Device address:", scan_result->scan_rst.bda, BD_ADDR_LEN );
            esp_log_buffer_hex("IBEACON_DEMO: Proximity UUID:", ibeacon_data->ibeacon_vendor.proximity_uuid, ESP_UUID_LEN_128);

            uint16_t major = ENDIAN_CHANGE_U16(ibeacon_data->ibeacon_vendor.major);
            uint16_t minor = ENDIAN_CHANGE_U16(ibeacon_data->ibeacon_vendor.minor);
            ESP_LOGI(DEMO_TAG, "Major: 0x%04x (%d)", major, major);
            ESP_LOGI(DEMO_TAG, "Minor: 0x%04x (%d)", minor, minor);
            ESP_LOGI(DEMO_TAG, "Measured power (RSSI at a 1m distance):%d dbm", ibeacon_data->ibeacon_vendor.measured_power);
            ESP_LOGI(DEMO_TAG, "RSSI of packet:%d dbm", scan_result->scan_rst.rssi);
        }

here is the API reference for the example I used "ibeacon_demo.c" [https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/bluetooth/esp_gap_ble.html#_CPPv428esp_ble_gap_update_whitelistb13esp_bd_addr_t22esp_ble_wl_addr_type_t][1]

1 Answers

You question is 2 parts, how to extract the address and how to compare it to the one your looking for.

The first is already answered for you, as this sample is extracting the address and putting it in the log:

esp_log_buffer_hex("IBEACON_DEMO: Device address:", scan_result->scan_rst.bda, BD_ADDR_LEN );

But your second question take a deeper understanding of how to compare byte arrays. I can try to help you understand the second part by telling you that the address is a byte array and that it is 6 bytes long, that's what the BD_ADDR_LEN constant is telling you.

Now you must compare the 2 byte arrays.

After you figure that out you can get into public and random addresses. But for now that's enough.

Related