diff --git a/codec2talkie/src/main/java/com/radio/codec2talkie/settings/PreferenceKeys.java b/codec2talkie/src/main/java/com/radio/codec2talkie/settings/PreferenceKeys.java
index 2e5d749e..5e4dff6d 100644
--- a/codec2talkie/src/main/java/com/radio/codec2talkie/settings/PreferenceKeys.java
+++ b/codec2talkie/src/main/java/com/radio/codec2talkie/settings/PreferenceKeys.java
@@ -9,6 +9,8 @@ public final class PreferenceKeys {
public static String PORTS_USB_PARITY = "ports_usb_parity";
public static String PORTS_USB_DTR = "ports_usb_dtr";
public static String PORTS_USB_RTS = "ports_usb_rts";
+ public static String PORTS_USB_IS_PREFIX_ENABLED = "ports_usb_is_prefix_enabled";
+ public static String PORTS_USB_PREFIX = "ports_usb_prefix";
public static String PORTS_BT_CLIENT_NAME = "ports_bt_client_name";
diff --git a/codec2talkie/src/main/java/com/radio/codec2talkie/tools/TextTools.java b/codec2talkie/src/main/java/com/radio/codec2talkie/tools/TextTools.java
index 5ed80b44..7c9da3dc 100644
--- a/codec2talkie/src/main/java/com/radio/codec2talkie/tools/TextTools.java
+++ b/codec2talkie/src/main/java/com/radio/codec2talkie/tools/TextTools.java
@@ -53,4 +53,14 @@ public static String getString(ByteBuffer byteBuffer) {
}
return result.toString();
}
+
+ public static byte[] hexStringToByteArray(String s) {
+ int len = s.length();
+ byte[] data = new byte[len / 2];
+ for (int i = 0; i < len; i += 2) {
+ data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ + Character.digit(s.charAt(i+1), 16));
+ }
+ return data;
+ }
}
diff --git a/codec2talkie/src/main/java/com/radio/codec2talkie/transport/TransportFactory.java b/codec2talkie/src/main/java/com/radio/codec2talkie/transport/TransportFactory.java
index 023759d2..39e32e1b 100644
--- a/codec2talkie/src/main/java/com/radio/codec2talkie/transport/TransportFactory.java
+++ b/codec2talkie/src/main/java/com/radio/codec2talkie/transport/TransportFactory.java
@@ -40,7 +40,7 @@ public static Transport create(TransportType transportType, Context context) thr
switch (transportType) {
case USB:
- return new UsbSerial(UsbPortHandler.getPort(), UsbPortHandler.getName());
+ return new UsbSerial(UsbPortHandler.getPort(), UsbPortHandler.getName(), context);
case BLUETOOTH:
return new Bluetooth(BluetoothSocketHandler.getSocket(), BluetoothSocketHandler.getName());
case TCP_IP:
diff --git a/codec2talkie/src/main/java/com/radio/codec2talkie/transport/UsbSerial.java b/codec2talkie/src/main/java/com/radio/codec2talkie/transport/UsbSerial.java
index 0912d4ee..a9ce0a8a 100644
--- a/codec2talkie/src/main/java/com/radio/codec2talkie/transport/UsbSerial.java
+++ b/codec2talkie/src/main/java/com/radio/codec2talkie/transport/UsbSerial.java
@@ -1,9 +1,17 @@
package com.radio.codec2talkie.transport;
+import android.content.Context;
+import android.content.SharedPreferences;
+
+import androidx.preference.PreferenceManager;
+
import com.hoho.android.usbserial.driver.SerialTimeoutException;
import com.hoho.android.usbserial.driver.UsbSerialPort;
+import com.radio.codec2talkie.settings.PreferenceKeys;
+import com.radio.codec2talkie.tools.TextTools;
import java.io.IOException;
+import java.nio.ByteBuffer;
public class UsbSerial implements Transport {
@@ -13,9 +21,18 @@ public class UsbSerial implements Transport {
private final UsbSerialPort _usbPort;
private final String _name;
- public UsbSerial(UsbSerialPort usbPort, String name) {
+ private final boolean _isPrefixEnabled;
+ private final byte[] _bytePrefix;
+
+ protected SharedPreferences _sharedPreferences;
+
+ public UsbSerial(UsbSerialPort usbPort, String name, Context context) {
_usbPort = usbPort;
_name = name;
+ _sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
+ _isPrefixEnabled = _sharedPreferences.getBoolean(PreferenceKeys.PORTS_USB_IS_PREFIX_ENABLED, false);
+ String prefix = _sharedPreferences.getString(PreferenceKeys.PORTS_USB_PREFIX, "");
+ _bytePrefix = TextTools.hexStringToByteArray(prefix);
}
@Override
@@ -31,7 +48,15 @@ public int read(byte[] data) throws IOException {
@Override
public int write(byte[] data) throws IOException {
try {
- _usbPort.write(data, TX_TIMEOUT);
+ if (_isPrefixEnabled) {
+ byte[] pkt = ByteBuffer.allocate(_bytePrefix.length + data.length)
+ .put(_bytePrefix)
+ .put(data)
+ .array();
+ _usbPort.write(pkt, TX_TIMEOUT);
+ } else {
+ _usbPort.write(data, TX_TIMEOUT);
+ }
return data.length;
} catch (SerialTimeoutException e) {
e.printStackTrace();
diff --git a/codec2talkie/src/main/res/values/strings.xml b/codec2talkie/src/main/res/values/strings.xml
index 21b030cd..568d28a2 100644
--- a/codec2talkie/src/main/res/values/strings.xml
+++ b/codec2talkie/src/main/res/values/strings.xml
@@ -192,6 +192,10 @@
Serial stop bits
USB serial settings
Set USB serial settings, such as speed, bits, parity, etc.
+ Enable USB packet prefix
+ Prefix USB data with the HEX string for LoRA UART modems
+ USB packet prefix value as HEX string
+ Prefix sent content with a hex string (so called transmission target in UART modems), e.g. C0FFEE
Play audio through the speaker
Output incoming audio through the speaker
diff --git a/codec2talkie/src/main/res/xml/preferences_usb.xml b/codec2talkie/src/main/res/xml/preferences_usb.xml
index 93aee6c8..96fe4e84 100644
--- a/codec2talkie/src/main/res/xml/preferences_usb.xml
+++ b/codec2talkie/src/main/res/xml/preferences_usb.xml
@@ -54,5 +54,21 @@
app:summary="@string/usb_rts_summary"
app:defaultValue="false">
+
+
+
+
+
+
+
\ No newline at end of file