基本思维就是打开串口,看串口能否正常打开,不能则被占用,能则读取数据。
public void SerialPort(File device, int baudrate) throws IOException {
/* Check access permission */
if (!device.canRead() || !device.canWrite()) {
try {
/* Missing read/write permission, trying to chmod the file */
Process su;
// su = Runtime.getRuntime().exec("/system/bin/su");
su = Runtime.getRuntime().exec("su");
String cmd = "chmod 666 " + device.getAbsolutePath() + "\n"
+ "exit\n";
DataOutputStream os = new DataOutputStream(su.getOutputStream());
os.write(cmd.getBytes());
os.flush();
os.close();
if ((su.waitFor() != 0) || !device.canRead()
|| !device.canWrite()) {
throw