@@ -159,6 +159,7 @@ public class RadioAudioService extends Service {
159159 private static final byte COMMAND_DEBUG_WARN = 0x03 ;
160160 private static final byte COMMAND_DEBUG_DEBUG = 0x04 ;
161161 private static final byte COMMAND_DEBUG_TRACE = 0x05 ;
162+ private static final byte COMMAND_HELLO = 0x06 ;
162163
163164 private final ESP32DataStreamParser esp32DataStreamParser = new ESP32DataStreamParser (this ::handleParsedCommand );
164165
@@ -209,6 +210,7 @@ public class RadioAudioService extends Service {
209210 private String radioType = RADIO_MODULE_VHF ;
210211 private boolean radioModuleNotFound = false ;
211212 private boolean checkedFirmwareVersion = false ;
213+ private boolean gotHello = false ;
212214
213215 // Safety constants
214216 private static int RUNAWAY_TX_TIMEOUT_SEC = 180 ; // Stop runaway tx after 3 minutes
@@ -939,6 +941,9 @@ public void onNewData(byte[] data) {
939941 @ Override
940942 public void onRunError (Exception e ) {
941943 Log .d ("DEBUG" , "Error reading from ESP32." );
944+ if (audioTrack != null ) {
945+ audioTrack .stop ();
946+ }
942947 connection .close ();
943948 try {
944949 serialPort .close ();
@@ -958,19 +963,17 @@ public void onRunError(Exception e) {
958963 usbIoManager .setReadBufferCount (16 *2 );
959964 usbIoManager .start ();
960965 checkedFirmwareVersion = false ;
966+ gotHello = false ;
961967
962968 Log .d ("DEBUG" , "Connected to ESP32." );
963969
964- // After a brief pause (to let it boot), do things with the ESP32 that we were waiting to do.
965- final Handler handler = new Handler (Looper .getMainLooper ());
966- handler .postDelayed (new Runnable () {
967- @ Override
968- public void run () {
969- if (!checkedFirmwareVersion ) {
970- checkFirmwareVersion ();
971- }
970+ new Handler (Looper .getMainLooper ()).postDelayed (() -> {
971+ if (!gotHello ) {
972+ Log .d ("DEBUG" , "Error: Did not HELLO from module." );
973+ callbacks .missingFirmware ();
974+ setMode (MODE_BAD_FIRMWARE );
972975 }
973- }, 3000 );
976+ }, 500 );
974977 }
975978
976979 /**
@@ -1021,17 +1024,11 @@ private void checkFirmwareVersion() {
10211024
10221025 // If we don't hear back from the ESP32, it means the firmware is either not
10231026 // installed or it's somehow corrupt.
1024- final Handler handler = new Handler (Looper .getMainLooper ());
1025- handler .postDelayed (new Runnable () {
1026- @ Override
1027- public void run () {
1028- if (mode != MODE_STARTUP || radioModuleNotFound ) {
1029- return ;
1030- } else {
1031- Log .d ("DEBUG" , "Error: Did not hear back from ESP32 after requesting its firmware version. Offering to flash." );
1032- callbacks .missingFirmware ();
1033- setMode (MODE_BAD_FIRMWARE );
1034- }
1027+ new Handler (Looper .getMainLooper ()).postDelayed (() -> {
1028+ if (mode == MODE_STARTUP && !radioModuleNotFound ) {
1029+ Log .d ("DEBUG" , "Error: Did not hear back from ESP32 after requesting its firmware version. Offering to flash." );
1030+ callbacks .missingFirmware ();
1031+ setMode (MODE_BAD_FIRMWARE );
10351032 }
10361033 }, 6000 );
10371034 }
@@ -1461,6 +1458,11 @@ private void handleParsedCommand(byte cmd, byte[] param) {
14611458 Log .w ("firmware" , new String (param ));
14621459 } else if (cmd == COMMAND_DEBUG_TRACE ) {
14631460 Log .v ("firmware" , new String (param ));
1461+ } else if (cmd == COMMAND_HELLO ) {
1462+ gotHello = true ;
1463+ audioTrack .stop ();
1464+ restartAudioPrebuffer ();
1465+ checkFirmwareVersion ();
14641466 } else {
14651467 Log .d ("DEBUG" , "Unknown cmd received from ESP32: 0x" + Integer .toHexString (cmd & 0xFF ) +
14661468 " paramLen=" + param .length );
0 commit comments