哪位开发过android串口通信的指点一下啊,刚接触感觉一点头绪没有。要的功能很简单,从串口读数据显示到editText中在网上找到关于serialport的说法,但怎么用却找不到,公司要求连的设备是一个有蓝牙功能的无线设备,可以通过串口发送数据。有偿提供解答也可以啊,实在是急,老板太凶了。

解决方案 »

  1.   

    两个方案:
    1.http://code.google.com/p/android-serialport-api/ 局限很大
    2.http://v-lad.org/projects/gnu.io.android/这个没有机型限制,很好,希望你可以仔细看看
      

  2.   

    RXTX是个提供串口和并口通信的开源java类库,由该项目发布的文档均遵循LGPL协议。该项目的主页位于http://users.frii.com/jarvi/rxtx/index.html。   RXTX项目提供了Windows,Linux,Mac os X,Solaris操作系统下的兼容javax.comm串口通讯包API的实现,为其他研发人员在此类系统下研发串口应用提供了相当的方便。   针对x86体系结构的Linux操作系统平台,RXTX的部署包括下面几个文档:   * RXTXcomm.jar RXTX自己的javax.comm实现   * librxtxSerial.so 由RXTXcomm.jar调用的底层串口库文档   * librxtxParallel.so 由RXTXcomm.jar调用的底层并口库文档   * javax.comm.properties RXTX驱动的类配置文档,内容是Driver=gnu.io.RXTXCommDriver   三.RXTX的配置方法及部分源代码(Linux环境)   为了使我们的程式使用RXTX作为串口通讯的底层API,需要配置他的环境。仍然以Linux系统平台为例:   1.复制librxtxSerial.so,librxtxParallel.so到$JAVA_HOME/lib/$(ARCH)/   2.复制RXTXcomm.jar到$JAVA_HOME/ext/,或在应用程式启动的CLASSPATH中包含RXTXcomm.jar   3.定义驱动类后将javax.comm.properties放在应用程式的根目录下   RXTX的使用上和sun提供的comm.jar基本相同,编程时最明显的不同是要包含的包名由javax.comm.*改成了gnu.io.*。下面是我们环境监测系统中封装的一个232串口驱动类部分源代码,使用RXTX作为串口通讯类库。   Windows系统配置如下:   1. 将RXTXcomm.jar放到<jre_home>\lib\ext\下   2. 把rxtxParallel.dll,rxtxSerial.dll这两个放到你java的当前目录下(选windows下的)   3. 例子仍然采用原来SUN官方<ReadSimple.java>实例(change all references from 'javax.comm' to 'gnu.io' )   4. 参考网址:http://users.frii.com/jarvi/rxtx/download.html   =============================   以下为RXTX的简单应用代码,仅参考。  
    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;   }   }   } 
      

  3.   

    这个讲的更好http://www.eoeandroid.com/thread-18993-1-1.html
      

  4.   

    谢谢,看了第二个,网站也提供 librxtxSerial.so等文件的下载,可是不知道如何在eclipse中使用这个呢
      

  5.   

    是啊,不懂技术真是可怜啊。。
    找了些资料看到RXTX可以 import进来 于serialPort相关的了,但关于 rxtx的使用还不清楚。路漫长啊