|
16 | 16 |
|
17 | 17 | package android.serialport; |
18 | 18 |
|
| 19 | +import android.util.Log; |
19 | 20 | import java.io.File; |
20 | 21 | import java.io.FileDescriptor; |
21 | 22 | import java.io.FileInputStream; |
|
24 | 25 | import java.io.InputStream; |
25 | 26 | import java.io.OutputStream; |
26 | 27 |
|
27 | | -import android.util.Log; |
28 | | - |
29 | 28 | public class SerialPort { |
30 | 29 |
|
31 | | - private static final String TAG = "SerialPort"; |
| 30 | + private static final String TAG = "SerialPort"; |
| 31 | + |
| 32 | + private static final String DEFAULT_SU_PATH = "/system/bin/su"; |
32 | 33 |
|
33 | | - /* |
34 | | - * Do not remove or rename the field mFd: it is used by native method close(); |
35 | | - */ |
36 | | - private FileDescriptor mFd; |
37 | | - private FileInputStream mFileInputStream; |
38 | | - private FileOutputStream mFileOutputStream; |
| 34 | + private static String sSuPath = DEFAULT_SU_PATH; |
39 | 35 |
|
40 | | - public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException { |
| 36 | + /** |
| 37 | + * Set the su binary path, the default su binary path is {@link #DEFAULT_SU_PATH} |
| 38 | + * |
| 39 | + * @param suPath su binary path |
| 40 | + */ |
| 41 | + public static void setSuPath(String suPath) { |
| 42 | + if (suPath == null) { |
| 43 | + return; |
| 44 | + } |
| 45 | + sSuPath = suPath; |
| 46 | + } |
| 47 | + |
| 48 | + /* |
| 49 | + * Do not remove or rename the field mFd: it is used by native method close(); |
| 50 | + */ |
| 51 | + private FileDescriptor mFd; |
| 52 | + private FileInputStream mFileInputStream; |
| 53 | + private FileOutputStream mFileOutputStream; |
| 54 | + |
| 55 | + public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException { |
41 | 56 |
|
42 | 57 | /* Check access permission */ |
43 | | - if (!device.canRead() || !device.canWrite()) { |
44 | | - try { |
45 | | - /* Missing read/write permission, trying to chmod the file */ |
46 | | - Process su; |
47 | | - su = Runtime.getRuntime().exec("/system/bin/su"); |
48 | | - String cmd = "chmod 666 " + device.getAbsolutePath() + "\n" |
49 | | - + "exit\n"; |
50 | | - su.getOutputStream().write(cmd.getBytes()); |
51 | | - if ((su.waitFor() != 0) || !device.canRead() |
52 | | - || !device.canWrite()) { |
53 | | - throw new SecurityException(); |
54 | | - } |
55 | | - } catch (Exception e) { |
56 | | - e.printStackTrace(); |
57 | | - throw new SecurityException(); |
58 | | - } |
59 | | - } |
60 | | - |
61 | | - mFd = open(device.getAbsolutePath(), baudrate, flags); |
62 | | - if (mFd == null) { |
63 | | - Log.e(TAG, "native open returns null"); |
64 | | - throw new IOException(); |
65 | | - } |
66 | | - mFileInputStream = new FileInputStream(mFd); |
67 | | - mFileOutputStream = new FileOutputStream(mFd); |
68 | | - } |
69 | | - |
70 | | - // Getters and setters |
71 | | - public InputStream getInputStream() { |
72 | | - return mFileInputStream; |
73 | | - } |
74 | | - |
75 | | - public OutputStream getOutputStream() { |
76 | | - return mFileOutputStream; |
77 | | - } |
78 | | - |
79 | | - // JNI |
80 | | - private native static FileDescriptor open(String path, int baudrate, int flags); |
81 | | - public native void close(); |
82 | | - static { |
83 | | - System.loadLibrary("serial_port"); |
84 | | - } |
| 58 | + if (!device.canRead() || !device.canWrite()) { |
| 59 | + try { |
| 60 | + /* Missing read/write permission, trying to chmod the file */ |
| 61 | + Process su; |
| 62 | + su = Runtime.getRuntime().exec(sSuPath); |
| 63 | + String cmd = "chmod 666 " + device.getAbsolutePath() + "\n" + "exit\n"; |
| 64 | + su.getOutputStream().write(cmd.getBytes()); |
| 65 | + if ((su.waitFor() != 0) || !device.canRead() || !device.canWrite()) { |
| 66 | + throw new SecurityException(); |
| 67 | + } |
| 68 | + } catch (Exception e) { |
| 69 | + e.printStackTrace(); |
| 70 | + throw new SecurityException(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + mFd = open(device.getAbsolutePath(), baudrate, flags); |
| 75 | + if (mFd == null) { |
| 76 | + Log.e(TAG, "native open returns null"); |
| 77 | + throw new IOException(); |
| 78 | + } |
| 79 | + mFileInputStream = new FileInputStream(mFd); |
| 80 | + mFileOutputStream = new FileOutputStream(mFd); |
| 81 | + } |
| 82 | + |
| 83 | + public SerialPort(String devicePath, int baudrate, int flags) |
| 84 | + throws SecurityException, IOException { |
| 85 | + this(new File(devicePath), baudrate, flags); |
| 86 | + } |
| 87 | + |
| 88 | + public SerialPort(File device, int baudrate) throws SecurityException, IOException { |
| 89 | + this(device, baudrate, 0); |
| 90 | + } |
| 91 | + |
| 92 | + public SerialPort(String devicePath, int baudrate) throws SecurityException, IOException { |
| 93 | + this(new File(devicePath), baudrate, 0); |
| 94 | + } |
| 95 | + |
| 96 | + // Getters and setters |
| 97 | + public InputStream getInputStream() { |
| 98 | + return mFileInputStream; |
| 99 | + } |
| 100 | + |
| 101 | + public OutputStream getOutputStream() { |
| 102 | + return mFileOutputStream; |
| 103 | + } |
| 104 | + |
| 105 | + // JNI |
| 106 | + private native static FileDescriptor open(String path, int baudrate, int flags); |
| 107 | + |
| 108 | + public native void close(); |
| 109 | + |
| 110 | + static { |
| 111 | + System.loadLibrary("serial_port"); |
| 112 | + } |
85 | 113 | } |
0 commit comments