Smart Card Reader | Andriod

การเขียนโค้ด Android เพื่ออ่านบัตรประชาชน ผ่าน Smart Card Reader

สำหรับองค์กรหรือผู้พัฒนาระบบที่ต้องการเชื่อมต่อเครื่องอ่านบัตรสมาร์ทการ์ดกับอุปกรณ์ Android สามารถเริ่มต้นได้ด้วย Android USB Host API เพื่อสื่อสารกับตัวเครื่องอ่าน และใช้ APDU Commands ในการรับส่งข้อมูลกับบัตรประชาชน โดยบทความนี้สรุปแนวทางตั้งค่าพื้นฐาน พร้อมตัวอย่างโค้ดเพื่อให้เห็นภาพการพัฒนาได้ชัดเจนยิ่งขึ้น

ภาพรวมการทำงาน

แนวทางนี้เหมาะสำหรับงานพัฒนาแอป Android ที่ต้องการอ่านข้อมูลจากบัตรประชาชนผ่านเครื่องอ่าน Smart Card Reader แบบ USB โดยระบบจะเริ่มจากการเปิดสิทธิ์การเชื่อมต่ออุปกรณ์, ตรวจสอบอุปกรณ์ที่ต่อเข้ามา, เปิดการเชื่อมต่อกับพอร์ต USB และส่งชุดคำสั่ง APDU ไปยังบัตรผ่านเครื่องอ่าน

โครงสร้างนี้สามารถนำไปต่อยอดกับระบบลงทะเบียน, ยืนยันตัวตน, ระบบบริการหน้าจุดเคาน์เตอร์ หรือระบบเฉพาะทางขององค์กรที่ต้องการดึงข้อมูลจากบัตรประชาชนเข้าสู่แอปพลิเคชัน Android

องค์ประกอบสำคัญ

Android USB Host APIใช้สำหรับตรวจจับและเชื่อมต่ออุปกรณ์ USB Reader กับระบบ Android
USB Permissionต้องกำหนดสิทธิ์และตรวจสอบก่อนเปิดใช้งานอุปกรณ์จริง
Device Filterใช้ระบุ Vendor ID และ Product ID ของเครื่องอ่านที่ต้องการรองรับ
APDU Commandใช้สื่อสารกับสมาร์ทการ์ด เช่น Select File และ Read Data

ขั้นตอนการตั้งค่าและตัวอย่างโค้ด

1

เพิ่ม Permission ใน AndroidManifest.xml

ขั้นตอนแรกคือกำหนดให้แอปรองรับการเชื่อมต่ออุปกรณ์ USB และระบุ meta-data สำหรับการตรวจจับเครื่องอ่านที่เชื่อมต่อเข้ากับอุปกรณ์ Android

<uses-permission android:name="android.permission.USB_PERMISSION" /> <uses-feature android:name="android.hardware.usb.host" /> <application> <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" /> </application>
AndroidManifest.xml
2

สร้างไฟล์ device_filter.xml

ไฟล์นี้ใช้สำหรับกำหนด Vendor ID และ Product ID ของเครื่องอ่าน Smart Card Reader ที่ต้องการให้ระบบรู้จัก โดยควรแก้ไขค่าให้ตรงกับรุ่นของอุปกรณ์ที่ใช้งานจริง

<?xml version="1.0" encoding="utf-8"?> <resources> <usb-device vendor-id="1839" product-id="8721" /> </resources>
res/xml/device_filter.xml
3

สร้างคลาสเชื่อมต่อ Smart Card Reader

ตัวอย่างด้านล่างเป็นโค้ด Java สำหรับเปิดการเชื่อมต่อกับอุปกรณ์ USB, ตรวจจับ Endpoint แบบ Bulk Transfer และใช้สำหรับรับส่งข้อมูลกับเครื่องอ่านบัตร

import android.hardware.usb.*; import android.content.Context; public class SmartCardReader { private UsbManager usbManager; private UsbDevice device; private UsbDeviceConnection connection; private UsbEndpoint inEndpoint, outEndpoint; public SmartCardReader(Context context) { usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE); } public void connect(UsbDevice device) { this.device = device; if (!usbManager.hasPermission(device)) { // Implement permission dialog return; } UsbInterface usbInterface = device.getInterface(0); connection = usbManager.openDevice(device); connection.claimInterface(usbInterface, true); 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 { outEndpoint = endpoint; } } } } public String sendApduCommand(byte[] command) { if (connection == null || outEndpoint == null || inEndpoint == null) { return "Device not connected"; } connection.bulkTransfer(outEndpoint, command, command.length, 5000); byte[] response = new byte[256]; int received = connection.bulkTransfer(inEndpoint, response, response.length, 5000); if (received > 0) { return bytesToHex(response, received); } return "No response"; } private String bytesToHex(byte[] bytes, int length) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { sb.append(String.format("%02X", bytes[i])); } return sb.toString(); } }
Java Example
4

เรียกใช้งานใน Activity

เมื่อค้นหาอุปกรณ์ USB จาก usbManager.getDeviceList() แล้ว สามารถสร้างอ็อบเจ็กต์ของคลาส SmartCardReader และส่งคำสั่ง APDU เพื่อเลือกไฟล์หรืออ่านข้อมูลจากบัตรได้ทันที

UsbDevice device = ... // ค้นหา UsbDevice ผ่าน usbManager.getDeviceList() SmartCardReader reader = new SmartCardReader(this); reader.connect(device); // ส่งคำสั่ง APDU เพื่อเลือกไฟล์ String response = reader.sendApduCommand( new byte[]{0x00, (byte) 0xA4, 0x00, 0x00, 0x02, 0x11, 0x11} ); Log.d("APDU Response", response);
Usage in Activity

ตัวอย่าง APDU Command ที่ใช้บ่อย

Select File

ใช้สำหรับเลือกไฟล์หรือแอปพลิเคชันภายในสมาร์ทการ์ดก่อนเริ่มอ่านข้อมูล

00 A4 00 00 02 11 11

Read Data

ใช้สำหรับอ่านข้อมูลจากตำแหน่งที่กำหนด โดยค่า 20 หมายถึงจำนวนไบต์ที่ต้องการอ่าน

00 B0 00 00 20

ข้อควรทราบก่อนใช้งานจริง

  • ควรตรวจสอบ Vendor ID และ Product ID ของเครื่องอ่านให้ตรงกับอุปกรณ์ที่ใช้งาน
  • ควรทดสอบการเชื่อมต่อกับเครื่องอ่านจริงก่อนนำไปใช้งานในระบบขององค์กร
  • การอ่านข้อมูลจากบัตรจำเป็นต้องใช้ชุดคำสั่ง APDU ที่เหมาะสมกับโครงสร้างข้อมูลของบัตร
  • ในงานพัฒนาเชิงองค์กร ควรมีการจัดการสิทธิ์, การตรวจสอบสถานะอุปกรณ์ และการจัดการข้อผิดพลาดเพิ่มเติม

กำลังมองหา Smart Card Reader สำหรับพัฒนาโปรแกรม?

The Compete พร้อมให้คำแนะนำทั้งด้านเครื่องอ่านบัตรสมาร์ทการ์ด, การเลือกอุปกรณ์ให้เหมาะกับระบบงาน และการประยุกต์ใช้งานร่วมกับซอฟต์แวร์ขององค์กร สำหรับงานอ่านบัตรประชาชน, งานยืนยันตัวตน และงานพัฒนาแอปพลิเคชันเฉพาะทาง

Visitors: 121,557