pythonraspberry-piraspberry-pi-pico

How to change the Bluetooth name of the Raspberry pico 2W?


Currently, I want to change the Name of the Pico 2W device displayed on the nRF Connect app. I am using Micro Pico in VsCode. I tried many things but the Name is always "N/A".

import bluetooth
from time import sleep


bluetooth.BLE().active(True)
ble = bluetooth.BLE()


device_name = 'PicoW_Device' 


advertising_data = b'\x02\x01\x06\x03\x03\x0F\x18\x09' + bytes(device_name, 'utf-8')


ble.gap_advertise(100, adv_data=advertising_data)

print(f"The Name of the device is currently: {device_name}")

while True:
    sleep(1)

I tried to change the name, but it always displayed "N/A"


Solution

  • The problem is how you constructed the advertising_data. Try this

    device_name = 'PicoW_Device'
    
    advertising_data = bytearray()
    advertising_data += b'\x02\x01\x06'  # Flags
    advertising_data += bytearray([1 + len(device_name), 0x09]) # Name length and Complete Local Name AD type
    advertising_data += device_name.encode('utf-8') # Ensures the name is encoded into bytes
    
    ble.gap_advertise(100, adv_data=bytes(advertising_data)) # Converts the bytearray to bytes