
Como funciona
Vínculo é pareamento com memória. Com bond=True, as duas placas guardam a chave de longo prazo em um dict do Python pelos eventos de IRQ de armazenamento de chaves do MicroPython (28/29/30): o mesmo mecanismo com que produtos reais guardam vínculos. O cliente pareia, criptografa e depois encerra a conexão de propósito. O servidor percebe a desconexão e volta a anunciar (a partir do laço principal: reiniciar dentro da IRQ criaria uma condição de corrida com o controlador). O cliente se reconecta ao endereço guardado e o enlace volta a ser criptografado direto com o vínculo guardado: sem um segundo pareamento, que é justamente o objetivo.
O que está na bancada
- 2× ESP32-C6
O código
Este script MicroPython roda na placa emulada a cada inicialização. Edite-o em “Code”.
import bluetooth, time b = bluetooth.BLE() b.active(1) b.config(bond=True, mitm=False, le_secure=False) S = {} server = b.config('mac')[1][-1] % 2 == 0 tag = 'SRV' if server else 'CLI' tgt = [] cn = [] dis = [] enc = [] def cb(e, d): if e == 29: t, i, k = d if k is None: j = 0 for q in S: if q[0] == t: if j == i: return S[q] j += 1 return None return S.get((t, bytes(k)), None) if e == 30: t, k, v = d k = (t, bytes(k)) if v is None: S.pop(k, None) else: S[k] = bytes(v) return True if e == 28: enc.append(1) if len(enc) > 1: print(tag + ' re-encrypted from the stored bond') else: print(tag + ' encrypted, bond stored') elif e == 5 and not tgt: tgt.append((d[0], bytes(d[1]))) print('CLI found advertiser') elif e == 7: print('CLI connected' + (' again' if cn else '')) cn.append(d[0]) elif e == 8: dis.append(1) print('CLI link closed') elif e == 1: print('SRV central connected') elif e == 2: dis.append(1) print('SRV central left') b.irq(cb) if server: b.gap_advertise(50000, b'\x02\x01\x06\x05\xffCAFE', connectable=True) print('SRV ' + 'advertising, bondable') while not dis: time.sleep_ms(50) time.sleep_ms(300) b.gap_advertise(50000, b'\x02\x01\x06\x05\xffCAFE', connectable=True) print('SRV ' + 're-advertising for the bonded central') else: b.gap_scan(0, 30000, 30000) while not tgt: time.sleep_ms(50) b.gap_scan(None) time.sleep_ms(50) b.gap_connect(tgt[0][0], tgt[0][1]) while not cn: time.sleep_ms(50) time.sleep_ms(2000) b.gap_pair(cn[0]) while not enc: time.sleep_ms(50) time.sleep_ms(1500) print('CLI ' + 'bonded; disconnecting') b.gap_disconnect(cn[0]) while not dis: time.sleep_ms(50) time.sleep_ms(500) print('CLI ' + 'reconnecting to the stored address') b.gap_connect(tgt[0][0], tgt[0][1]) while len(cn) < 2: time.sleep_ms(50) time.sleep_ms(2000) b.gap_pair(cn[1]) while len(enc) < 2: time.sleep_ms(50) print('CLI ' + 'rebonded with NO pairing exchange - the bond held')
Experimente
- Acompanhe a história na serial até a última linha: 'rebonded with NO pairing exchange - the bond held'.
- No Wi-Fi sniffer, compare as duas conexões: o SMP aparece na primeira e está ausente na segunda.
- Procure 'SRV re-advertising for the bonded central' entre a desconexão e a reconexão.
- É por isso que seus fones se reconectam sem avisar: a LTK sobreviveu à desconexão.



