如何在java android中发送和读取apdu命令到USB智能卡读卡器

问题描述 投票:0回答:1

我尝试使用 UsbManager 执行此操作,发送命令可以工作,但是当我想读取响应时,响应数据不正确

我需要向智能卡读卡器发送此命令 {0x00, (byte) 0x83, 0x00, 0x00, 0X00},这样做

发送命令

byte[] command = new byte[] {0x00, (byte) 0x83, 0x00, 0x00, 0X00};
            String commandHex = bytesToHex(command);
            byte[] outBufferXFRBlock = hexStringToByteArray("6F" + "05000000" + "00" + sequenceNumber + "00" + "0000" + commandHex);

            int outByteCount = connection.bulkTransfer(outEndpoint, outBufferXFRBlock, outBufferXFRBlock.length, TIMEOUT);

阅读回复

final byte[] inBuffer = new byte[80];
int inByteCount = connection.bulkTransfer(inEndpoint, inBuffer, inBuffer.length, TIMEOUT);

开机和关机命令有效 我做的一切都对吗?

java android apdu
1个回答
0
投票

我不熟悉您的设备配置或您如何管理与其通信。但是,这里有一些代码可能有助于简化您的过程

import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;

import java.util.HashMap;

public class SmartCardReader {

    private UsbManager usbManager;
    private UsbDevice usbDevice;
    private UsbDeviceConnection usbDeviceConnection;
    private UsbEndpoint inEndpoint;
    private UsbEndpoint outEndpoint;
    private static final int TIMEOUT = 1000; // Timeout in milliseconds

    public SmartCardReader(UsbManager usbManager, UsbDevice usbDevice) {
        this.usbManager = usbManager;
        this.usbDevice = usbDevice;
    }

    public boolean connectToDevice() {
        // Open connection to the USB device
        usbDeviceConnection = usbManager.openDevice(usbDevice);
        if (usbDeviceConnection == null) {
            return false;
        }

        // Find and open the appropriate USB interface
        UsbInterface usbInterface = usbDevice.getInterface(0);
        usbDeviceConnection.claimInterface(usbInterface, true);

        // Find and set the input and output endpoints
        for (int i = 0; i < usbInterface.getEndpointCount(); i++) {
            UsbEndpoint endpoint = usbInterface.getEndpoint(i);
            if (endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
                if (endpoint.getDirection() == UsbConstants.USB_DIR_IN) {
                    inEndpoint = endpoint;
                } else if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) {
                    outEndpoint = endpoint;
                }
            }
        }

        return inEndpoint != null && outEndpoint != null;
    }

    public void disconnectFromDevice() {
        if (usbDeviceConnection != null) {
            usbDeviceConnection.releaseInterface(usbInterface);
            usbDeviceConnection.close();
        }
    }

    public byte[] sendAPDUCommand(byte[] command) {
        byte[] response = new byte[64]; // Adjust buffer size as needed

        // Send command to USB smart card reader
        int bytesTransferred = usbDeviceConnection.bulkTransfer(outEndpoint, command, command.length, TIMEOUT);
        if (bytesTransferred < 0) {
            // Error handling
            return null;
        }

        // Read response from USB smart card reader
        bytesTransferred = usbDeviceConnection.bulkTransfer(inEndpoint, response, response.length, TIMEOUT);
        if (bytesTransferred < 0) {
            // Error handling
            return null;
        }

        return response;
    }

    // Other methods for parsing and interpreting APDU responses can be added here
}
© www.soinside.com 2019 - 2024. All rights reserved.