खोज…


स्मार्ट कार्ड भेजें और प्राप्त करें

कनेक्शन के लिए, यहां आपको समझने में सहायता के लिए एक स्निपेट दिया गया है:

//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);
}

अब आपको यह समझना होगा कि जावा में पैकेज javax.smarcard का उपयोग करके संचार होता है जो एंड्रॉइड के लिए उपलब्ध नहीं है, इसलिए यहां एक विचार प्राप्त करें कि आप एपीडीयू (स्मार्टकार्ड कमांड) को कैसे संचारित या भेज सकते हैं।

अब जैसा कि ऊपर वर्णित उत्तर में बताया गया है

आप केवल बल्क-आउट एंडपॉइंट पर 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 हैं: 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