Skip to content

Maxlen of Bluetooth name? Too long names result in a crash during start_advertising #5043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ThomasAtBBTF opened this issue Jul 23, 2021 · 9 comments

Comments

@ThomasAtBBTF
Copy link

CircuitPython version

Adafruit CircuitPython 7.0.0-alpha.4 on 2021-07-08; Adafruit ItsyBitsy nRF52840 Express with nRF52840

Code/REPL

# boot.py
usb_cdc.enable(console=True, data=False)
usb_midi.disable()

#code.py
...

devicename = "123456789A123456789B123457"
# this are 27 characters (no error in advertisement with names which are 26 characters long)

...

_bleio.adapter.name = devicename  # I don't know if the device name here or bleow is neccessary!
ble = adafruit_ble.BLERadio()
ble.name = devicename # I don't know if the device name here or above is necessary!
...
    print("Start advertizing:", self.service)
...
if self.isconnected:
  ...
else:
  print("unconnected", self.ble, self.ble.name, self.ble.connections)
...

...
ble.start_advertising(self.advertisement)
# In that call the Error below is thrown.

Repl:
unconnected <BLERadio object at 2001c8a0> 123456789A123456789B1234567 ()
Start advertizing: HID
Traceback (most recent call last):
  File "code.py", line 336, in <module>
  File "code.py", line 174, in check
  File "/lib/adafruit_ble/__init__.py", line 195, in start_advertising
ValueError: Invalid BLE parameter

Code done running.

Press any key to enter the REPL. Use CTRL-D to reload.

The next reload will often crash the board (the serial port over USB is disconnected)
And only reset(I think) or removing and reapplying USB-Power reboots the board.

Behavior

When I use device names with 27 characters or more a Runtime Error is thrown during Advertisement.
Screenshot_20210723095314

In this picture, you see a crash with devicename 30 characters long.,

Description

Error when allowing user-specific DeviceNames.
(I will limit them to 26 Characters in the code)user-specific

Additional information

During narrowing down this issue I found another strange bug which I will file later.

@ThomasAtBBTF
Copy link
Author

My suggestion would be, either to quietly slice the name in the setter to the max allowable length or raise an error in the setter and not let the error come up in start_advertizing.

@tannewt tannewt added the ble label Jul 23, 2021
@tannewt tannewt added this to the 7.x.x milestone Jul 23, 2021
@tannewt tannewt added the nordic label Jul 23, 2021
@tannewt
Copy link
Member

tannewt commented Jul 23, 2021

I like the idea of validating the name when it is set. That is clearest for the setter on how to fix it.

@ThomasAtBBTF
Copy link
Author

Fine with me, but is 26 characters really the threshold?
Is the length of other parameters in the report not changing the threshold?
I read somewhere that there is a length-limit of 31 bytes for a report.
But I don't understand this in enough detail to make a statement here.

@tannewt
Copy link
Member

tannewt commented Jul 26, 2021

I'm not sure where the limit is being imposed. Figuring that out is part of the task here.

31 bytes is the limit for legacy advertisements IIRC.

@ThomasAtBBTF
Copy link
Author

Yes, this is also my limited understanding.
But with Bluetooth Beacons, the name of the beacon is often much longer.
Maybe there is a way to split the name into multiple chunks.

@tannewt
Copy link
Member

tannewt commented Jul 27, 2021

What do you mean by Bluetooth Beacon? Do you have an example product that does it?

BLE 5 does have extended advertisements that would support up to ~512 bytes IIRC.

@ThomasAtBBTF
Copy link
Author

Bluetooth beacons are used for indoor navigation for blind and visually impaired persons.
Here are articles describing the functionality:
https://www.lighthouseguild.org/vision-health/a-hybrid-indoor-positioning-system-for-the-blind-and-visually-impaired-using-bluetooth-and-google-tango/

https://www.irjet.net/archives/V7/i4/IRJET-V7I4609.pdf

@tannewt
Copy link
Member

tannewt commented Jul 29, 2021

How are you getting the name of a BLE beacon? My impression is that they may simply advertise an ID that an app can lookup for a name. NRF Connect will list the contents of advertisements around you.

@dhalbert
Copy link
Collaborator

dhalbert commented Dec 9, 2021

The defined maximum length of the name is 248 bytes, but whether you can use that depends on the advertising capabilities of the peripheral. The actual length that fits depends on what else is in the advertising packet (e.g. various UUIDs), including in the extended advertisement. So I don't think we can validate the length when .name is set. We could possibly throw a better error instead of Invalid BLE parameter.

The nRF SD adds the default name to the advertisement by itself, so it's not even that we are assembling the advertisement and it's too long. We'd have to keep track of the default name length and also see that there is not another name given.

I am going to move this to Long Term, because I don't see a short-term fix, unfortunately.

@dhalbert dhalbert modified the milestones: 7.x.x, Long term Dec 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants