1
1
package com .radio .codec2talkie .transport ;
2
2
3
+ import android .content .Context ;
4
+ import android .content .SharedPreferences ;
5
+
6
+ import androidx .preference .PreferenceManager ;
7
+
3
8
import com .hoho .android .usbserial .driver .SerialTimeoutException ;
4
9
import com .hoho .android .usbserial .driver .UsbSerialPort ;
10
+ import com .radio .codec2talkie .settings .PreferenceKeys ;
11
+ import com .radio .codec2talkie .tools .TextTools ;
5
12
6
13
import java .io .IOException ;
14
+ import java .nio .ByteBuffer ;
7
15
8
16
public class UsbSerial implements Transport {
9
17
@@ -13,9 +21,18 @@ public class UsbSerial implements Transport {
13
21
private final UsbSerialPort _usbPort ;
14
22
private final String _name ;
15
23
16
- public UsbSerial (UsbSerialPort usbPort , String name ) {
24
+ private final boolean _isPrefixEnabled ;
25
+ private final byte [] _bytePrefix ;
26
+
27
+ protected SharedPreferences _sharedPreferences ;
28
+
29
+ public UsbSerial (UsbSerialPort usbPort , String name , Context context ) {
17
30
_usbPort = usbPort ;
18
31
_name = name ;
32
+ _sharedPreferences = PreferenceManager .getDefaultSharedPreferences (context );
33
+ _isPrefixEnabled = _sharedPreferences .getBoolean (PreferenceKeys .PORTS_USB_IS_PREFIX_ENABLED , false );
34
+ String prefix = _sharedPreferences .getString (PreferenceKeys .PORTS_USB_PREFIX , "" );
35
+ _bytePrefix = TextTools .hexStringToByteArray (prefix );
19
36
}
20
37
21
38
@ Override
@@ -31,7 +48,15 @@ public int read(byte[] data) throws IOException {
31
48
@ Override
32
49
public int write (byte [] data ) throws IOException {
33
50
try {
34
- _usbPort .write (data , TX_TIMEOUT );
51
+ if (_isPrefixEnabled ) {
52
+ byte [] pkt = ByteBuffer .allocate (_bytePrefix .length + data .length )
53
+ .put (_bytePrefix )
54
+ .put (data )
55
+ .array ();
56
+ _usbPort .write (pkt , TX_TIMEOUT );
57
+ } else {
58
+ _usbPort .write (data , TX_TIMEOUT );
59
+ }
35
60
return data .length ;
36
61
} catch (SerialTimeoutException e ) {
37
62
e .printStackTrace ();
0 commit comments