수색…


스마트 카드 송수신

연결에 대한 이해를 돕기위한 스 니펫은 다음과 같습니다.

//Allows you to enumerate and communicate with connected USB devices.
UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
//Explicitly asking for permission
final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();

UsbDevice device = deviceList.get("//the device you want to work with");
if (device != null) {
    mUsbManager.requestPermission(device, mPermissionIntent);
}

이제 Java에서 통신이 Android에서는 사용할 수없는 패키지 javax.smarcard를 사용하여 이루어 지므로 APDU (스마트 카드 명령)와 통신하거나 송수신 할 수있는 방법에 대한 아이디어를 얻으려면 여기를 살펴보십시오.

이제 위에서 언급 한 답변에서 말한 것처럼

대량 출력 엔드 포인트를 통해 APDU (스마트 카드 명령)를 보내고 대량 응답 엔드 포인트에서 응답 APDU를 수신 할 수 없습니다. 엔드 포인트를 얻으려면 아래 코드 스 니펫을 참조하십시오.

UsbEndpoint epOut = null, epIn = null;
UsbInterface usbInterface;

UsbDeviceConnection connection = mUsbManager.openDevice(device);

    for (int i = 0; i < device.getInterfaceCount(); i++) {
        usbInterface = device.getInterface(i);
        connection.claimInterface(usbInterface, true);

        for (int j = 0; j < usbInterface.getEndpointCount(); j++) {
            UsbEndpoint ep = usbInterface.getEndpoint(j);

            if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
                if (ep.getDirection() == UsbConstants.USB_DIR_OUT) {
                    // from host to device
                    epOut = ep;

                } else if (ep.getDirection() == UsbConstants.USB_DIR_IN) {
                    // from device to host
                    epIn = ep;
                }
            }
        }
    }

이제 APDU 명령 및 APDU 응답 블록을 송수신 할 대량 입출력 엔드 포인트가 있습니다.

명령을 보내려면 아래 코드 스 니펫을 참조하십시오.

public void write(UsbDeviceConnection connection, UsbEndpoint epOut, byte[] command) {
    result = new StringBuilder();
    connection.bulkTransfer(epOut, command, command.length, TIMEOUT);
    //For Printing logs you can use result variable
    for (byte bb : command) {
        result.append(String.format(" %02X ", bb));
    }
}

수신 / 읽기에 대한 응답은 아래 코드 스 니펫을 참조하십시오.

public int read(UsbDeviceConnection connection, UsbEndpoint epIn) {
result = new StringBuilder();
final byte[] buffer = new byte[epIn.getMaxPacketSize()];
int byteCount = 0;
byteCount = connection.bulkTransfer(epIn, buffer, buffer.length, TIMEOUT);

//For Printing logs you can use result variable
if (byteCount >= 0) {
    for (byte bb : buffer) {
        result.append(String.format(" %02X ", bb));
    }

    //Buffer received was : result.toString()
} else {
    //Something went wrong as count was : " + byteCount
}

return byteCount;
}

이제이 대답을 보게되면 보내질 첫 번째 명령은 다음과 같습니다.

카드를 활성화하려면 PC_to_RDR_IccPowerOn 명령을 사용하십시오.

여기에서 USB 장치 클래스 사양 문서의 6.1.1 절을 읽으면 작성할 수 있습니다.

62000000000000000000 이것을 보낼 수있는 방법은 다음과 같습니다.

write(connection, epOut, "62000000000000000000");

이제 APDU 명령을 성공적으로 보낸 후 다음을 사용하여 응답을 읽을 수 있습니다.

read(connection, epIn);

와 비슷한 것을 받으세요.

80 18000000 00 00 00 00 00 3BBF11008131FE45455041000000000000000000000000F1

이제 코드에서받은 응답은 코드에서 read() 메서드의 result 변수에 있습니다.



Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow