android 读取串口数据的服务

盛年不重来,一日难再晨,及时当勉励,岁月不待人。这篇文章主要讲述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 17protected SerialPort mSerialPort; 18protected OutputStream mOutputStream; 19private InputStream mInputStream; 20private ReadThread mReadThread; 21 22private class ReadThread extends Thread { 23byte[] buffer = new byte[128]; 24 25@Override 26public void run() { 27super.run(); 28while (!isInterrupted()) { 29int size; 30try { 31 32if (mInputStream == null) 33return; 34size = mInputStream.read(buffer); 35if (size > 0) { 36onDataReceived(buffer, size); 38} 39} catch (IOException e) { 40e.printStackTrace(); 41return; 42} 43} 44} 45} 46 47@Override 48public void onCreate() { 50try { 52mSerialPort = new SerialPort(new File("/dev/ttyS3"), 9600, 0); 53mOutputStream = mSerialPort.getOutputStream(); 54mInputStream = mSerialPort.getInputStream(); 55 56mReadThread = new ReadThread(); 57mReadThread.start(); 58} catch (Exception e) { 59e.printStackTrace(); 60} 61} 62 63protected abstract void onDataReceived(final byte[] buffer, final int size); 64 65@Override 66public void onDestroy() { 67if (mReadThread != null){ 68mReadThread.interrupt(); 69} 70if (mSerialPort != null) { 71mSerialPort.close(); 72mSerialPort = null; 73} 75mSerialPort = null; 76super.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, software 11* 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 and 14* 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 { 30private static final String TAG = "SerialPort"; 31 32/* 33* Do not remove or rename the field mFd: it is used by native method 34* close(); 35*/ 36private FileDescriptor mFd; 37private FileInputStream mFileInputStream; 38private FileOutputStream mFileOutputStream; 39 40public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException { 41try { 42System.loadLibrary("serial_port"); 43} catch (Exception ex) { 44Log.d("asdf", ex.getMessage()); 45} 46/* Check access permission */ 47if (!device.canRead() || !device.canWrite()) { 48try { 49/* Missing read/write permission, trying to chmod the file */ 50Process su; 51su = Runtime.getRuntime().exec("/system/bin/su"); 52String cmd = "chmod 666 " + device.getAbsolutePath() + "\n" + "exit\n"; 53su.getOutputStream().write(cmd.getBytes()); 54if ((su.waitFor() != 0) || !device.canRead() || !device.canWrite()) { 55throw new SecurityException(); 56} 57} catch (Exception e) { 58e.printStackTrace(); 59throw new SecurityException(); 60} 61} 62Log.d("port", "open ready"); 63mFd = open(device.getAbsolutePath(), baudrate, flags); 64if (mFd == null) { 65Log.e(TAG, "native open returns null"); 66throw new IOException(); 67} 68mFileInputStream = new FileInputStream(mFd); 69mFileOutputStream = new FileOutputStream(mFd); 70} 71 72// Getters and setters 73public InputStream getInputStream() { 74return mFileInputStream; 75} 76 77public OutputStream getOutputStream() { 78return mFileOutputStream; 79} 80 81// JNI 82private native static FileDescriptor open(String path, int baudrate, int flags); 83 84public native void close(); 85 86static { 87System.loadLibrary("serial_port"); 88} 89 }

 
 

 

    推荐阅读