การเชื่อมต่อ Flutter กับ Smart Card Reader ผ่าน Bluetooth (ACS ACR3901U-S1)
การพัฒนาแอปพลิเคชันที่สามารถอ่าน Smart Card ได้ผ่านอุปกรณ์ Bluetooth กำลังเป็นที่นิยมมากขึ้น โดยเฉพาะในอุตสาหกรรมที่ต้องการความปลอดภัยสูง เช่น การเงิน รัฐบาล และระบบยืนยันตัวตน ในบทความนี้ เราจะสอนวิธีเชื่อมต่อ Flutter กับ ACS ACR3901U-S1 Smart Card Reader ผ่าน Bluetooth และอ่านข้อมูลจากบัตร Smart Card
อุปกรณ์และเครื่องมือที่ต้องใช้
- Smart Card Reader รุ่น ACS ACR3901U-S1 (รองรับ Bluetooth)
- บัตร Smart Card ที่รองรับการอ่าน
- โทรศัพท์มือถือ หรือแท็บเล็ต ที่รองรับ Bluetooth
- Flutter SDK (สามารถติดตั้งตาม คู่มือทางการ)
- แพ็กเกจ Flutter สำหรับ Bluetooth Communication เช่น
flutter_blue_plus
หรือflutter_reactive_ble
ติดตั้งแพ็กเกจที่จำเป็น
เริ่มต้นด้วยการติดตั้งแพ็กเกจที่ช่วยให้ Flutter สามารถสื่อสารผ่าน Bluetooth ได้ โดยใช้คำสั่ง:
flutter pub add flutter_blue_plus
Code language: Python (python)
หรือใช้
flutter_reactive_ble
ตามความเหมาะสม:
flutter pub add flutter_reactive_ble
การสแกนอุปกรณ์ Bluetooth
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'dart:async';
class BluetoothService {
FlutterBluePlus flutterBlue = FlutterBluePlus.instance;
StreamSubscription<List<ScanResult>>? _scanSubscription;
BluetoothDevice? _connectedDevice;
void scanForDevices() {
flutterBlue.startScan(timeout: Duration(seconds: 5));
_scanSubscription = flutterBlue.scanResults.listen((results) {
for (ScanResult r in results) {
if (r.device.name.contains("ACR3901U-S1")) {
print("พบอุปกรณ์: ${r.device.name}, ID: ${r.device.id}");
flutterBlue.stopScan();
connectToDevice(r.device);
break;
}
}
});
}
}
Code language: Python (python)
การเชื่อมต่อกับ Smart Card Reader
void connectToDevice(BluetoothDevice device) async {
try {
await device.connect();
_connectedDevice = device;
print("เชื่อมต่อกับอุปกรณ์สำเร็จ");
discoverServices(device);
} catch (e) {
print("เกิดข้อผิดพลาดในการเชื่อมต่อ: $e");
}
}
Code language: Python (python)
ค้นหา Service และ Characteristic
void discoverServices(BluetoothDevice device) async {
List<BluetoothService> services = await device.discoverServices();
for (var service in services) {
for (var characteristic in service.characteristics) {
if (characteristic.properties.read) {
readCardData(characteristic);
}
}
}
}
Code language: Python (python)
อ่านข้อมูลจาก Smart Card
void readCardData(BluetoothCharacteristic characteristic) async {
try {
var value = await characteristic.read();
print("ข้อมูลจากบัตร: ${value.map((e) => e.toRadixString(16)).join(" ")}");
} catch (e) {
print("ไม่สามารถอ่านข้อมูลจากบัตร: $e");
}
}
Code language: Python (python)
ปิดการเชื่อมต่อ
void disconnectDevice() async {
if (_connectedDevice != null) {
await _connectedDevice!.disconnect();
print("ตัดการเชื่อมต่อสำเร็จ");
}
}
Code language: JavaScript (javascript)
สรุป
การเชื่อมต่อ Flutter กับ Smart Card Reader (ACS ACR3901U-S1) ผ่าน Bluetooth สามารถทำได้โดยใช้
flutter_blue_plus
หรือ
flutter_reactive_ble
ขั้นตอนหลักคือ:
- สแกนหาอุปกรณ์ Bluetooth ที่รองรับ
- เชื่อมต่อกับอุปกรณ์ และค้นหา Services
- อ่านข้อมูลจาก Smart Card ผ่าน Bluetooth Characteristic
- ปิดการเชื่อมต่อเมื่อไม่ใช้งาน
การทำงานนี้ช่วยให้สามารถพัฒนาแอปพลิเคชันสำหรับตรวจสอบและอ่านข้อมูลจาก Smart Card ได้ง่ายขึ้น เหมาะสำหรับระบบที่ต้องการความปลอดภัยสูง