以下是基于开源项目https://github.com/cepr/android-serialport-api下开发的,作简要记录。
都说是,谷歌的官方库
一、项目配置
1.创建了jni和jniLibs两文件夹,将.so文件放在jniLibs下,将mk和C一些文件放在jni下
将下载的库 android-serialport-api-android-serialport-api-1.1\android-serialport-api中相应的文件拷贝到下图中
2.在main/Java下,创建android_serialport_api包,放入SerialPort.java文件,这个包名必须的是和jni包下的SerialPort.c中的函数名同步的。直接拷贝文件夹,然后删除里面sample,别着急编译,否则报错
3.添加编译选项
代码:
sourceSets {
main { jni.srcDirs = [] }
}
二、在项目中使用
类SerialPort.java中的代码
官网给的类,中需要注意修改的地方就是,看你开发板su的路径
Process su = Runtime.getRuntime().exec("/system/xbin/su");
如果执行失败,就是没有root,我开发板是root的,但是还是不能运行上面的话,然后我进入
各种坑
进入你的目录
C:\Users\T\AppData\Local\Android\Sdk>cd platform-tools
adb root
adb shell
关闭安全策略,理解为防火墙,只是(临时关闭)
setenforce 0
如果还不行,强制 串口读写
root@msm8909:/ # chmod 666 /dev/ttyHSL1
然后在运行就可以了
/*
* Copyright 2009 Cedric Priscal
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android_serialport_api;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.util.Log;
public class SerialPort
{
private static final String TAG = "SerialPort";
/*
* Do not remove or rename the field mFd: it is used by native method close();
*/
private FileDescriptor mFd;
private FileInputStream mFileInputStream;
private FileOutputStream mFileOutputStream;
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/xbin/su");
String cmd = "chmod 666 " + 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();
}
}
mFd = open(device.getAbsolutePath(), baudrate, flags);
if (mFd == null)
{
Log.e(TAG, "native open returns null");
throw new IOException();
}
mFileInputStream = new FileInputStream(mFd);
mFileOutputStream = new FileOutputStream(mFd);
}
// Getters and setters
public InputStream getInputStream()
{
return mFileInputStream;
}
public OutputStream getOutputStream()
{
return mFileOutputStream;
}
// JNI(调用java本地接口,实现串口的打开和关闭)
/**串口有五个重要的参数:串口设备名,波特率,检验位,数据位,停止位
其中检验位一般默认位NONE,数据位一般默认为8,停止位默认为1*/
// JNI
/**
* @param path 串口设备的据对路径
* @param baudrate 波特率
* @param flags 校验位
*/
private native static FileDescriptor open(String path, int baudrate, int flags);
public native void close();
static
{
//加载jni下的C文件库
System.loadLibrary("serial_port");
}
}
使用上面的类
private boolean OpenSerialPort()
{
//串口号
Spinner spinner = (Spinner) findViewById(R.id.spinnerSerialName);
String strSerialName = "/dev/" + spinner.getSelectedItem().toString();
//波特率
spinner = (Spinner) findViewById(R.id.spinnerBaudrate);
String strBaudrate = spinner.getSelectedItem().toString();
int iBaudrate = Integer.getInteger(strBaudrate);
try
{
SerialPort mSerialPort = new SerialPort(new File(strSerialName), iBaudrate, 0);
InputStream mInputStream = mSerialPort.getInputStream();
return true;
}
catch (SecurityException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
}
在提供一个新的方法,使用as自带的模拟器,然后执行下面的操作
第一步,需要在C:\Program Files (x86)\Android\android-sdk\tools 在这个目录下执行
emulator.exe @模拟器名称 -qemu -serial COM2 指令 (要在安装的tools目录下执行)
执行成功的话,会出现一个对话框.要你设置串口.别乱设置 (n,8,1,没得选,这是google项目已经规定好的)
执行成功的话,那么模拟器就会启动.