@@ -118,6 +118,7 @@ static uint16_t conn_mtu;
118118
119119// Bonding status of the peripheral.
120120static uint16_t bond_auth_err = NO_AUTH ;
121+ static uint8_t bond_auth_mode_last = GAPBOND_PAIRING_MODE_NO_PAIRING ;
121122
122123// GATT service handles
123124static uint16_t gatt_service_handle , gatt_service_end_handle ;
@@ -241,6 +242,15 @@ pbio_error_t pbdrv_bluetooth_start_advertising_func(pbio_os_state_t *state, void
241242
242243 DEBUG_PRINT ("set_discoverable\n" );
243244
245+ // If we were set to initiate pairing because we bonded with a peripheral
246+ // previously, unset it to avoid trying to bond with a PC host on connect.
247+ if (bond_auth_mode_last != GAPBOND_PAIRING_MODE_NO_PAIRING ) {
248+ bond_auth_mode_last = GAPBOND_PAIRING_MODE_NO_PAIRING ;
249+ PBIO_OS_AWAIT_WHILE (state , write_xfer_size );
250+ GAP_BondMgrSetParameter (GAPBOND_PAIRING_MODE , sizeof (bond_auth_mode_last ), & bond_auth_mode_last );
251+ PBIO_OS_AWAIT_UNTIL (state , hci_command_status );
252+ }
253+
244254 static int8_t tx_power ;
245255 PBIO_OS_AWAIT_WHILE (state , write_xfer_size );
246256 HCI_LE_readAdvertisingChannelTxPower ();
@@ -366,8 +376,6 @@ pbio_error_t pbdrv_bluetooth_send_pybricks_value_notification(pbio_os_state_t *s
366376pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect_func (pbio_os_state_t * state , void * context ) {
367377 pbdrv_bluetooth_peripheral_t * peri = context ;
368378
369- static uint8_t buf [1 ];
370-
371379 static pbio_os_timer_t peripheral_scan_restart_timer ;
372380
373381 // Scan and connect timeout, if applicable.
@@ -516,14 +524,12 @@ pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect_func(pbio_os_state_t *s
516524
517525 // Configure to initiate pairing right after connect if bonding required.
518526 // NB: We must unset "initiate" before we allow a new connection to
519- // Pybricks Code or it will try to pair with the PC. However, this happens
520- // automatically since gap_init runs again on disconnect, which resets it.
521- // It won't unset automatically if Pybricks Code is already connected, but
522- // then it doesn't matter since we're already connected.
527+ // Pybricks Code or it will try to pair with the PC. We do this just before
528+ // starting to advertise again, which covers all our use cases.
523529 PBIO_OS_AWAIT_WHILE (state , write_xfer_size );
524- buf [ 0 ] = (peri -> config -> options & PBDRV_BLUETOOTH_PERIPHERAL_OPTIONS_PAIR ) ?
530+ bond_auth_mode_last = (peri -> config -> options & PBDRV_BLUETOOTH_PERIPHERAL_OPTIONS_PAIR ) ?
525531 GAPBOND_PAIRING_MODE_INITIATE : GAPBOND_PAIRING_MODE_NO_PAIRING ;
526- GAP_BondMgrSetParameter (GAPBOND_PAIRING_MODE , 1 , buf );
532+ GAP_BondMgrSetParameter (GAPBOND_PAIRING_MODE , sizeof ( bond_auth_mode_last ), & bond_auth_mode_last );
527533 PBIO_OS_AWAIT_UNTIL (state , hci_command_status );
528534
529535 PBIO_OS_AWAIT_WHILE (state , write_xfer_size );
@@ -1571,7 +1577,7 @@ static const struct {
15711577 {GAPBOND_PAIRING_MODE , GAPBOND_PAIRING_MODE_NO_PAIRING },
15721578 {GAPBOND_MITM_PROTECTION , 0 }, // disabled
15731579 {GAPBOND_IO_CAPABILITIES , GAPBOND_IO_CAP_NO_INPUT_NO_OUTPUT },
1574- {GAPBOND_BONDING_ENABLED , 1 }, // // enabled, as in allowed. It won't happen by default.
1580+ {GAPBOND_BONDING_ENABLED , 1 }, // enabled, as in allowed. It won't happen by default.
15751581 {GAPBOND_SECURE_CONNECTION , GAPBOND_SECURE_CONNECTION_NONE },
15761582};
15771583
0 commit comments