flutter_nfc_kit 3.0.1 flutter_nfc_kit: ^3.0.1 copied to clipboard
Provide NFC functionality on Android and iOS, including reading metadata, read & write NDEF records, and transceive layer 3 & 4 data with NFC tags / cards
import 'dart:async';
import 'dart:io' show Platform, sleep;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_nfc_kit/flutter_nfc_kit.dart';
import 'package:ndef/ndef.dart' as ndef;
import 'record-setting/raw_record_setting.dart';
import 'record-setting/text_record_setting.dart';
import 'record-setting/uri_record_setting.dart';
void main() => runApp(MaterialApp(home: MyApp()));
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
String _platformVersion =
'${Platform.operatingSystem} ${Platform.operatingSystemVersion}';
NFCAvailability _availability = NFCAvailability.not_supported;
NFCTag? _tag;
String? _result, _writeResult;
TabController? _tabController;
List<ndef.NDEFRecord>? _records;
@override
void dispose() {
_tabController!.dispose();
super.dispose();
}
@override
void initState() {
super.initState();
initPlatformState();
_tabController = new TabController(length: 2, vsync: this);
_records = [];
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
NFCAvailability availability;
try {
availability = await FlutterNfcKit.nfcAvailability;
} on PlatformException {
availability = NFCAvailability.not_supported;
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
// _platformVersion = platformVersion;
_availability = availability;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('NFC Flutter Kit Example App'),
bottom: TabBar(
tabs: <Widget>[
Tab(text: 'read'),
Tab(text: 'write'),
],
controller: _tabController,
)),
body: new TabBarView(controller: _tabController, children: <Widget>[
Scrollbar(
child: SingleChildScrollView(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Running on: $_platformVersion\nNFC: $_availability'),
ElevatedButton(
onPressed: () async {
try {
NFCTag tag = await FlutterNfcKit.poll();
setState(() {
_tag = tag;
});
await FlutterNfcKit.setIosAlertMessage(
"working on it...");
if (tag.standard == "ISO 14443-4 (Type B)") {
String result1 =
await FlutterNfcKit.transceive("00B0950000");
String result2 = await FlutterNfcKit.transceive(
"00A4040009A00000000386980701");
setState(() {
_result = '1: $result1\n2: $result2\n';
});
} else if (tag.type == NFCTagType.iso18092) {
String result1 =
await FlutterNfcKit.transceive("060080080100");
setState(() {
_result = '1: $result1\n';
});
} else if (tag.type == NFCTagType.mifare_ultralight ||
tag.type == NFCTagType.mifare_classic) {
var ndefRecords = await FlutterNfcKit.readNDEFRecords();
var ndefString = ndefRecords
.map((r) => r.toString())
.reduce((value, element) => value + "\n" + element);
setState(() {
_result = '1: $ndefString\n';
});
}
} catch (e) {
setState(() {
_result = 'error: $e';
});
}
// Pretend that we are working
sleep(new Duration(seconds: 1));
await FlutterNfcKit.finish(iosAlertMessage: "Finished!");
},
child: Text('Start polling'),
),
_tag != null
? Text(
'ID: ${_tag!.id}\nStandard: ${_tag!.standard}\nType: ${_tag!.type}\nATQA: ${_tag!.atqa}\nSAK: ${_tag!.sak}\nHistorical Bytes: ${_tag!.historicalBytes}\nProtocol Info: ${_tag!.protocolInfo}\nApplication Data: ${_tag!.applicationData}\nHigher Layer Response: ${_tag!.hiLayerResponse}\nManufacturer: ${_tag!.manufacturer}\nSystem Code: ${_tag!.systemCode}\nDSF ID: ${_tag!.dsfId}\nNDEF Available: ${_tag!.ndefAvailable}\nNDEF Type: ${_tag!.ndefType}\nNDEF Writable: ${_tag!.ndefWritable}\nNDEF Can Make Read Only: ${_tag!.ndefCanMakeReadOnly}\nNDEF Capacity: ${_tag!.ndefCapacity}\n\n Transceive Result:\n$_result')
: Text('No tag polled yet.')
])))),
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
ElevatedButton(
onPressed: () async {
if (_records!.length != 0) {
try {
NFCTag tag = await FlutterNfcKit.poll();
setState(() {
_tag = tag;
});
if (tag.type == NFCTagType.mifare_ultralight ||
tag.type == NFCTagType.mifare_classic) {
await FlutterNfcKit.writeNDEFRecords(_records!);
setState(() {
_writeResult = 'OK';
});
} else {
setState(() {
_writeResult =
'error: NDEF not supported: ${tag.type}';
});
}
} catch (e, stacktrace) {
setState(() {
_writeResult = 'error: $e';
});
print(stacktrace);
} finally {
await FlutterNfcKit.finish();
}
} else {
setState(() {
_writeResult = 'error: No record';
});
}
},
child: Text("Start writing"),
),
ElevatedButton(
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return SimpleDialog(
title: Text("Record Type"),
children: <Widget>[
SimpleDialogOption(
child: Text("Text Record"),
onPressed: () async {
Navigator.pop(context);
final result = await Navigator.push(
context, MaterialPageRoute(
builder: (context) {
return TextRecordSetting();
}));
if (result != null) {
if (result is ndef.TextRecord) {
setState(() {
_records!.add(result);
});
}
}
},
),
SimpleDialogOption(
child: Text("Uri Record"),
onPressed: () async {
Navigator.pop(context);
final result = await Navigator.push(
context, MaterialPageRoute(
builder: (context) {
return UriRecordSetting();
}));
if (result != null) {
if (result is ndef.UriRecord) {
setState(() {
_records!.add(result);
});
}
}
},
),
SimpleDialogOption(
child: Text("Raw Record"),
onPressed: () async {
Navigator.pop(context);
final result = await Navigator.push(
context, MaterialPageRoute(
builder: (context) {
return NDEFRecordSetting();
}));
if (result != null) {
if (result is ndef.NDEFRecord) {
setState(() {
_records!.add(result);
});
}
}
},
),
]);
});
},
child: Text("Add record"),
)
],
),
Text('Result: $_writeResult'),
Expanded(
flex: 1,
child: ListView(
shrinkWrap: true,
children: List<Widget>.generate(
_records!.length,
(index) => GestureDetector(
child: Text(
'id:${_records![index].idString}\ntnf:${_records![index].tnf}\ntype:${_records![index].type?.toHexString()}\npayload:${_records![index].payload?.toHexString()}\n'),
onTap: () async {
final result = await Navigator.push(context,
MaterialPageRoute(builder: (context) {
return NDEFRecordSetting(
record: _records![index]);
}));
if (result != null) {
if (result is ndef.NDEFRecord) {
setState(() {
_records![index] = result;
});
} else if (result is String &&
result == "Delete") {
_records!.removeAt(index);
}
}
},
))),
),
]),
)
]),
),
);
}
}