4141STATIC conn_state_t conn_state ;
4242osMutexId_t bluetooth_connection_mutex_id ;
4343bleio_adapter_obj_t common_hal_bleio_adapter_obj ;
44+ uint8_t ble_bonding_handle = 0xFF ;
4445
4546__ALIGNED (4 ) static uint8_t bluetooth_connection_mutex_cb [osMutexCbSize ];
4647const osMutexAttr_t bluetooth_connection_mutex_attr = {
@@ -106,7 +107,6 @@ void common_hal_bleio_check_connected(uint16_t conn_handle) {
106107
107108// Bluetooth stack event handler.
108109void sl_bt_on_event (sl_bt_msg_t * evt ) {
109- sl_status_t sc = SL_STATUS_OK ;
110110 bd_addr address ;
111111 uint8_t address_type = 0 ;
112112 STATIC uint8_t serv_idx = 0 ;
@@ -122,23 +122,18 @@ void sl_bt_on_event(sl_bt_msg_t *evt) {
122122
123123 case sl_bt_evt_system_boot_id :
124124
125- sc = sl_bt_system_get_identity_address (& address , & address_type );
125+ sl_bt_system_get_identity_address (& address , & address_type );
126126
127127 snprintf ((char * )device_name , 14 + 1 ,
128128 "CIRCUITPY-%X%X" ,address .addr [1 ], address .addr [0 ]);
129129 sl_bt_gatt_server_write_attribute_value (gattdb_device_name ,
130130 0 ,14 ,device_name );
131131
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 );
136133
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 );
142137 break ;
143138
144139 // This event indicates that a new connection was opened.
@@ -154,14 +149,7 @@ void sl_bt_on_event(sl_bt_msg_t *evt) {
154149 bleio_connections [0 ].is_central = false;
155150 bleio_connections [0 ].mtu = 0 ;
156151 }
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 ;
165153 osMutexRelease (bluetooth_connection_mutex_id );
166154 break ;
167155
@@ -181,6 +169,8 @@ void sl_bt_on_event(sl_bt_msg_t *evt) {
181169 case sl_bt_evt_connection_closed_id :
182170 common_hal_bleio_adapter_remove_connection (
183171 evt -> data .evt_connection_closed .connection );
172+ // reset bonding handle variable to avoid deleting wrong bonding info
173+ ble_bonding_handle = 0xFF ;
184174 break ;
185175
186176 case sl_bt_evt_system_external_signal_id :
@@ -306,7 +296,7 @@ void sl_bt_on_event(sl_bt_msg_t *evt) {
306296 && serv_idx < connection -> remote_service_list -> len ) {
307297
308298 service = connection -> remote_service_list -> items [serv_idx ];
309- sc = sl_bt_gatt_discover_characteristics (
299+ sl_bt_gatt_discover_characteristics (
310300 evt -> data .evt_gatt_procedure_completed .connection ,
311301 service -> handle );
312302
@@ -366,34 +356,20 @@ void sl_bt_on_event(sl_bt_msg_t *evt) {
366356 break ;
367357
368358 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 );
375360 break ;
376361
377362 case sl_bt_evt_sm_bonded_id :
378363 break ;
379364
380365 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+ }
381370 break ;
382371
383372 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- }
397373 break ;
398374
399375 default :
0 commit comments