-
Notifications
You must be signed in to change notification settings - Fork 124
/
main.dart
310 lines (298 loc) · 14.1 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import 'dart:async';
import 'dart:io' show Platform, sleep;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_nfc_kit/flutter_nfc_kit.dart';
import 'package:logging/logging.dart';
import 'package:ndef/ndef.dart' as ndef;
import 'package:ndef/utilities.dart';
import 'ndef_record/raw_record_setting.dart';
import 'ndef_record/text_record_setting.dart';
import 'ndef_record/uri_record_setting.dart';
void main() {
Logger.root.level = Level.ALL; // defaults to Level.INFO
Logger.root.onRecord.listen((record) {
print('${record.level.name}: ${record.time}: ${record.message}');
});
runApp(MaterialApp(theme: ThemeData(useMaterial3: true), home: MyApp()));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
String _platformVersion = '';
NFCAvailability _availability = NFCAvailability.not_supported;
NFCTag? _tag;
String? _result, _writeResult, _mifareResult;
late TabController _tabController;
List<ndef.NDEFRecord>? _records;
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
@override
void initState() {
super.initState();
if (!kIsWeb)
_platformVersion =
'${Platform.operatingSystem} ${Platform.operatingSystemVersion}';
else
_platformVersion = 'Web';
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>[
const SizedBox(height: 20),
Text('Running on: $_platformVersion\nNFC: $_availability'),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () async {
try {
NFCTag tag = await FlutterNfcKit.poll();
setState(() {
_tag = tag;
});
await FlutterNfcKit.setIosAlertMessage(
"Working on it...");
_mifareResult = null;
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.ndefAvailable ?? false) {
var ndefRecords = await FlutterNfcKit.readNDEFRecords();
var ndefString = '';
for (int i = 0; i < ndefRecords.length; i++) {
ndefString += '${i + 1}: ${ndefRecords[i]}\n';
}
setState(() {
_result = ndefString;
});
} else if (tag.type == NFCTagType.webusb) {
var r = await FlutterNfcKit.transceive(
"00A4040006D27600012401");
print(r);
}
} catch (e) {
setState(() {
_result = 'error: $e';
});
}
// Pretend that we are working
if (!kIsWeb) sleep(new Duration(seconds: 1));
await FlutterNfcKit.finish(iosAlertMessage: "Finished!");
},
child: Text('Start polling'),
),
const SizedBox(height: 10),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: _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}\nMifare Info:${_tag!.mifareInfo} Transceive Result:\n$_result\n\nBlock Message:\n$_mifareResult')
: const Text('No tag polled yet.')),
])))),
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
const SizedBox(height: 20),
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 ||
tag.type == NFCTagType.iso15693) {
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 NDEFTextRecordSetting();
}));
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 NDEFUriRecordSetting();
}));
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"),
)
],
),
const SizedBox(height: 10),
Text('Result: $_writeResult'),
const SizedBox(height: 10),
Expanded(
flex: 1,
child: ListView(
shrinkWrap: true,
children: List<Widget>.generate(
_records!.length,
(index) => GestureDetector(
child: Padding(
padding: const EdgeInsets.all(10),
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);
}
}
},
))),
),
]),
)
]),
),
);
}
}