@@ -21,6 +21,9 @@ class BTHome:
21
21
0x40 # Currently hardcoded as: no encryption, regular updates, version 2
22
22
)
23
23
24
+ # Advertising size limit
25
+ _ADVERT_PAYLOAD_MAX_BYTES = 31 # [^3]
26
+
24
27
debug = False
25
28
26
29
# Device name used in BLE advertisements.
@@ -293,7 +296,7 @@ class BTHome:
293
296
window = False
294
297
295
298
def __init__ (self , local_name = "BTHome" , debug = False ):
296
- local_name = local_name [:10 ] # Truncate to fit [^3 ]
299
+ local_name = local_name [:10 ] # Truncate to fit [^4 ]
297
300
self ._local_name = local_name
298
301
self .debug = debug
299
302
@@ -310,7 +313,7 @@ def packet_id(self):
310
313
# Technically, the functions below could be static methods, but @staticmethod
311
314
# on a dictionary of functions only works with Python >3.10, and MicroPython
312
315
# is based on 3.4. Also, __func__ and __get()__ workarounds throw errors in
313
- # MicroPython. [^4 ]
316
+ # MicroPython. [^5 ]
314
317
315
318
# Binary flag (true/false, on/off)
316
319
def _pack_binary (self , object_id , value ):
@@ -481,6 +484,9 @@ def pack_advertisement(self, *args):
481
484
advertisement_bytes += self ._pack_service_data (* args )
482
485
if self .debug :
483
486
print ("BLE Advertisement:" , advertisement_bytes .hex ().upper ())
487
+ print ("Advertisement Len:" , len (advertisement_bytes ))
488
+ if len (advertisement_bytes ) > BTHome ._ADVERT_PAYLOAD_MAX_BYTES :
489
+ raise ValueError ("BLE advertisement exceeds max length" )
484
490
return advertisement_bytes
485
491
486
492
@@ -502,5 +508,6 @@ def demo():
502
508
503
509
# [^1]: https://community.silabs.com/s/article/kba-bt-0201-bluetooth-advertising-data-basics
504
510
# [^2]: https://bthome.io/images/License_Statement_-_BTHOME.pdf
505
- # [^3]: https://community.st.com/t5/stm32-mcus-wireless/ble-name-advertising/m-p/254711/highlight/true#M10645
506
- # [^4]: https://stackoverflow.com/questions/41921255/staticmethod-object-is-not-callable
511
+ # [^3]: https://github.com/orgs/micropython/discussions/12701
512
+ # [^4]: https://community.st.com/t5/stm32-mcus-wireless/ble-name-advertising/m-p/254711/highlight/true#M10645
513
+ # [^5]: https://stackoverflow.com/questions/41921255/staticmethod-object-is-not-callable
0 commit comments