|
| 1 | +diff --git a/node_modules/capacitor-plugin-usb-serial/android/src/main/java/com/viewtrak/plugins/usbserial/UsbSerial.java b/node_modules/capacitor-plugin-usb-serial/android/src/main/java/com/viewtrak/plugins/usbserial/UsbSerial.java |
| 2 | +index a366b4a..c094c1e 100644 |
| 3 | +--- a/node_modules/capacitor-plugin-usb-serial/android/src/main/java/com/viewtrak/plugins/usbserial/UsbSerial.java |
| 4 | ++++ b/node_modules/capacitor-plugin-usb-serial/android/src/main/java/com/viewtrak/plugins/usbserial/UsbSerial.java |
| 5 | +@@ -105,6 +105,7 @@ public class UsbSerial implements SerialInputOutputManager.Listener { |
| 6 | + |
| 7 | + @Override |
| 8 | + public void onNewData(byte[] data) { |
| 9 | ++ android.util.Log.d("UsbSerial", "onNewData called with " + data.length + " bytes"); |
| 10 | + mainLooper.post(() -> updateReceivedData(data)); |
| 11 | + } |
| 12 | + |
| 13 | +@@ -195,11 +196,15 @@ public class UsbSerial implements SerialInputOutputManager.Listener { |
| 14 | + } |
| 15 | + } |
| 16 | + usbSerialPort.open(usbConnection); |
| 17 | ++ android.util.Log.d("UsbSerial", "Serial port opened successfully"); |
| 18 | + usbSerialPort.setParameters(settings.baudRate, settings.dataBits, settings.stopBits, settings.parity); |
| 19 | ++ android.util.Log.d("UsbSerial", "Serial parameters set: baudRate=" + settings.baudRate); |
| 20 | + if (settings.dtr) usbSerialPort.setDTR(true); |
| 21 | + if (settings.rts) usbSerialPort.setRTS(true); |
| 22 | + usbIoManager = new SerialInputOutputManager(usbSerialPort, this); |
| 23 | ++ android.util.Log.d("UsbSerial", "SerialInputOutputManager created"); |
| 24 | + usbIoManager.start(); |
| 25 | ++ android.util.Log.d("UsbSerial", "SerialInputOutputManager started - should now receive data"); |
| 26 | + // connected = true; |
| 27 | + setConnectedDevice(device); |
| 28 | + } catch (Exception exception) { |
| 29 | +@@ -233,8 +238,12 @@ public class UsbSerial implements SerialInputOutputManager.Listener { |
| 30 | + throw new Error("can't send empty string to device", new Throwable("EMPTY_STRING")); |
| 31 | + } |
| 32 | + try { |
| 33 | +- byte[] data = (str + "\r\n").getBytes(); |
| 34 | ++ // Convert hex string to bytes (for binary protocols like MSP) |
| 35 | ++ android.util.Log.d("UsbSerial", "writeSerial: converting hex string (" + str.length() + " chars) to bytes"); |
| 36 | ++ byte[] data = HexDump.hexStringToByteArray(str); |
| 37 | ++ android.util.Log.d("UsbSerial", "writeSerial: writing " + data.length + " bytes"); |
| 38 | + usbSerialPort.write(data, WRITE_WAIT_MILLIS); |
| 39 | ++ android.util.Log.d("UsbSerial", "writeSerial: write successful"); |
| 40 | + } catch (Exception e) { |
| 41 | + closeSerial(); |
| 42 | + throw new Error("connection lost: " + e.getMessage(), e.getCause()); |
| 43 | +@@ -260,25 +269,13 @@ public class UsbSerial implements SerialInputOutputManager.Listener { |
| 44 | + |
| 45 | + private void updateReceivedData(byte[] data) { |
| 46 | + try { |
| 47 | +- messageNMEA += new String(data); |
| 48 | +- |
| 49 | +- while (messageNMEA.indexOf(0x0a) != -1) { |
| 50 | +- |
| 51 | +- int eol = messageNMEA.indexOf(0x0a); |
| 52 | +- if (-1 != eol) { |
| 53 | +- String sentence = messageNMEA.substring(0, eol + 1); |
| 54 | +- messageNMEA = messageNMEA.substring(eol + 1); |
| 55 | +- |
| 56 | +- // Boolean allowed = throttle.tryAcquire(); |
| 57 | +- // if (!allowed) { |
| 58 | +- // return; |
| 59 | +- // } |
| 60 | +- |
| 61 | +- callback.receivedData(sentence); |
| 62 | +- } else if (messageNMEA.length() > 128) { |
| 63 | +- throw new Exception("invalid NMEA string"); |
| 64 | +- } |
| 65 | +- } |
| 66 | ++ // FIXED: Send all data immediately as hex string (for binary protocols like MSP) |
| 67 | ++ // Original code waited for newline characters (NMEA protocol), which never comes with binary data |
| 68 | ++ android.util.Log.d("UsbSerial", "updateReceivedData: sending " + data.length + " bytes as hex"); |
| 69 | ++ String hexData = HexDump.toHexString(data); |
| 70 | ++ android.util.Log.d("UsbSerial", "updateReceivedData: hex string length = " + hexData.length()); |
| 71 | ++ callback.receivedData(hexData); |
| 72 | ++ android.util.Log.d("UsbSerial", "updateReceivedData: callback.receivedData called successfully"); |
| 73 | + } catch (Exception exception) { |
| 74 | + updateReadDataError(exception); |
| 75 | + } |
| 76 | +diff --git a/node_modules/capacitor-plugin-usb-serial/android/src/main/java/com/viewtrak/plugins/usbserial/UsbSerialPlugin.java b/node_modules/capacitor-plugin-usb-serial/android/src/main/java/com/viewtrak/plugins/usbserial/UsbSerialPlugin.java |
| 77 | +index 2d2edd1..ec097e8 100644 |
| 78 | +--- a/node_modules/capacitor-plugin-usb-serial/android/src/main/java/com/viewtrak/plugins/usbserial/UsbSerialPlugin.java |
| 79 | ++++ b/node_modules/capacitor-plugin-usb-serial/android/src/main/java/com/viewtrak/plugins/usbserial/UsbSerialPlugin.java |
| 80 | +@@ -156,9 +156,12 @@ public class UsbSerialPlugin extends Plugin implements Callback { |
| 81 | + |
| 82 | + @Override |
| 83 | + public void receivedData(String Data) { |
| 84 | ++ android.util.Log.d("UsbSerialPlugin", "receivedData callback: " + Data.length() + " chars"); |
| 85 | + JSObject ret = new JSObject(); |
| 86 | + ret.put("data", Data); |
| 87 | ++ android.util.Log.d("UsbSerialPlugin", "Calling notifyListeners with data event"); |
| 88 | + notifyListeners("data", ret); |
| 89 | ++ android.util.Log.d("UsbSerialPlugin", "notifyListeners completed"); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
0 commit comments