← index #7130PR #17725
Related · high · value 0.779
QUERY · ISSUE

ESP32 Bluetooth: Error code "OSError: -18" when advertising with a too long payload

openby YueyanGuo1218opened 2021-04-19updated 2023-02-28
port-esp32

MicroPython v1.15 on 2021-04-18; ESP32 module with ESP32

When trying to advertising with a long payload(longer than 31 bytes) using bluetooth lib on esp32, Micropython would give an unclear error code " OSError: -18 ".

import bluetooth
esp32_ble = bluetooth.BLE()
esp32_ble.active(True)

payload = b'\x02\x01\x06\n\taLongName\x11\x07\x9e\xca\xdc$\x0e\xe5\xa9\xe0\x93\xf3\xa3\xb5\x01\x00@n'

esp32_ble.gap_advertise(interval_us=200000, adv_data=payload)
# Traceback (most recent call last):
#  File "<stdin>", line 6, in <module>
# OSError: -18

A payload longer than 31 bytes might be generated with a 128bit Service UUID and a long name(len>8).

from ble_advertising import advertising_payload    #example in /micropython/example/bluetooth

ServiceUUID = bluetooth.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")
payload = advertising_payload(name="muchTooLong", services=[ServiceUUID])

A short name would work just fine.
The error code is not helpful for debugging, but I'm not sure how to improve this.
I opened this issue to notify who have met the same problem.

CANDIDATE · PULL REQUEST

examples/bluetooth/ble_advertising.py: fix decoding of UUID32

mergedby dpgeorgeopened 2025-07-20updated 2025-07-31
docsexamples

Summary

This fixes a long-standing bug in the BLE example, the part that decodes UUIDs.

The bug was reported here: https://github.com/raspberrypi/pico-micropython-examples/issues/86

I also updated the relevant part of the documentation, about how UUIDs are constructed.

Testing

Tested with the following script:

import bluetooth
import ble_advertising

for uuid in (
    0x1234,
    b'\x34\x12',
    b'\x78\x56\x34\x12',
    b'1234567812345678',
    '6E400001-B5A3-F393-E0A9-E50E24DCCA9E',
):
    adv = ble_advertising.advertising_payload(name="test", services=[bluetooth.UUID(uuid)])
    try:
        print(ble_advertising.decode_services(adv))
    except Exception as er:
        print(er)

Prior to this fix the output was:

[UUID(0x1234)]
[UUID(0x1234)]
buffer too small
[UUID('38373635-3433-3231-3837-363534333231')]
[UUID('6e400001-b5a3-f393-e0a9-e50e24dcca9e')]

With the fix, the output is:

[UUID(0x1234)]
[UUID(0x1234)]
[UUID(0x12345678)]
[UUID('38373635-3433-3231-3837-363534333231')]
[UUID('6e400001-b5a3-f393-e0a9-e50e24dcca9e')]

Trade-offs and Alternatives

<!-- If the Pull Request has some negative impact (i.e. increased code size)
then please explain why you think the trade-off improvement is worth it.
If you can think of alternative ways to do this, please explain that here too.

 Delete this heading if not relevant (i.e. small fixes) -->

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied