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中一个Activity多个intent-filter的调用方法
  2. Android gson解析json数据工具类
  3. Android模拟SD卡实现方法解析
  4. Android编程实现屏幕自适应方向尺寸与分辨率的方法
  5. 第三课:android数据相关---文件
  6. Android Studio导入Project的方法
  7. Android使用AudioRecord遇到的问题与解决方法
  8. Android串口Serial服务解析
  9. 王家林的81门一站式云计算分布式大数据&移动互联网解决方案课程

随机推荐

  1. 【译】Android系统简介
  2. 【Android(安卓)Training视频系列】第4讲
  3. 免费学习android开发技术分享
  4. android:installLocation简析
  5. android 开发BUG
  6. android设定手机的显示模式,横竖屏,是否全
  7. Android多国语言文件夹命名
  8. Android——通知(Notification)
  9. Android当中的SeekBar与iOS中的UISlider
  10. Android(安卓)path 贝塞尔曲线 波浪形