dtb_link 0.0.15+21
dtb_link: ^0.0.15+21 copied to clipboard
DTB Link - utility app enabling applications to connect and integrate with NFC card readers over Bluetooth
example/lib/main.dart
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:dtb_link/dtb_link.dart';
enum EasyKey { sKey, echo, saleTran, voidTran }
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
DtbLink dtbLink = DtbLink();
DataInitRet? initData;
List<BluetoothDevice> deviceList = [];
TextEditingController controller = TextEditingController();
ScrollController scrollController = ScrollController();
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
dtbLink.terminalInitData().then((data) {
debugPrint("initData: ${data?.toJson()}");
initData = data;
});
dtbLink.onEvent((onData) {
debugPrint("onData: $onData");
Map<String, dynamic> retData = json.decode(onData);
if (retData["command"] == "bluetooth.device_found") {
String? deviceId = retData["ret"]?["device_id"];
String? deviceName = retData["ret"]?["device_name"];
if (deviceId != null && deviceName != null) {
final device = BluetoothDevice(deviceId: deviceId, deviceName: deviceName);
if (!deviceList.contains(device)) {
setState(() {
deviceList.add(device);
addMessage("${device.toJson()}");
});
}
}
} else if (retData["command"] == "bluetooth.download_file.pregress") {
int? current = retData["ret"]?["current"];
int? total = retData["ret"]?["total"];
debugPrint("current: $current, total: $total");
}
});
initPlatformState();
});
}
Future<void> initPlatformState() async {
String platformVersion;
try {
debugPrint("platformVersion before");
platformVersion = await dtbLink.getPlatformVersion() ?? 'Unknown platform version';
debugPrint("platformVersion: $platformVersion");
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("DTB Link Flutter Demo $_platformVersion")),
body: Padding(
padding: const EdgeInsets.all(10),
child: Column(
children: [
Expanded(
child: SingleChildScrollView(
child: Column(
children: [
Card(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Wrap(
runSpacing: 6,
children: [
const Text("Bluetooth"),
ElevatedButton(
onPressed: () {
deviceList.clear();
addMessage("=>search device");
dtbLink.bluetoothStartSearch();
},
style: ElevatedButton.styleFrom(minimumSize: const Size.fromHeight(50)),
child: const Text("Bluetooth Search"),
),
ElevatedButton(
onPressed: () {
addMessage("=>isConnected");
dtbLink.bluetoothIsConnected().then((result) {
addMessage("<=$result");
});
},
style: ElevatedButton.styleFrom(minimumSize: const Size.fromHeight(50)),
child: const Text("Bluetooth is connected"),
),
ElevatedButton(
onPressed: () {
addMessage("=>Contactless upload");
dtbLink.bluetoothContactlessFileUpload().then((result) {
addMessage("<=$result");
});
},
style: ElevatedButton.styleFrom(minimumSize: const Size.fromHeight(50)),
child: const Text("Bluetooth Contactless upload"),
),
ElevatedButton(
onPressed: () {
addMessage("=>Contactless upload");
dtbLink.bluetoothEmvFileUpload().then((result) {
addMessage("<=$result");
});
},
style: ElevatedButton.styleFrom(minimumSize: const Size.fromHeight(50)),
child: const Text("Bluetooth EMV upload"),
),
ElevatedButton(
onPressed: () {
addMessage("=>Device data");
dtbLink.bluetoothDeviceData().then((device) {
addMessage("<=${device.toJson()}");
});
},
style: ElevatedButton.styleFrom(minimumSize: const Size.fromHeight(50)),
child: const Text("Bluetooth Device Data"),
),
ElevatedButton(
onPressed: () {
addMessage("=>Reconnect Device");
dtbLink.bluetoothReconnectDevice().then((device) {
addMessage("<=${device?.toJson()}");
});
},
style: ElevatedButton.styleFrom(minimumSize: const Size.fromHeight(50)),
child: const Text("Bluetooth Reconnect Device"),
),
ElevatedButton(
onPressed: () {
addMessage("=>card read");
dtbLink.traceNo.then((traceNo) {
dtbLink
.bluetoothCardRead(amount: "500.00", tranType: 1, traceNo: traceNo)
.then((ret) {
addMessage("<=$ret");
});
});
},
style: ElevatedButton.styleFrom(minimumSize: const Size.fromHeight(50)),
child: const Text("Bluetooth card read"),
),
],
),
),
),
Card(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Wrap(
runSpacing: 6,
children: [
const Text("Terminal"),
ElevatedButton(
onPressed: () {
addMessage("=>Echo test");
dtbLink.terminalEchoTest().then((ret) {
addMessage("<=$ret");
});
},
style: ElevatedButton.styleFrom(minimumSize: const Size.fromHeight(50)),
child: const Text("Echo Test"),
),
ElevatedButton(
onPressed: () {
addMessage("=>Init Data");
dtbLink.terminalInitData().then((ret) {
addMessage("<=${ret?.toJson()}");
});
},
style: ElevatedButton.styleFrom(minimumSize: const Size.fromHeight(50)),
child: const Text("Init Data"),
),
ElevatedButton(
onPressed: () {
addMessage("=>Init terminal");
dtbLink.terminalDoInit(terminalId: "", trc: "").then((ret) {
addMessage("<=${ret.toJson()}");
});
},
style: ElevatedButton.styleFrom(minimumSize: const Size.fromHeight(50)),
child: const Text("Init Terminal"),
),
ElevatedButton(
onPressed: () {
addMessage("=>do sale");
dtbLink.doSale(amount: 500).then((ret) {
addMessage("<=$ret");
});
},
style: ElevatedButton.styleFrom(minimumSize: const Size.fromHeight(50)),
child: const Text("Do Sale"),
),
],
),
),
)
],
),
),
),
// Text
Container(
constraints: const BoxConstraints(maxHeight: 200),
child: SingleChildScrollView(
controller: scrollController,
child: TextField(
maxLines: null,
enabled: false,
controller: controller,
),
),
),
],
),
),
),
);
}
addMessage(String msg) {
controller.text = "${controller.text}\n$msg\n";
Future.delayed(const Duration(milliseconds: 200), () {
scrollController.animateTo(scrollController.position.maxScrollExtent,
duration: const Duration(milliseconds: 200), curve: Curves.easeInOut);
});
}
}