1

我是 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 上一样

4

1 回答 1

-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带有读写方法。

于 2015-04-15T08:27:40.560 回答