Set bluetooth mac address on ESP32? #10080
Replies: 6 comments 6 replies
-
i think it's not possible to set specific bluetooth mac address. check doc about 'addr_mode' |
Beta Was this translation helpful? Give feedback.
-
It seems the esp-idf only recently got the API to change individual mac addresses: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/misc_system_api.html#mac-address It's not even in the 5.0 release. It seems there were ways to change single or all mac on previous versions as well but this seems to be the first time there's a clean API to do this. |
Beta Was this translation helpful? Give feedback.
-
I just tested to add support for esp_base_mac_addr_set() to MPY and it works just fine. Downside of this approach is that it needs to be done before any network related code is run and it affects all mac addresses (Wifi station, wifi ap, bluetooth and ethernet). I'll append a patch here. |
Beta Was this translation helpful? Give feedback.
-
So here's the little patch that allows to read and write the base mac address on esp32. Initially I hoped that I could use this to first read the base mac, then modify it and write it back. Turned out that you cannot read the base MAC before you set it. So you cannot use that to read the base mac set by the system itself. But unique_id() returns exactly that. So adding this to boot.py will move the mac address into the LNT range:
Afterwards the device shows up using the 10:45:f8 vendor prefix for wifi as well as bluetooth. |
Beta Was this translation helpful? Give feedback.
-
alright. so. i think i did a thing. i mean it's ghetto and i'd really love for this to be implemented the way it should be, at the BLE level, but this should work in the meantime. per the documentation, given the base device MAC, the last octet of the BLE MAC is offset by 2 bits. which presents itself as an issue if you try to set your desired BLE MAC using the base_mac_addr method: from machine import base_mac_addr
import bluetooth as bt
# let's say this is the desired BLE mac
mac = bytearray([0x1C, 0x88, 0x3F, 0xE5, 0x7E, 0x8D])
# set device base mac addr
base_mac_addr(mac)
ble = bt.BLE()
ble.active(True)
ble.config('mac')
(0, b'\x1c\x88?\xe5~\x8f') which can be programmed around like so: from machine import base_mac_addr
import bluetooth as bt
mac = bytearray([0x1C, 0x88, 0x3F, 0xE5, 0x7E, 0x8D])
def base_ble_offset(mac):
mac[5] = mac[5] - 2
return mac
base_mac_addr(base_ble_offset(mac))
ble = bt.BLE()
ble.active(True)
ble.config('mac')
(0, b'\x1c\x88?\xe5~\x8d') did i do it right? |
Beta Was this translation helpful? Give feedback.
-
Is there a chance this patch will make it into the official MicroPython distribution?! |
Beta Was this translation helpful? Give feedback.
-
I am trying to act like a fischertechnik toy controller. The matching other device will only connect to devices of a specific vendor, in this case LNT (vendor prefix 10:45:F8).
Can I change the BT MAC address through micropython? I tried:
bluetooth.BLE().config(mac=(0, b'\x01\x02\x03\x04\x05\x06'))
But that results in:
ValueError: unknown config param
Beta Was this translation helpful? Give feedback.
All reactions