rfid 0.0.26
rfid: ^0.0.26 copied to clipboard
RFID scanner plugin for integration in flutter applications. Package currently supports IOS platforms
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:rfid/enum/enum.dart';
import 'package:rfid/model/rifd_connection_result/rifd_connection_result.dart';
import 'package:rfid/rfid.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: "first",
routes: {
"first": (context) => Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: const MyHomePage(title: 'Test'),
),
"second": (context) => Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
actions: [
IconButton(
onPressed: () {
Navigator.popAndPushNamed(context, "first");
},
icon: const Icon(Icons.back_hand))
],
),
body: const SizedBox(),
)
},
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
RfidConnectionResult? _connectionResult;
String? _connectedDevices;
String? _serialNumber;
final _rfidpluginPlugin = RfidPlugin();
String? _batteryHealth;
@override
void initState() {
_getBatteryLevel();
_rfidpluginPlugin.deviceConnectionChangeStream.listen((event) {
_getBatteryLevel();
});
_rfidpluginPlugin.equipmentScanChangeStream.listen((event) {
debugPrint("Signal Strength ${event.signalStrength.toString()}");
debugPrint("Rssi ${event.rssi.toString()}");
});
_rfidpluginPlugin.switchStateChangeStream.listen((event) {
debugPrint(event.toString());
});
super.initState();
}
void scanBarCode() async {
await _rfidpluginPlugin.scanBarCode();
}
void _getBatteryLevel() async {
try {
// final connectedDevices = await _rfidpluginPlugin.getAvailableDevices();
String? serialNumber = "1128-IN-001022";
// if (connectedDevices.isNotEmpty) {
// serialNumber = connectedDevices.first.serialNumber;
// }
final RfidConnectionResult connectionResult =
await _rfidpluginPlugin.connectDevice(serialNumber: serialNumber);
// final status = await _rfidpluginPlugin.getDeviceConnectionStatus(
// serialNumber: serialNumber);
// debugPrint(status.toString());
// final batteryHealth =
// await _rfidpluginPlugin.getBatteryHealth(serialNumber: serialNumber);
setState(() {
_connectionResult = connectionResult;
_serialNumber = serialNumber;
_connectedDevices = "1";
_batteryHealth = "-";
});
} on PlatformException catch (_) {
setState(() {});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Active Device ${_serialNumber ?? 'No Active Device'}',
),
Text(
'Connection Status ${_connectionResult?.connectionResult.toString()}',
),
Text(
'Connected Devices count ${_connectedDevices ?? 'No Devices Connected'}',
),
Text(
'Connected Device battery health ${_batteryHealth ?? 'No Devices Connected'}',
),
ElevatedButton(
onPressed: () {
Navigator.popAndPushNamed(context, "second");
},
child: const Text('Navigate to second screen'),
),
ElevatedButton(
onPressed: scanBarCode,
child: const Text('Scan BarCode'),
),
ElevatedButton(
onPressed: () async {
final status = await _rfidpluginPlugin.getDeviceConnectionStatus(
serialNumber: "1128-IN-001022");
debugPrint(status.toString());
},
child: const Text('Get device connection status'),
),
ElevatedButton(
onPressed: () async {
await _rfidpluginPlugin.triggerBatteryCommand(
serialNumber: "1128-IN-001022");
},
child: const Text('Get device battery status'),
),
ElevatedButton(
onPressed: () async {
final result = await _rfidpluginPlugin.setPower(power: 20);
debugPrint(result.toString());
final secondResult = await _rfidpluginPlugin.setPower(power: 28);
debugPrint(secondResult.toString());
},
child: const Text('Set device power'),
),
IconButton(
onPressed: () async {
await _rfidpluginPlugin.disconnect();
},
icon: const Icon(Icons.error)),
IconButton(
onPressed: () async {
await _rfidpluginPlugin.alertCommand(
duration: RfidAlertDuration.long);
},
icon: const Icon(Icons.alarm)),
],
),
);
}
}