android 讀取串口資料的服務,android串口
2016-09-1813:10:03
繼承Service,定義抽象方法onDataReceived,子類通過實現抽象方法擷取接收到資料的回調。
1 package com.zrsoft.liftad.serialport; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.OutputStream; 7 8 import android.app.Service; 9 import android_serialport_api.SerialPort;10 11 import com.zrsoft.liftad.MyApp;12 import com.zrsoft.liftad.utils.Logger;13 14 public abstract class SerialPortService extends Service {15 17 protected SerialPort mSerialPort;18 protected OutputStream mOutputStream;19 private InputStream mInputStream;20 private ReadThread mReadThread;21 22 private class ReadThread extends Thread {23 byte[] buffer = new byte[128];24 25 @Override26 public void run() {27 super.run();28 while (!isInterrupted()) {29 int size;30 try {31 32 if (mInputStream == null)33 return;34 size = mInputStream.read(buffer);35 if (size > 0) {36 onDataReceived(buffer, size); 38 }39 } catch (IOException e) {40 e.printStackTrace();41 return;42 }43 }44 }45 }46 47 @Override48 public void onCreate() { 50 try { 52 mSerialPort = new SerialPort(new File("/dev/ttyS3"), 9600, 0);53 mOutputStream = mSerialPort.getOutputStream();54 mInputStream = mSerialPort.getInputStream();55 56 mReadThread = new ReadThread();57 mReadThread.start();58 } catch (Exception e) {59 e.printStackTrace();60 }61 }62 63 protected abstract void onDataReceived(final byte[] buffer, final int size);64 65 @Override66 public void onDestroy() {67 if (mReadThread != null){68 mReadThread.interrupt();69 }70 if (mSerialPort != null) {71 mSerialPort.close();72 mSerialPort = null;73 } 75 mSerialPort = null;76 super.onDestroy();77 }78 }
SerialPort 類:
1 /* 2 * Copyright 2009 Cedric Priscal 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License. 15 */16 17 package android_serialport_api;18 19 import java.io.File;20 import java.io.FileDescriptor;21 import java.io.FileInputStream;22 import java.io.FileOutputStream;23 import java.io.IOException;24 import java.io.InputStream;25 import java.io.OutputStream;26 27 import android.util.Log;28 29 public class SerialPort {30 private static final String TAG = "SerialPort";31 32 /*33 * Do not remove or rename the field mFd: it is used by native method34 * close();35 */36 private FileDescriptor mFd;37 private FileInputStream mFileInputStream;38 private FileOutputStream mFileOutputStream;39 40 public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException {41 try {42 System.loadLibrary("serial_port");43 } catch (Exception ex) {44 Log.d("asdf", ex.getMessage());45 }46 /* Check access permission */47 if (!device.canRead() || !device.canWrite()) {48 try {49 /* Missing read/write permission, trying to chmod the file */50 Process su;51 su = Runtime.getRuntime().exec("/system/bin/su");52 String cmd = "chmod 666 " + device.getAbsolutePath() + "\n" + "exit\n";53 su.getOutputStream().write(cmd.getBytes());54 if ((su.waitFor() != 0) || !device.canRead() || !device.canWrite()) {55 throw new SecurityException();56 }57 } catch (Exception e) {58 e.printStackTrace();59 throw new SecurityException();60 }61 }62 Log.d("port", "open ready");63 mFd = open(device.getAbsolutePath(), baudrate, flags);64 if (mFd == null) {65 Log.e(TAG, "native open returns null");66 throw new IOException();67 }68 mFileInputStream = new FileInputStream(mFd);69 mFileOutputStream = new FileOutputStream(mFd);70 }71 72 // Getters and setters73 public InputStream getInputStream() {74 return mFileInputStream;75 }76 77 public OutputStream getOutputStream() {78 return mFileOutputStream;79 }80 81 // JNI82 private native static FileDescriptor open(String path, int baudrate, int flags);83 84 public native void close();85 86 static {87 System.loadLibrary("serial_port");88 }89 }