Skip to content

Commit 4eee626

Browse files
committed
aioble/security: Add DeviceConnection.pairing_in_progress flag.
Will be True if the pairing process has been started but is waiting for ack from remote device.
1 parent cdd260f commit 4eee626

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

micropython/bluetooth/aioble/aioble/device.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,15 @@ class DeviceConnection:
151151
_connected = {}
152152

153153
def __init__(self, device):
154-
self.device = device
154+
self.device: Device = device
155155
device._connection = self
156156

157157
self.encrypted = False
158158
self.authenticated = False
159159
self.bonded = False
160160
self.key_size = False
161161
self.mtu = None
162+
self.pairing_in_progress = False
162163

163164
self._conn_handle = None
164165

micropython/bluetooth/aioble/aioble/security.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import json
88

99
from .core import log_info, log_warn, ble, register_irq_handler
10-
from .device import DeviceConnection
10+
from .device import DeviceConnection, Device
1111

1212
_IRQ_ENCRYPTION_UPDATE = const(28)
1313
_IRQ_GET_SECRET = const(29)
@@ -72,6 +72,15 @@ def _save_secrets(arg=None):
7272
_modified = False
7373

7474

75+
def _get_connection(key) -> DeviceConnection:
76+
if not key:
77+
return None
78+
addr = bytes(reversed(key[-6:]))
79+
for connection in DeviceConnection._connected.values():
80+
if connection.device.addr == addr:
81+
return connection
82+
83+
7584
def _security_irq(event, data):
7685
global _modified
7786

@@ -84,6 +93,7 @@ def _security_irq(event, data):
8493
connection.authenticated = authenticated
8594
connection.bonded = bonded
8695
connection.key_size = key_size
96+
connection.pairing_in_progress = False
8797
# TODO: Handle failure.
8898
if encrypted and connection._pair_event:
8999
connection._pair_event.set()
@@ -128,6 +138,11 @@ def _security_irq(event, data):
128138
else:
129139
# Return the secret for this key (or None).
130140
key = sec_type, bytes(key)
141+
142+
if conn := _get_connection(key):
143+
log_info("encryption / pairing started", conn)
144+
conn.pairing_in_progress = True
145+
131146
return _secrets.get(key, None)
132147

133148
elif event == _IRQ_PASSKEY_ACTION:

0 commit comments

Comments
 (0)