forked from nfcim/flutter_nfc_kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.dart
More file actions
450 lines (433 loc) · 20 KB
/
Copy pathmain.dart
File metadata and controls
450 lines (433 loc) · 20 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
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
State createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
String _platformVersion = '';
NFCAvailability _availability = NFCAvailability.not_supported;
NFCTag? _tag;
String? _result, _writeResult, _mifareResult;
String? _sessionErrorResult;
bool _streaming = false;
int _streamCount = 0;
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 = TabController(length: 2, vsync: this);
_records = [];
FlutterNfcKit.tagStream.listen((tag) {
setState(() {
_tag = tag;
print(_tag);
});
});
}
// 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;
});
}
// A benign command used to keep the session busy. Exact bytes depend on the
// tag; unsupported-command errors are ignored while streaming, so any tag works.
String _dummyCommandFor(NFCTag tag) {
if (tag.type == NFCTagType.iso18092) return "060080080100";
return "00A4040000"; // ISO7816 SELECT (no AID)
}
// Validates iOS session-invalidation errors by keeping the session genuinely
// busy: after polling, it continuously transceives dummy data until you stop
// it (tap again) or a session error fires. Tap Cancel on the iOS sheet while
// streaming to see UserCanceled (409) surface mid-transceive. SystemIsBusy
// (503) may also surface here. Both are CoreNFC-only (iOS).
Future<void> _streamDummyData() async {
if (_streaming) {
setState(() => _streaming = false); // request stop
return;
}
setState(() {
_streaming = true;
_streamCount = 0;
_sessionErrorResult = 'Polling…';
});
try {
NFCTag tag = await FlutterNfcKit.poll(
iosAlertMessage: "Streaming — tap Cancel to test UserCanceled");
final dummy = _dummyCommandFor(tag);
while (_streaming) {
try {
await FlutterNfcKit.transceive(dummy);
} on PlatformException catch (e) {
// Only a transient tag-level comm error (500, e.g. the tag rejecting
// our dummy command) is ignored so streaming continues. Any other code
// means the session ended — canceled (409), busy (503), timed out
// (408), terminated unexpectedly (502), or no longer active (406) —
// so stop and report it instead of spinning.
if (e.code != '500') rethrow;
}
setState(() {
_streamCount++;
_sessionErrorResult = 'Streaming dummy data… ($_streamCount sent)';
});
await Future.delayed(const Duration(milliseconds: 100));
}
await FlutterNfcKit.finish(iosAlertMessage: "Stopped");
setState(() =>
_sessionErrorResult = 'Stopped after $_streamCount commands.');
} on PlatformException catch (e) {
setState(() => _sessionErrorResult = 'code=${e.code} message=${e.message}');
} finally {
setState(() => _streaming = false);
}
}
// Drives the real SystemIsBusy (503) flow: stream dummy data until iOS kicks
// us off (502 SessionTerminatedUnexpectedly), then retry immediately — too
// soon, so iOS returns SystemIsBusy (503). It then backs off and retries to
// show the session recovers. iOS-only (CoreNFC).
Future<void> _testSystemIsBusy() async {
setState(() => _sessionErrorResult = 'Streaming until iOS kicks us off…');
try {
// 1. Hold the session busy until iOS terminates it.
NFCTag tag = await FlutterNfcKit.poll(
iosAlertMessage: "Hold still until kicked off…");
final dummy = _dummyCommandFor(tag);
var sent = 0;
while (true) {
try {
await FlutterNfcKit.transceive(dummy);
setState(() =>
_sessionErrorResult = 'Streaming… (${++sent} sent), waiting to be kicked off');
} on PlatformException catch (e) {
if (e.code == '500') continue; // transient tag error, keep streaming
// Session ended (expected: 502 kicked off) — break out to retry.
setState(() => _sessionErrorResult =
'Kicked off after $sent: code=${e.code} (${e.message}). Retrying instantly…');
break;
}
}
// 2. Retry immediately — too soon, so iOS should report SystemIsBusy.
try {
await FlutterNfcKit.poll(
timeout: const Duration(seconds: 5), iosAlertMessage: "Instant retry");
await FlutterNfcKit.finish();
setState(() => _sessionErrorResult =
'Instant retry unexpectedly succeeded (no SystemIsBusy this run).');
return;
} on PlatformException catch (e) {
setState(() => _sessionErrorResult =
'Instant retry → code=${e.code} message=${e.message}');
if (e.code != '503') return; // demo only continues if we actually got busy
}
// 3. Back off, then retry — the session should now recover.
await Future.delayed(const Duration(seconds: 3));
await FlutterNfcKit.poll(iosAlertMessage: "Retry after backoff");
await FlutterNfcKit.finish();
setState(() => _sessionErrorResult =
'Got 503 SystemIsBusy on instant retry; recovered after 3s backoff. ✓');
} on PlatformException catch (e) {
setState(() => _sessionErrorResult = 'code=${e.code} message=${e.message}');
}
}
@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: 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(Duration(seconds: 1));
await FlutterNfcKit.finish(iosAlertMessage: "Finished!");
},
child: Text('Start polling'),
),
const SizedBox(height: 10),
const Divider(),
const Text('Session error test (iOS)'),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: _streamDummyData,
child:
Text(_streaming ? 'Stop streaming' : 'Stream dummy data'),
),
const SizedBox(width: 10),
ElevatedButton(
onPressed: _streaming ? null : _testSystemIsBusy,
child: const Text('Test SystemIsBusy'),
),
],
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Text(_sessionErrorResult == null
? 'Stream dummy data to keep the session busy, then tap Cancel '
'on the iOS sheet to see UserCanceled. SystemIsBusy is '
'OS-driven and may not reproduce every run.'
: 'Session error: $_sessionErrorResult')),
const Divider(),
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}\nTransceive 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!.isNotEmpty) {
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);
}
}
},
))),
),
]),
)
]),
),
);
}
}