1.首先下载一个libserial_port.so,新建目录libs/armeabi,将so文件放到该目录下。
2.定义串口类,在类的构建函数中修改权限,打开设备,创建输入流和输出流,通过native接口访问串口打开关闭函数
public class SerialPort {
/*Do not remove or rename the field mFd: it is used by native method close();*/
public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException, InvalidParameterException{
//如果串口权限不够,改变权限
/* 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 666 " + device.getAbsolutePath() + "\n"
+ "exit\n";
su.getOutputStream().write(cmd.getBytes());
if ((su.waitFor() != 0) || !d
android串口操作
最新推荐文章于 2024-08-08 07:21:54 发布
本文介绍了如何在Android中进行串口操作,包括设置权限、创建串口类、使用JNI调用C库函数打开和关闭串口,以及创建读取线程来处理接收到的数据。示例代码展示了如何实例化串口类并读取数据。
摘要由CSDN通过智能技术生成