How To Find Mac Address Of Bluetooth Module Hc 05
In this ESP32 tutorial, nosotros will bank check how to get the Bluetooth accost of the device, using the Arduino core. The tests of this ESP32 tutorial were performed using a DFRobot'due south ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.
Introduction
In this ESP32 tutorial, we volition check how to get the Bluetooth accost of the device, using the Arduino core.
The Bluetooth Device Address (sometimes referred equally BD_ADDR) is a unique 6 byte identifier assigned to each Bluetooth device by the manufacturer [i].
One of import thing to mentioned is that the 3 almost significant bytes (upper part of the address) tin be used to decide the manufacturer of the device [ane].
Regarding the code, we will be using the IDF Bluetooth API, which provides to us a role to recall the mention address.
The tests of this ESP32 tutorial were performed using a DFRobot's ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.
If you prefer, yous can cheque a video tutorial on how to obtain the device Bluetooth address on my Youtube channel. The approach used in this video is slightly different since we take advantage of the BluetoothSerial.h library to initialize the Bluetooth stack.
The code
We will start our lawmaking by including the libraries needed to both initialize the Bluetooth stack (esp_bt_main.h) and to have access to the function that allows to recollect the device address (esp_bt_device.h).
#include "esp_bt_main.h" #include "esp_bt_device.h"
As we have been doing in the previous tutorials, nosotros will create a role to initialize both the Bluetooth controller and host stacks. Although in this tutorial we are not going to actually perform whatsoever Bluetooth communication, we need to have the Bluedroid host stack initialized and enabled to be able to retrieve the device address [2].
So, our Bluetooth init function will be similar to what we have been doing in the previous tutorials. We first initialize and enable the controller stack with a call to the btStart part and then we initialize the Bluedroid stack with a call to theesp_bluedroid_init function. Subsequently that, we call theesp_bluedroid_enableto enable the Bluedroid stack.
You can check this init function below, already with all the mentioned calls and the error checking.
bool initBluetooth() { if (!btStart()) { Serial.println("Failed to initialize controller"); return false; } if (esp_bluedroid_init() != ESP_OK) { Series.println("Failed to initialize bluedroid"); return false; } if (esp_bluedroid_enable() != ESP_OK) { Series.println("Failed to enable bluedroid"); return false; } }
We will follow the same arroyo and encapsulate the printing of the device address in a function, which nosotros will callprintDeviceAddress.
void printDeviceAddress() { // Print code here }
In order to get the device accost, nosotros simply need to call the esp_bt_dev_get_address function.
This part takes no arguments and returns the six bytes of the Bluetooth device address. In example the Bluedroid stack has not been initialized, it will return Nada [2].
Note that the six bytes will be returned every bit a pointer to an assortment of uint8_t, which nosotros will store on a variable.
const uint8_t* betoken = esp_bt_dev_get_address();
Equally mentioned, this array will have 6 elements which we tin iterate and print one by one.
for (int i = 0; i < 6; i++) { // Format and print the bytes }
We volition apply the sprintf office to format each byte of the address in a two characters length hexadecimal string, to make it easier to read and to follow the standard format [ane].
We will utilize the%02X format specifier, which prints each byte every bit a hexadecimal uppercase string with two characters, with a leading zero padded if needed.
Note that in the standard format the address is displayed with each byte separated by colons [one], which is also the format nosotros are going to employ.
for (int i = 0; i < vi; i++) { char str[iii]; sprintf(str, "%02X", (int)point[i]); Series.print(str); if (i < 5){ Serial.print(":"); } }
Now that we finished our printing function, we will motion on to the Arduino setup. There, we will initialize a serial connection to print the results of our program.
Followed by that, we will call the Bluetooth initialization office and the address press function.
void setup() { Serial.begin(115200); initBluetooth(); printDeviceAddress(); }
The concluding source lawmaking tin be seen below.
#include "esp_bt_main.h" #include "esp_bt_device.h" bool initBluetooth() { if (!btStart()) { Serial.println("Failed to initialize controller"); return faux; } if (esp_bluedroid_init() != ESP_OK) { Serial.println("Failed to initialize bluedroid"); return faux; } if (esp_bluedroid_enable() != ESP_OK) { Serial.println("Failed to enable bluedroid"); return imitation; } } void printDeviceAddress() { const uint8_t* point = esp_bt_dev_get_address(); for (int i = 0; i < 6; i++) { char str[3]; sprintf(str, "%02X", (int)signal[i]); Series.impress(str); if (i < 5){ Series.print(":"); } } } void setup() { Serial.begin(115200); initBluetooth(); printDeviceAddress(); } void loop() {}
Testing the lawmaking
To test the code, simply compile it and upload information technology. When information technology finishes, open the Arduino IDE Serial Monitor and check the string that gets printed. Y'all should have a result similar to effigy i, which shows the device address in the hexadecimal format we specified.
Figure 1 – Printing the Bluetooth address of the ESP32.
Every bit mentioned in the introductory section, we tin apply this address to lookup the vendor of the Bluetooth device. You lot can use this website to make the lookup. As shown in figure 2, the device address shown before has Espressif (the company that makes the ESP32) as vendor.
Effigy 2 – Bluetooth device address vendor lookup.
References
[1] https://macaddresschanger.com/what-is-bluetooth-address-BD_ADDR
[2] http://esp-idf.readthedocs.io/en/latest/api-reference/bluetooth/esp_bt_device.html?highlight=esp_bt_dev_get_address
Related posts
- ESP32 Arduino: Serial communication over Bluetooth Hi World
- ESP32 Arduino Bluetooth Classic: Setting the device name
- ESP32 Arduino Bluetooth classic: Getting started
- ESP32 Bluetooth: Advertising a SPP service with SDP
- ESP32 Bluetooth: Receiving data through RFCOMM
- ESP32 Bluetooth: Finding the device with Python and BTStack
- ESP32 Bluetooth: Using the BTstack library
Source: https://techtutorialsx.com/2018/03/09/esp32-arduino-getting-the-bluetooth-device-address/
Posted by: benoithoughle.blogspot.com
0 Response to "How To Find Mac Address Of Bluetooth Module Hc 05"
Post a Comment