哪位开发过android串口通信的指点一下啊,刚接触感觉一点头绪没有。要的功能很简单,从串口读数据显示到editText中在网上找到关于serialport的说法,但怎么用却找不到,公司要求连的设备是一个有蓝牙功能的无线设备,可以通过串口发送数据。有偿提供解答也可以啊,实在是急,老板太凶了。
解决方案 »
- android中可以实现动画背景吗?
- 代码生成的TextView怎样设置样式
- AlertDialog.Builder listview SimpleCursorAdapter删除项问题
- android传感器问题
- Android 初始化问题
- 安卓 拼板开发用什么语言?
- 求Android 4高级编程(第3版) 完整非扫描版
- SD卡装载广播,开机广播
- win7安装android studio遇到问题 求指点
- android 使用第三方浏览器唤醒app 唤醒多次后 接受<intent-filter> 的activity的oncreate方法 无法再次被唤起
- 跪求android群发短信代码
- Android2.3开发环境用32位和64位有要求么?
1.http://code.google.com/p/android-serialport-api/ 局限很大
2.http://v-lad.org/projects/gnu.io.android/这个没有机型限制,很好,希望你可以仔细看看
import java.io.*; import java.util.*; import gnu.io.*; public class RFIDReader implements Runnable,SerialPortEventListener{ static CommPortIdentifier portId; static Enumeration portList; InputStream inputStream; SerialPort serialPort; Thread readThread; public static void main(String[] args){ init(); } public static void init(){ portList=CommPortIdentifier.getPortIdentifiers(); while(portList.hasMoreElements()){ portId=(CommPortIdentifier)portList.nextElement(); if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL){ if(portId.getName().equals("COM1")){ RFIDReader reader=new RFIDReader(); System.out.println("COM1 start!"); } } } } public RFIDReader(){ try{ serialPort=(SerialPort)portId.open("Sunder",2000); }catch(PortInUseException e){System.out.println(e);} try{ inputStream=serialPort.getInputStream(); }catch(IOException e){System.out.println(e);} try{ serialPort.addEventListener(this); }catch(TooManyListenersException e){System.out.println(e);} serialPort.notifyOnDataAvailable(true); try{ serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); }catch(UnsupportedCommOperationException e){System.out.println(e);} readThread=new Thread(this); readThread.start(); } public void run(){ try{ Thread.sleep(20000); }catch(InterruptedException e){System.out.println(e);} } public void serialEvent(SerialPortEvent event){ switch(event.getEventType()){ case SerialPortEvent.BI: case SerialPortEvent.OE: case SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CD: case SerialPortEvent.CTS: case SerialPortEvent.DSR: case SerialPortEvent.RI: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE: byte[] readBuffer=new byte[20]; try{ while(inputStream.available()>0){ int numBytes=inputStream.read(readBuffer); } System.out.println(new String(readBuffer)); }catch(IOException e){System.out.println(e);} break; } } }
找了些资料看到RXTX可以 import进来 于serialPort相关的了,但关于 rxtx的使用还不清楚。路漫长啊