flutter nfc reader
a new flutter plugin to help developers looking to use internal hardware inside ios or android devices for reading nfc tags.
the system activate a pooling reading session that stops automatically once a tag has been recognised. you can also trigger the stop event manually using a dedicated function.
installation nfc reader
add to pubspec.yaml:
dependencies:
flutter_nfc_reader:
git:
url: git://github.com/matteocrippa/flutter-nfc-reader.git
ref: master
and then run the shell
flutter packages get
last step import to the project:
import 'package:flutter_nfc_reader/flutter_nfc_reader.dart';
how to use nfc reader
android setup
add those two lines to your androidmanifest.xml
on the top
<uses-permission android:name="android.permission.nfc" />
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
ios setup
atm only swift
based flutter project are supported.
- enable capabilities / near field communication tag reading.
- info.plist file, add privacy – nfc scan usage description with string value nfc tag.
read nfc
this function will return a promise when a read occurs, till that very moment the reading session is open.
the promise will return a nfcdata
model, this model contains:
- id > id of the tag
- content > content of the tag
- error > if any error occurs
future<void> startnfc() async {
nfcdata response;
setstate(() {
_nfcdata = nfcdata();
_nfcdata.status = nfcstatus.reading;
});
print('nfc: scan started');
try {
print('nfc: scan readed nfc tag');
response = await flutternfcreader.read;
} on platformexception {
print('nfc: scan stopped exception');
}
setstate(() {
_nfcdata = response;
});
}
stop nfc
future<void> stopnfc() async {
nfcdata response;
try {
print('nfc: stop scan by user');
response = await flutternfcreader.stop;
} on platformexception {
print('nfc: stop scan exception');
response = nfcdata(
id: '',
content: '',
error: 'nfc scan stop exception',
statusmapper: '',
);
response.status = nfcstatus.error;
;
}
setstate(() {
_nfcdata = response;
});
}
for better details look at the demo app.
Comments are closed.