Skip to content

Commit 60efba8

Browse files
committed
Restructure mmSetBluetoothProtocol
Restart ESP32 if needed - when changing mode
1 parent e3db394 commit 60efba8

File tree

1 file changed

+63
-2
lines changed

1 file changed

+63
-2
lines changed

Firmware/RTK_Everywhere/menuMain.ino

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,12 +579,73 @@ BluetoothRadioType_e mmChangeBluetoothProtocol(BluetoothRadioType_e bluetoothUse
579579
return bluetoothUserChoice;
580580
}
581581

582-
// Restart Bluetooth radio if settings have changed
582+
// Update Bluetooth radio if settings have changed
583583
void mmSetBluetoothProtocol(BluetoothRadioType_e bluetoothUserChoice, bool clearBtPairings)
584584
{
585585
if ((bluetoothUserChoice != settings.bluetoothRadioType)
586586
|| (clearBtPairings != settings.clearBtPairings))
587587
{
588+
// To avoid connection failures, we may need to restart the ESP32
589+
590+
// If Bluetooth was on, and the user has selected OFF, then just stop
591+
if ((settings.bluetoothRadioType != BLUETOOTH_RADIO_OFF)
592+
&& (bluetoothUserChoice == BLUETOOTH_RADIO_OFF))
593+
{
594+
bluetoothStop();
595+
settings.bluetoothRadioType = bluetoothUserChoice;
596+
settings.clearBtPairings = clearBtPairings;
597+
return;
598+
}
599+
// If Bluetooth was off, and the user has selected on, and Bluetooth has not been started previously
600+
// then just start
601+
else if ((settings.bluetoothRadioType == BLUETOOTH_RADIO_OFF)
602+
&& (bluetoothUserChoice != BLUETOOTH_RADIO_OFF)
603+
&& (bluetoothRadioPreviousOnType == BLUETOOTH_RADIO_OFF))
604+
{
605+
bluetoothStart();
606+
settings.bluetoothRadioType = bluetoothUserChoice;
607+
settings.clearBtPairings = clearBtPairings;
608+
return;
609+
}
610+
// If Bluetooth was off, and the user has selected on, and Bluetooth has been started previously
611+
// then restart
612+
else if ((settings.bluetoothRadioType == BLUETOOTH_RADIO_OFF)
613+
&& (bluetoothUserChoice != BLUETOOTH_RADIO_OFF)
614+
&& (bluetoothRadioPreviousOnType != BLUETOOTH_RADIO_OFF))
615+
{
616+
settings.bluetoothRadioType = bluetoothUserChoice;
617+
settings.clearBtPairings = clearBtPairings;
618+
recordSystemSettings();
619+
systemPrintln("Rebooting to apply new Bluetooth choice. Goodbye!");
620+
delay(1000);
621+
ESP.restart();
622+
return;
623+
}
624+
// If Bluetooth was in Accessory Mode, and still is, and clearBtPairings is true
625+
// then restart skipping the online check
626+
else if ((settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_ACCESSORY_MODE)
627+
&& (bluetoothUserChoice == BLUETOOTH_RADIO_SPP_ACCESSORY_MODE)
628+
&& clearBtPairings)
629+
{
630+
bluetoothStartSkipOnlineCheck();
631+
return;
632+
}
633+
// If Bluetooth was on, and the user has selected a different mode
634+
// then restart
635+
else if ((settings.bluetoothRadioType != BLUETOOTH_RADIO_OFF)
636+
&& (bluetoothUserChoice != settings.bluetoothRadioType))
637+
{
638+
settings.bluetoothRadioType = bluetoothUserChoice;
639+
settings.clearBtPairings = clearBtPairings;
640+
recordSystemSettings();
641+
systemPrintln("Rebooting to apply new Bluetooth choice. Goodbye!");
642+
delay(1000);
643+
ESP.restart();
644+
return;
645+
}
646+
// <--- Insert any new special cases here --->
647+
648+
// Previous catch-all. Likely to cause connection failures...
588649
bluetoothStop();
589650
settings.bluetoothRadioType = bluetoothUserChoice;
590651
settings.clearBtPairings = clearBtPairings;
@@ -838,7 +899,7 @@ void menuRadio()
838899

839900
wifiEspNowOn(__FILE__, __LINE__); // Turn on the hardware if settings.enableEspNow is true
840901

841-
// Restart Bluetooth radio if settings have changed
902+
// Update Bluetooth radio if settings have changed
842903
mmSetBluetoothProtocol(bluetoothUserChoice, clearBtPairings);
843904

844905
// LoRa radio state machine will start/stop radio upon next updateLora in loop()

0 commit comments

Comments
 (0)