@@ -72,6 +72,14 @@ void bluetoothUpdate()
7272 // LED is controlled by tickerBluetoothLedUpdate()
7373
7474 btPrintEchoExit = false ; // Reset the exiting of config menus and/or command modes
75+
76+ #ifdef COMPILE_AUTHENTICATION
77+ if (sendAccessoryHandshakeOnBtConnect)
78+ {
79+ appleAccessory->startHandshake ((Stream *)bluetoothSerialSpp);
80+ sendAccessoryHandshakeOnBtConnect = false ; // One-shot
81+ }
82+ #endif
7583 }
7684
7785 else if ((bluetoothState == BT_CONNECTED) && (!bluetoothIsConnected ()))
@@ -411,6 +419,12 @@ void bluetoothFlush()
411419#endif // COMPILE_BT
412420}
413421
422+ void BTConfirmRequestCallback (uint32_t numVal) {
423+ systemPrintf (" Device sent PIN: %06lu. Sending confirmation\r\n " , numVal);
424+ bluetoothSerialSpp->confirmReply (true ); // AUTO_PAIR - equivalent to enableSSP(false, true);
425+ // TODO: if the RTK device has an OLED, we should display the PIN so user can confirm
426+ }
427+
414428// Get MAC, start radio
415429// Tack device's MAC address to end of friendly broadcast name
416430// This allows multiple units to be on at same time
@@ -533,10 +547,23 @@ void bluetoothStart()
533547 }
534548 else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_ACCESSORY_MODE)
535549 {
536- // Support Apple Accessory
537-
538- bluetoothSerialSpp->enableSSP (false ,
539- false ); // Enable secure pairing, authenticate without displaying anything
550+ // Uncomment the next line to force deletion of all paired (bonded) devices
551+ // (This should only be necessary if you have changed the SSP pairing type)
552+ // settings.clearBtPairings = true;
553+
554+ // Enable secure pairing without PIN :
555+ // iPhone displays Connection Unsuccessful - but then connects anyway...
556+ bluetoothSerialSpp->enableSSP (false , false );
557+
558+ // Enable secure pairing with PIN :
559+ // bluetoothSerialSpp->enableSSP(false, true);
560+
561+ // Accessory Protocol recommends using a PIN
562+ // Support Apple Accessory: Device to Accessory
563+ // 1. Search for an accessory from the device and initiate pairing.
564+ // 2. Verify pairing is successful after exchanging a pin code.
565+ // bluetoothSerialSpp->enableSSP(true, true); // Enable secure pairing with PIN
566+ // bluetoothSerialSpp->onConfirmRequest(&BTConfirmRequestCallback); // Callback to verify the PIN
540567
541568 beginSuccess &= bluetoothSerialSpp->begin (
542569 deviceName, true , true , settings.sppRxQueueSize , settings.sppTxQueueSize , 0 , 0 ,
@@ -546,16 +573,32 @@ void bluetoothStart()
546573 {
547574 // bluetoothSerialSpp.getBtAddress(btMACAddress); // Read the ESP32 BT MAC Address
548575
576+ if (settings.clearBtPairings )
577+ {
578+ // Paired / bonded devices are stored in flash. Only a full flash erase
579+ // or deleteAllBondedDevices() will clear them all. They can be deleted
580+ // individually, but that would need a menu and more functions added to
581+ // the BT classes.
582+ // Deleting all bonded devices after a factory reset seems sensible.
583+ // TODO: test all the possibilities / overlap of this and "Forget Device"
584+ if (settings.debugNetworkLayer )
585+ systemPrintln (" Deleting all bonded devices" );
586+ bluetoothSerialSpp->deleteAllBondedDevices (); // Must be called after begin
587+ settings.clearBtPairings = false ;
588+ recordSystemSettings ();
589+ }
590+
549591 esp_sdp_init ();
550592
551593 esp_bluetooth_sdp_hdr_overlay_t record = {(esp_bluetooth_sdp_types_t )0 };
552594 record.type = ESP_SDP_TYPE_RAW;
553595 record.uuid .len = sizeof (UUID_IAP2);
554596 memcpy (record.uuid .uuid .uuid128 , UUID_IAP2, sizeof (UUID_IAP2));
555- record.service_name_length = strlen (sdp_service_name) + 1 ;
556- record.service_name = (char *)sdp_service_name;
557- // record.service_name_length = strlen(deviceName) + 1; // Doesn't seem to help the failed connects
558- // record.service_name = (char *)deviceName;
597+ // record.service_name_length = strlen(sdp_service_name) + 1;
598+ // record.service_name = (char *)sdp_service_name;
599+ // Use the same EIR Local Name parameter as the Name in the IdentificationInformation
600+ record.service_name_length = strlen (deviceName) + 1 ;
601+ record.service_name = (char *)deviceName;
559602 // record.rfcomm_channel_number = 1; // Doesn't seem to help the failed connects
560603 esp_sdp_create_record ((esp_bluetooth_sdp_record_t *)&record);
561604 }
0 commit comments