← index #13639Issue #13642
Related · high · value 2.708
QUERY · ISSUE

STM32: BLE Bonding Example does not work

openby hoihuopened 2024-02-11updated 2025-03-03
bugport-stm32

I'm using a Pyboad SF2 and nightly build as of
MicroPython v1.23.0-preview.90.ged15b3c6c on 2024-02-06; PYBD-SF2W with STM32F722IEK

When trying the bonding example:
https://github.com/micropython/micropython/blob/master/examples/bluetooth/ble_bonding_peripheral.py

I got the following response:

no secrets available
get secret: 2 0 None
get secret: 10 0 b'irk'
set secret: (10, b'irk') b'\xdc$W\xae\xf1I\xb9\xee\xc0\x9f\xc7\xed,\x82\t\x94'
Traceback (most recent call last):
  File "<stdin>", line 199, in <module>
  File "<stdin>", line 184, in demo
  File "<stdin>", line 81, in __init__
  File "<stdin>", line 158, in _advertise
OSError: -513

the problem seems to be related to the ble.config's addr_mode. If I uncomment the call here:
https://github.com/micropython/micropython/blob/master/examples/bluetooth/ble_bonding_peripheral.py#L75
and here:
https://github.com/micropython/micropython/blob/master/examples/bluetooth/ble_bonding_peripheral.py#L157
it doesn't throw the error.

I understand that the ble bonding is still a work in progress demo. However I wanted to have a go on implementing bonding support for the pico_w. , and for this I thought I'd first start with a working example from a supported platform (even if it's from nimble - so another ble stack than on the pico_w).

Also.. is addr_mode set to 2 required for ble bonding?

Thanks for hints and clarifications!

CANDIDATE · ISSUE

STM32: BLE bond is not persistant

openby hoihuopened 2024-02-11updated 2025-03-03
bugport-stm32

Situation

  • MicroPython v1.23.0-preview.90.ged15b3c6c on 2024-02-06; PYBD-SF2W with STM32F722IEK
  • using the temperature sensor code with bonding from https://github.com/micropython/micropython/blob/master/examples/bluetooth/ble_bonding_peripheral.py
  • calls to ble.config(addr_mode=2= are commented out, as explained in https://github.com/micropython/micropython/issues/13639
  • I'm using an iPhone12 as counterpart for BLE tests.

What happens

  • The device advertises itself, is connectable -> ok
  • The secrets are exchanged (set_secret() messages appear when connecting). -> ok
  • The secrets are stored in secrets.json when disconnected -> ok
  • When rebooting the pyboard, or after a reset, I can't connect anymore, the iPhone complains with "peer removed pairing information" -> not ok
  • When deleting the bond from the iPhone, via bluetooth settings -> tap on device -> forget, it works again.

Details

  • I logged the secret exchanges with some debug messages on the get_secret event handler. While pairing for the first time it says the following:
encryption update 64 1 0 1 16
30 (1, <memoryview>, <memoryview>)
set secret: (1, b'\x00\x8a\xd0\xb8\xc5\xfb\xa0') b'\x00\x8a\xd0\xb8\xc5\xfb\xa0\x10\x00\x00\x0.........'
30 (2, <memoryview>, <memoryview>)
set secret: (2, b'\x00\x8a\xd0\xb8\xc5\xfb\xa0') b'\x00\x8a\xd0\xb8\xc5\xfb\xa0\x10\x00\x00\x0.........'
2 (64, 0, <memoryview>)

After rebootin the pyboard it says:

1 (64, 1, <memoryview>)
29 (1, 0, <memoryview>)
get secret: 1 0 b"\x01)\x8a\x7f'\x95H"
secrets store: {(1, b'\x00\x8a\xd0\xb8\xc5\xfb\xa0'): '\x00\x8a\xd0\xb8\xc5\xfb\xa0\x10\x00\x00\x0.....', 
                          (2, b'\x00\x8a\xd0\xb8\xc5\xfb\xa0'): '\x00\x8a\xd0\xb8\xc5\xfb\xa0\x10\x00\x00\x00\x0.......'}
secret check: (1, b"\x01)\x8a\x7f'\x95H")
result: None
2 (64, 1, <memoryview>)

the secret check log log the key that was received here: https://github.com/micropython/micropython/blob/master/examples/bluetooth/ble_bonding_peripheral.py#L140 and the secret store is self._secrets

so since self._secrets does not contain a key with (1, b"\x01)\x8a\x7f'\x95H") it return None and I think that's the reason that the pairing fails. I hope can shed some light?

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