Adsense HTML/JavaScript

Saturday, December 4, 2021

arduino-esp32, get BLE MAC address in String form

arduino-esp32 exercise to get BLE MAC address in String form. 
#include <BLEDevice.h>
#include "esp_bt_device.h"
 
String getMACinString() {
  const uint8_t* macAddress = esp_bt_dev_get_address();
  char charMAC[18];

  sprintf(charMAC, "%02X", (int)macAddress[0]);
  charMAC[2] = ':';
  sprintf(charMAC+3, "%02X", (int)macAddress[1]);
  charMAC[5] = ':';

  sprintf(charMAC+6, "%02X", (int)macAddress[2]);
  charMAC[8] = ':';
  sprintf(charMAC+9, "%02X", (int)macAddress[3]);
  charMAC[11] = ':';

  sprintf(charMAC+12, "%02X", (int)macAddress[4]);
  charMAC[14] = ':';
  sprintf(charMAC+15, "%02X", (int)macAddress[5]);

  return (String)charMAC;
}
 
void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println("\n\n==================================");
  Serial.printf("Chip Model: %s %s %d\n", 
                ESP.getChipModel(), 
                "rev.", 
                (int)ESP.getChipRevision());
  Serial.printf("with number of cores = %d\n", (int)ESP.getChipCores());
  Serial.println("==================================");

 
  //initBluetooth();
  BLEDevice::init("ESP32");
  
  String myMACString = getMACinString();
  Serial.println("My MAC address = " + myMACString);
}
 
void loop() {
  // put your main code here, to run repeatedly:

}

Tested on ESP32-DevKitC v4. with arduino-esp32 2.0.1.



No comments:

Post a Comment