ESP32 获取蓝牙MAC地址【PlatformIO+Arduino】

最近更新于 2025-07-06 20:44

测试环境

VScode: 1.101.2
PlatformIO:Core 6.1.18,Home 3.4.4

Espressif 32:6.11.0(工具链平台)
ESP32-WROOM-32(硬件)

实现

#include "Arduino.h"

#include "BluetoothSerial.h"

String deviceName = "ESP32蓝牙";

BluetoothSerial serialBT;

void setup()
{
    Serial.begin(115200);
    serialBT.begin(deviceName);

    uint8_t macArr[6] = {0};
    serialBT.getBtAddress(macArr); // 获取 MAC 地址数组
    Serial.print("方式一 通过字节数组获取MAC:");
    for (int i = 0; i < ESP_BD_ADDR_LEN; ++i)
    {
        Serial.print(macArr[i], HEX);
        Serial.print(":");
    }
    Serial.println();

    BTAddress macObj = serialBT.getBtAddressObject(); // 获取 MAC 地址对象
    Serial.printf("方式二 通过对象获取MAC:%s\n", macObj.toString().c_str());

    String macStr = serialBT.getBtAddressString();
    Serial.printf("方式三 通过字符串对象获取MAC:%s\n", macStr.c_str());
}

void loop()
{
    delay(100);
}

file

file

ESP32 获取蓝牙MAC地址【PlatformIO+Arduino】
Scroll to top
打开目录