@@ -33,9 +33,9 @@ static volatile BTState bluetoothState = BT_OFF;
3333
3434#include < BleBatteryService.h>
3535
36- BTSerialInterface *bluetoothSerialSpp;
37- BTSerialInterface *bluetoothSerialBle;
38- BTSerialInterface *bluetoothSerialBleCommands; // Second BLE serial for CLI interface to mobile app
36+ BTSerialInterface *bluetoothSerialSpp = nullptr ;
37+ BTSerialInterface *bluetoothSerialBle = nullptr ;
38+ BTSerialInterface *bluetoothSerialBleCommands = nullptr ; // Second BLE serial for CLI interface to mobile app
3939BleBatteryService bluetoothBatteryService;
4040
4141#define BLE_SERVICE_UUID " 6e400001-b5a3-f393-e0a9-e50e24dcca9e"
@@ -174,6 +174,9 @@ byte bluetoothGetState()
174174int bluetoothRead (uint8_t *buffer, int length)
175175{
176176#ifdef COMPILE_BT
177+ if (bluetoothGetState () == BT_OFF)
178+ return 0 ;
179+
177180 if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
178181 {
179182 int bytesRead = 0 ;
@@ -223,6 +226,9 @@ int bluetoothCommandRead(uint8_t *buffer, int length)
223226uint8_t bluetoothRead ()
224227{
225228#ifdef COMPILE_BT
229+ if (bluetoothGetState () == BT_OFF)
230+ return 0 ;
231+
226232 if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
227233 {
228234 // Give incoming BLE the priority
@@ -261,6 +267,9 @@ uint8_t bluetoothCommandRead()
261267int bluetoothRxDataAvailable ()
262268{
263269#ifdef COMPILE_BT
270+ if (bluetoothGetState () == BT_OFF)
271+ return 0 ;
272+
264273 if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
265274 {
266275 // Give incoming BLE the priority
@@ -299,6 +308,9 @@ int bluetoothCommandAvailable()
299308int bluetoothWrite (const uint8_t *buffer, int length)
300309{
301310#ifdef COMPILE_BT
311+ if (bluetoothGetState () == BT_OFF)
312+ return 0 ;
313+
302314 if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
303315 {
304316 // Write to both interfaces
@@ -365,6 +377,9 @@ int bluetoothCommandWrite(const uint8_t *buffer, int length)
365377int bluetoothWrite (uint8_t value)
366378{
367379#ifdef COMPILE_BT
380+ if (bluetoothGetState () == BT_OFF)
381+ return 0 ;
382+
368383 if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
369384 {
370385 // Write to both interfaces
@@ -399,6 +414,9 @@ int bluetoothWrite(uint8_t value)
399414void bluetoothFlush ()
400415{
401416#ifdef COMPILE_BT
417+ if (bluetoothGetState () == BT_OFF)
418+ return ;
419+
402420 if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
403421 {
404422 bluetoothSerialBle->flush ();
@@ -420,33 +438,30 @@ void bluetoothFlush()
420438}
421439
422440void BTConfirmRequestCallback (uint32_t numVal) {
441+ if (bluetoothGetState () == BT_OFF)
442+ return ;
443+
423444 systemPrintf (" Device sent PIN: %06lu. Sending confirmation\r\n " , numVal);
424445 bluetoothSerialSpp->confirmReply (true ); // AUTO_PAIR - equivalent to enableSSP(false, true);
425446 // TODO: if the RTK device has an OLED, we should display the PIN so user can confirm
426447}
427448
428- // Get MAC, start radio
429- // Tack device's MAC address to end of friendly broadcast name
430- // This allows multiple units to be on at same time
449+ // Begin Bluetooth with a broadcast name of 'SparkFun Postcard-XXXX' or 'SparkPNT Facet mosaicX5-XXXX'
450+ // Add 4 characters of device's MAC address to end of the broadcast name
451+ // This allows users to discern between multiple devices in the local area
431452void bluetoothStart ()
432453{
433454 if (settings.bluetoothRadioType == BLUETOOTH_RADIO_OFF)
434455 return ;
435456
436- #ifdef COMPILE_BT
437- if (!online.bluetooth )
457+ if (online.bluetooth )
438458 {
459+ return ;
460+ }
461+
462+ #ifdef COMPILE_BT
463+ { // Maintain the indentation for now. TODO: delete the braces and correct indentation
439464 bluetoothState = BT_OFF;
440- char stateName[11 ] = {0 };
441- if (inRoverMode () == true )
442- strncpy (stateName, " Rover-" , sizeof (stateName) - 1 );
443- else if (inBaseMode () == true )
444- strncpy (stateName, " Base-" , sizeof (stateName) - 1 );
445- else
446- {
447- strncpy (stateName, " Rover-" , sizeof (stateName) - 1 );
448- log_d (" State out of range for Bluetooth Broadcast: %d" , systemState);
449- }
450465
451466 char productName[50 ] = {0 };
452467 strncpy (productName, platformPrefix, sizeof (productName));
@@ -455,41 +470,56 @@ void bluetoothStart()
455470 if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE ||
456471 settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
457472 {
458- if (strcmp (productName, " Facet L-Band Direct" ) == 0 )
473+ // Longest platform prefix is currently "Facet mosaicX5". We are just OK.
474+ if (strcmp (productName, " LONG PLATFORM PREFIX" ) == 0 )
459475 {
460- strncpy (productName, " Facet L-Band " , sizeof (productName));
476+ strncpy (productName, " SHORTER PREFIX " , sizeof (productName));
461477 }
462478 }
463479
464- snprintf (deviceName, sizeof (deviceName), " %s %s%02X%02X" , productName, stateName, btMACAddress[4 ],
480+ RTKBrandAttribute *brandAttributes = getBrandAttributeFromBrand (present.brand );
481+
482+ snprintf (deviceName, sizeof (deviceName), " %s %s-%02X%02X" , brandAttributes->name , productName, btMACAddress[4 ],
465483 btMACAddress[5 ]);
466484
467- if (strlen (deviceName) > 28 )
485+ if (strlen (deviceName) > 28 ) // "SparkPNT Facet mosaicX5-ABCD" = 28 chars. We are just OK
468486 {
469- if (ENABLE_DEVELOPER)
470- systemPrintf (
471- " Warning! The Bluetooth device name '%s' is %d characters long. It may not work in BLE mode.\r\n " ,
472- deviceName, strlen (deviceName));
487+ // BLE will fail quietly if broadcast name is more than 28 characters
488+ systemPrintf (
489+ " ERROR! The Bluetooth device name \" %s\" is %d characters long. It will not work in BLE mode.\r\n " ,
490+ deviceName, strlen (deviceName));
491+ reportFatalError (" Bluetooth device name is longer than 28 characters." );
473492 }
474493
475494 // Select Bluetooth setup
476495 if (settings.bluetoothRadioType == BLUETOOTH_RADIO_OFF)
477496 return ;
478497 else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
479498 {
480- bluetoothSerialSpp = new BTClassicSerial ();
481- bluetoothSerialBle = new BTLESerial ();
482- bluetoothSerialBleCommands = new BTLESerial ();
499+ if (bluetoothSerialSpp == nullptr )
500+ bluetoothSerialSpp = new BTClassicSerial ();
501+ if (bluetoothSerialBle == nullptr )
502+ bluetoothSerialBle = new BTLESerial ();
503+ if (bluetoothSerialBleCommands == nullptr )
504+ bluetoothSerialBleCommands = new BTLESerial ();
483505 }
484506 else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP)
485- bluetoothSerialSpp = new BTClassicSerial ();
507+ {
508+ if (bluetoothSerialSpp == nullptr )
509+ bluetoothSerialSpp = new BTClassicSerial ();
510+ }
486511 else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
487512 {
488- bluetoothSerialBle = new BTLESerial ();
489- bluetoothSerialBleCommands = new BTLESerial ();
513+ if (bluetoothSerialBle == nullptr )
514+ bluetoothSerialBle = new BTLESerial ();
515+ if (bluetoothSerialBleCommands == nullptr )
516+ bluetoothSerialBleCommands = new BTLESerial ();
490517 }
491518 else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_ACCESSORY_MODE)
492- bluetoothSerialSpp = new BTClassicSerial ();
519+ {
520+ if (bluetoothSerialSpp == nullptr )
521+ bluetoothSerialSpp = new BTClassicSerial ();
522+ }
493523
494524 // Not yet implemented
495525 // if (pinBluetoothTaskHandle == nullptr)
@@ -692,7 +722,7 @@ void bluetoothStart()
692722 bluetoothState = BT_NOTCONNECTED;
693723 reportHeapNow (false );
694724 online.bluetooth = true ;
695- }
725+ } // if (1)
696726#endif // COMPILE_BT
697727}
698728
@@ -721,46 +751,69 @@ void bluetoothStop()
721751#ifdef COMPILE_BT
722752 if (online.bluetooth )
723753 {
754+ if (settings.debugNetworkLayer )
755+ systemPrintln (" Bluetooth turning off" );
756+
757+ bluetoothState = BT_OFF; // Indicate to tasks that BT is unavailable
758+
724759 if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
725760 {
726761 bluetoothSerialBle->flush (); // Complete any transfers
727762 bluetoothSerialBle->disconnect (); // Drop any clients
728763 bluetoothSerialBle->end (); // Release resources
764+ delete bluetoothSerialBle;
765+ bluetoothSerialBle = nullptr ;
729766
730767 bluetoothSerialBleCommands->flush (); // Complete any transfers
731768 bluetoothSerialBleCommands->disconnect (); // Drop any clients
732769 bluetoothSerialBleCommands->end (); // Release resources
770+ delete bluetoothSerialBleCommands;
771+ bluetoothSerialBleCommands = nullptr ;
733772
734773 bluetoothSerialSpp->flush (); // Complete any transfers
735774 bluetoothSerialSpp->disconnect (); // Drop any clients
736775 bluetoothSerialSpp->end (); // Release resources
776+ delete bluetoothSerialSpp;
777+ bluetoothSerialSpp = nullptr ;
778+
779+ bluetoothBatteryService.end ();
737780 }
738781 else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP)
739782 {
740783 bluetoothSerialSpp->flush (); // Complete any transfers
741784 bluetoothSerialSpp->disconnect (); // Drop any clients
742785 bluetoothSerialSpp->end (); // Release resources
786+ delete bluetoothSerialSpp;
787+ bluetoothSerialSpp = nullptr ;
743788 }
744789 else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
745790 {
746791 bluetoothSerialBle->flush (); // Complete any transfers
747792 bluetoothSerialBle->disconnect (); // Drop any clients
748793 bluetoothSerialBle->end (); // Release resources
794+ delete bluetoothSerialBle;
795+ bluetoothSerialBle = nullptr ;
749796
750797 bluetoothSerialBleCommands->flush (); // Complete any transfers
751798 bluetoothSerialBleCommands->disconnect (); // Drop any clients
752799 bluetoothSerialBleCommands->end (); // Release resources
800+ delete bluetoothSerialBleCommands;
801+ bluetoothSerialBleCommands = nullptr ;
802+
803+ bluetoothBatteryService.end ();
753804 }
754805 else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_ACCESSORY_MODE)
755806 {
756807 bluetoothSerialSpp->flush (); // Complete any transfers
757808 bluetoothSerialSpp->disconnect (); // Drop any clients
758809 bluetoothSerialSpp->end (); // Release resources
810+ delete bluetoothSerialSpp;
811+ bluetoothSerialSpp = nullptr ;
759812 }
760813
761- log_d (" Bluetooth turned off" );
814+ if (settings.debugNetworkLayer )
815+ systemPrintln (" Bluetooth turned off" );
762816
763- bluetoothState = BT_OFF;
764817 reportHeapNow (false );
765818 online.bluetooth = false ;
766819 }
@@ -805,6 +858,13 @@ void bluetoothPrintStatus()
805858void bluetoothSendBatteryPercent (int batteryLevelPercent)
806859{
807860#ifdef COMPILE_BT
861+ if (bluetoothGetState () == BT_OFF)
862+ return ;
863+
864+ if ((settings.bluetoothRadioType != BLUETOOTH_RADIO_SPP_AND_BLE) &&
865+ (settings.bluetoothRadioType != BLUETOOTH_RADIO_BLE))
866+ return ;
867+
808868 bluetoothBatteryService.reportBatteryPercent (batteryLevelPercent);
809869#endif // COMPILE_BT
810870}
0 commit comments