41
41
STATIC conn_state_t conn_state ;
42
42
osMutexId_t bluetooth_connection_mutex_id ;
43
43
bleio_adapter_obj_t common_hal_bleio_adapter_obj ;
44
+ uint8_t ble_bonding_handle = 0xFF ;
44
45
45
46
__ALIGNED (4 ) static uint8_t bluetooth_connection_mutex_cb [osMutexCbSize ];
46
47
const osMutexAttr_t bluetooth_connection_mutex_attr = {
@@ -106,7 +107,6 @@ void common_hal_bleio_check_connected(uint16_t conn_handle) {
106
107
107
108
// Bluetooth stack event handler.
108
109
void sl_bt_on_event (sl_bt_msg_t * evt ) {
109
- sl_status_t sc = SL_STATUS_OK ;
110
110
bd_addr address ;
111
111
uint8_t address_type = 0 ;
112
112
STATIC uint8_t serv_idx = 0 ;
@@ -122,23 +122,18 @@ void sl_bt_on_event(sl_bt_msg_t *evt) {
122
122
123
123
case sl_bt_evt_system_boot_id :
124
124
125
- sc = sl_bt_system_get_identity_address (& address , & address_type );
125
+ sl_bt_system_get_identity_address (& address , & address_type );
126
126
127
127
snprintf ((char * )device_name , 14 + 1 ,
128
128
"CIRCUITPY-%X%X" ,address .addr [1 ], address .addr [0 ]);
129
129
sl_bt_gatt_server_write_attribute_value (gattdb_device_name ,
130
130
0 ,14 ,device_name );
131
131
132
- sc = sl_bt_sm_configure (0x00 ,sl_bt_sm_io_capability_noinputnooutput );
133
- if (SL_STATUS_OK != sc ) {
134
- mp_raise_bleio_BluetoothError (translate ("Sm configure fail" ));
135
- }
132
+ sl_bt_sm_store_bonding_configuration (5 ,2 );
136
133
137
- sc = sl_bt_sm_set_bondable_mode (1 );
138
- if (SL_STATUS_OK != sc ) {
139
- mp_raise_bleio_BluetoothError (translate ("Set bondable mode fail" ));
140
- }
141
- sl_bt_sm_delete_bondings ();
134
+ sl_bt_sm_configure (0x00 ,sl_bt_sm_io_capability_noinputnooutput );
135
+
136
+ sl_bt_sm_set_bondable_mode (1 );
142
137
break ;
143
138
144
139
// This event indicates that a new connection was opened.
@@ -154,14 +149,7 @@ void sl_bt_on_event(sl_bt_msg_t *evt) {
154
149
bleio_connections [0 ].is_central = false;
155
150
bleio_connections [0 ].mtu = 0 ;
156
151
}
157
-
158
- sc = sl_bt_sm_increase_security (
159
- evt -> data .evt_connection_opened .connection );
160
-
161
- if (SL_STATUS_OK != sc ) {
162
- mp_raise_bleio_BluetoothError (
163
- translate ("Increase security fail." ));
164
- }
152
+ ble_bonding_handle = evt -> data .evt_connection_opened .bonding ;
165
153
osMutexRelease (bluetooth_connection_mutex_id );
166
154
break ;
167
155
@@ -181,6 +169,8 @@ void sl_bt_on_event(sl_bt_msg_t *evt) {
181
169
case sl_bt_evt_connection_closed_id :
182
170
common_hal_bleio_adapter_remove_connection (
183
171
evt -> data .evt_connection_closed .connection );
172
+ // reset bonding handle variable to avoid deleting wrong bonding info
173
+ ble_bonding_handle = 0xFF ;
184
174
break ;
185
175
186
176
case sl_bt_evt_system_external_signal_id :
@@ -306,7 +296,7 @@ void sl_bt_on_event(sl_bt_msg_t *evt) {
306
296
&& serv_idx < connection -> remote_service_list -> len ) {
307
297
308
298
service = connection -> remote_service_list -> items [serv_idx ];
309
- sc = sl_bt_gatt_discover_characteristics (
299
+ sl_bt_gatt_discover_characteristics (
310
300
evt -> data .evt_gatt_procedure_completed .connection ,
311
301
service -> handle );
312
302
@@ -366,34 +356,20 @@ void sl_bt_on_event(sl_bt_msg_t *evt) {
366
356
break ;
367
357
368
358
case sl_bt_evt_sm_confirm_bonding_id :
369
- sc = sl_bt_sm_bonding_confirm (
370
- evt -> data .evt_sm_confirm_bonding .connection ,1 );
371
- if (SL_STATUS_OK != sc ) {
372
- mp_raise_bleio_BluetoothError (
373
- translate ("Bonding confirm fail" ));
374
- }
359
+ sl_bt_sm_bonding_confirm (evt -> data .evt_sm_confirm_bonding .connection ,1 );
375
360
break ;
376
361
377
362
case sl_bt_evt_sm_bonded_id :
378
363
break ;
379
364
380
365
case sl_bt_evt_sm_bonding_failed_id :
366
+ if (ble_bonding_handle != 0xFF ) {
367
+ sl_bt_sm_delete_bonding (ble_bonding_handle );
368
+ ble_bonding_handle = 0xFF ;
369
+ }
381
370
break ;
382
371
383
372
case sl_bt_evt_connection_parameters_id :
384
- switch (evt -> data .evt_connection_parameters .security_mode )
385
- {
386
- case connection_mode1_level1 :
387
- break ;
388
- case connection_mode1_level2 :
389
- break ;
390
- case connection_mode1_level3 :
391
- break ;
392
- case connection_mode1_level4 :
393
- break ;
394
- default :
395
- break ;
396
- }
397
373
break ;
398
374
399
375
default :
0 commit comments