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 }

更多相关文章

  1. Android如何横屏, 全屏, 背景灯常量
  2. Android中一个Activity多个intent-filter的调用方法
  3. 关于android SDK安装Failed to fetch URL http://dl-ssl.google.
  4. 【 Android(安卓)'四大组件' 】篇 -- Activity 的 "启动流程"
  5. Android(安卓)gson解析json数据工具类
  6. android 写文件权限
  7. Android(安卓)JSON,Gson,fastjson实现比较
  8. android scroller类的使用
  9. android加密的即时通信软件 -服务器端

随机推荐

  1. 解决android sdk 国内无法更新
  2. Android(安卓)GridView的使用
  3. Android学习笔记----Android简单有效的闪
  4. Android(安卓)SwitchButton(滑动开关)
  5. Android乐动力V5.75最新获Key方法,提交步
  6. Android之ExpandableListView
  7. android中的贪吃蛇源码
  8. BIOS中开启虚拟化技术
  9. android控制PC
  10. android中view的宽高测量