1. First download a libserial_port.so, create a new directory libs/armeabi, and place the so file in this directory.
2. Define the serial port class, modify permissions in the class construction function, open the device, create input streams and output streams, and access the serial port through the native interface to open and close the function
Copy the codeThe code is as follows:
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{
//If the serial port permissions are not sufficient, change the permissions
/* Check access permission */
if (!() || !()) {
try {
/* Missing read/write permission, trying to chmod the file */
Process su;
su = ().exec("/system/bin/su");
String cmd = "chmod 666 " + () + "\n"
+ "exit\n";
().write(());
if ((() != 0) || !()
|| !()) {
throw new SecurityException();
}
} catch (Exception e) {
();
throw new SecurityException();
}
}
mFd = open((), baudrate, flags);//Open the serial port
if (mFd == null) {
(TAG, "native open returns null");
throw new IOException();
}
mFileInputStream = new FileInputStream(mFd);//Serial port input stream
mFileOutputStream = new FileOutputStream(mFd);//Serial port output stream
}
// Getters and setters
public InputStream getInputStream() {
return mFileInputStream;
}
public OutputStream getOutputStream() {
return mFileOutputStream;
}
// JNI
private native static FileDescriptor open(String path, int baudrate, int flags);//Serial port open() function in c file
public native void close();//serial port close() function in c file
static {
("serial_port");//Load the serial port library
}
}
}
3. Define the abstract class ServerData
Copy the codeThe code is as follows:
public abstract class ServerData {
protected SerialPort mSerialPort;
protected OutputStream mOutputStream;
private InputStream mInputStream;
private ReadThread mReadThread;
private class ReadThread extends Thread {
@Override
//Read data in thread and process data
public void run() {
();
byte[] buffer = new byte[128];
int size;
while(true) {
try {
if (mInputStream == null) return;
size = (buffer);//Read data
if (size > 0) {
onDataReceived(buffer, size);//process data
}
} catch (IOException e) {
();
return;
}
}
}
}
4. Instantiate the serial port class, output stream and input stream, instantiate the read thread and start executing the thread
[code]
public ServerData(String path, int baudrate){
try {
mSerialPort = new SerialPort(new File(path), baudrate, 0);
mOutputStream = ();
mInputStream = ();
/* Create a receiving thread */
mReadThread = new ReadThread();
();
} catch (SecurityException e) {
} catch (IOException e) {
} catch (InvalidParameterException e) {
}
}
protected abstract void onDataReceived(final byte[] buffer, final int size);
}
[/code]
5. Then create a new class, implement the above abstract function in the newly created class, and write a function to return the read data.
Copy the codeThe code is as follows:
package View;
//Import R class, the package is different, it cannot be consumed directly, it needs to be imported before it can be used
import android_serialport_api.;
/* EtcView class, Etc interface management */
public class SerialView {
private Activity context = null;
private Serial mEtcServer = null;
/* Etc interface constructor */
public SerialView(Activity context) {
= context;
}
public void EtcInitView() {
//This way you can find the id under the package android_serialport_api.sample
TextView mytext=(TextView)();
mEtcServer = new Serial("/dev/s3c2410_serial3", 9600);
}
public void EtcRefresh() {
//Return the data read by the serial port thread
byte[] buffer = ();
String recString=new String(buffer);//Convert the array of byte[] into a string string
(recString);//Set character text
buffer = null;
}
}