【问题标题】:Communication with usbserialhost to Android与 usbserialhost 到 Android 的通信
【发布时间】:2015-04-15 07:57:56
【问题描述】:

我是 android 编程智能卡检测(读取)的新手。我们使用 otg 电缆将 RFC 读卡器 (RFID-RC522) 连接到 android 设备。使用 USB 串行端口 API 检测到 RFC 读卡器,但无法从 RFC 读卡器读取卡。我们已经在java中实现了读卡,但在android中无法实现。

1) 如何获取相当于windows COM端口的端口号

2) 如果不可能,我该如何使用 connection.controlTransfer(UsbConstants.USB_DIR_OUT, 0x00, 0x0000, 0, null, 0, 0);方法

3) 连接设备的Java代码

static int connect ( String portName ) throws IOException
{

     CommPortIdentifier portIdentifier = null;


     try {
         portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
     } catch (NoSuchPortException e) {
         System.out.println("Port " + portName + " is not available");
     }
     if ( portIdentifier.isCurrentlyOwned() )
     {
         System.out.println("Error: Port " + portName + " is currently in use");
     }
     else
     {
         CommPort commPort = null;
         try {
             commPort = portIdentifier.open(portName,2000);
         } catch (PortInUseException e) {
             System.out.println("Error: Port " + portName + " is currently in use");
         }
         System.out.println("Info: Port Availble "+portName);
         if ( commPort instanceof SerialPort )
         {
             SerialPort serialPort = (SerialPort) commPort;
             try {
                 serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
             } catch (UnsupportedCommOperationException e) {
                 System.out.println("Error: Port " + portName + " is currently in use");
             }

             InputStreamIn = serialPort.getInputStream();
             OutputStreamOut = serialPort.getOutputStream();
             return 0;
         }
         else
         {
             //System.out.println("Error: Only serial ports are handled by this example.");
             System.out.println("Error: Port " + portName + " is currently in use");
         }
     }    
     return -1;
 }

4) 请告诉我如何在 android 上像在 java 上一样交流

【问题讨论】:

    标签: java android android-usb


    【解决方案1】:

    您必须使用 USB Host Android API: http://developer.android.com/guide/topics/connectivity/usb/host.html 枚举设备,授予权限。

    您也可以使用此 API 进行交流,或者还有一个用于 USB 串行设备的开源库:https://github.com/mik3y/usb-serial-for-android

    更新1

    例如,在这个库中,您有一个类https://github.com/mik3y/usb-serial-for-android/blob/master/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/UsbSerialPort.java 具有读写方法。

    【讨论】:

    • 我们有这样的命令 static int PCD_IDLE = 0x00; //没有动作;取消当前命令;静态int PCD_AUTHENT = 0x0E; // 认证密钥;静态 int PCD_RECEIVE = 0x08; // 接收数据;静态 int PCD_TRANSMIT = 0x04; // 传输数据;静态int PCD_TRANSCEIVE = 0x0C; //发送和接收数据hwo使用android传递命令请告诉我
    • 请举例说明如何读取它 static int PCD_IDLE = 0x00;使用 api
    • 请帮助这对我很重要
    • @ANANDPRABHAKAR,我不会为你开发。您必须自己尝试和测试。您必须阅读 Android USB Host 和 usb-serial-for-android 的文档。
    • @ANANDPRABHAKAR,StackOverflow 不是商业支持。这是社区支持!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 2012-10-01
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 2014-12-23
    相关资源
    最近更新 更多