1.Andorid串口开发包一般使用google多年前提供的android-serialport-api, 提供自用分支
https://github.com/zealzeng/android-serialport-api
2.Android设备一般需要root, 保证设备串口文件如/dev/ttyS0,
/dev/ttyUSB0等可读可写, 如果无权限, 则需要切到su执行chmod
666。需要注意的是有些设备su路径是/system/bin/su, 有些是/system/xbin/su. 见SerialPort.java关键代码.
public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException {
/* Check access permission */
if (!device.canRead() || !device.canWrite()) {
Process su = null;
try {
/* Missing read/write permission, trying to chmod the file */
//su = Runtime.getRuntime().exec("/system/bin/su");
su = Runtime.getRuntime().exec("/system/xbin/su");
String cmd = "chmod 666 " + device.getAbsolutePath() + "\n" + "exit\n";
su.getOutputStream().write(cmd.getBytes(