zhouasden
zhouasden
采纳率33.3%
2016-11-24 11:01

android_serialport_api 串口通信 new SerialPort异常

已采纳

代码 :执行 su = Runtime.getRuntime().exec("/system/bin/su"); 就被try catch了

public SerialPort(File device, int baudrate, int flags) throws SecurityException, 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");
String cmd = "chmod 777 " + device.getAbsolutePath() + "\n"
+ "exit\n";
su.getOutputStream().write(cmd.getBytes());
if ((su.waitFor() != 0) || !device.canRead()
|| !device.canWrite()) {
throw new SecurityException();
}
} catch (Exception e) {
e.printStackTrace();
throw new SecurityException();
}
}
Log.e("Tag", "串口打开...");
mFd = open(device.getAbsolutePath(), baudrate, flags);
Log.e("Tag", "串口打开成功");

    if (mFd == null) {
        throw new IOException();
    }
    mFileInputStream = new FileInputStream(mFd);
    mFileOutputStream = new FileOutputStream(mFd);

}
 错误信息:
11-24 18:58:45.459: E/ActivityManager(412): ANR in com.zxsd.airdetection
11-24 18:58:45.459: E/ActivityManager(412): PID: 4736
11-24 18:58:45.459: E/ActivityManager(412): Reason: Executing service com.zxsd.airdetection/com.zxsd.Server.AirServer
11-24 18:58:45.459: E/ActivityManager(412): Load: 0.21 / 0.23 / 0.18
11-24 18:58:45.459: E/ActivityManager(412): CPU usage from 9647ms to 0ms ago with 99% awake:
11-24 18:58:45.459: E/ActivityManager(412):   0.7% 100/surfaceflinger: 0.4% user + 0.3% kernel
11-24 18:58:45.459: E/ActivityManager(412):   0.6% 412/system_server: 0.1% user + 0.5% kernel / faults: 3 minor
11-24 18:58:45.459: E/ActivityManager(412):   0.6% 472/com.android.systemui: 0.4% user + 0.2% kernel
11-24 18:58:45.459: E/ActivityManager(412):   0.2% 28/fb-vsync: 0% user + 0.2% kernel
11-24 18:58:45.459: E/ActivityManager(412):   0.2% 823/RTW_CMD_THREAD: 0% user + 0.2% kernel
11-24 18:58:45.459: E/ActivityManager(412):   0.1% 1//init: 0% user + 0.1% kernel / faults: 28 minor
11-24 18:58:45.459: E/ActivityManager(412):   0.1% 23/irq/58-rk30-adc: 0.1% user + 0% kernel
11-24 18:58:45.459: E/ActivityManager(412):   0.1% 26/fb-vsync: 0% user + 0.1% kernel
11-24 18:58:45.459: E/ActivityManager(412):   0.1% 103/mediaserver: 0.1% user + 0% kernel
11-24 18:58:45.459: E/ActivityManager(412):   0.1% 120/adbd: 0% user + 0.1% kernel
11-24 18:58:45.459: E/ActivityManager(412):   0% 1147/com.google.process.gapps: 0% user + 0% kernel
11-24 18:58:45.459: E/ActivityManager(412): 0.8% TOTAL: 0.2% user + 0.4% kernel + 0.1% softirq
11-24 18:58:45.459: E/ActivityManager(412): CPU usage from 1923ms to 2435ms later:
11-24 18:58:45.459: E/ActivityManager(412):   3.9% 412/system_server: 0% user + 3.9% kernel
11-24 18:58:45.459: E/ActivityManager(412):     1.9% 426/ActivityManager: 0% user + 1.9% kernel
11-24 18:58:45.459: E/ActivityManager(412): 0.5% TOTAL: 0% user + 0.5% kernel

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答