LynxNfc
Reads and writes NFC tags: a membership card, a museum label, a smart-home tag or a device you're setting up. import { LynxNfc } from '@blotch/angular-lynx';
An NFC tag holds an NDEF message: a short list of records, such as a link or some text. LynxNfc reads those records and writes new ones.
Turn it on
Add permissions.nfc to the build target's options in angular.json, with the reason your app reads tags:
What it adds:
- iOS:
NFCReaderUsageDescription, and the NFC Tag Reading entitlement. - Android: the
NFCpermission, granted at install. The NFC feature is declared not required, so Google Play still offers the app to phones without NFC.
Without it, the native code is left out of both apps. state is then unsupported, and every call rejects with unsupported.
A free Personal Team can't sign the NFC Tag Reading entitlement. Set ios.teamId to a team in the paid Apple Developer Program to run the app on an iPhone. The Simulator needs no team, but it has no NFC reader.
See Turn on device features for every setting.
Usage
On iOS, read() opens the system's scan sheet with your alertMessage. On Android there's no sheet: show your own "Hold your phone near the tag" while the call is open.
Write a tag
write() replaces what the next tag holds.
- A locked tag rejects with
readOnly. - Records that don't fit reject with
tooLarge. Only the tag can tell. - No records, or an invalid one, throw a
TypeErrorbefore any tag comes near. Amimerecord without a media type is invalid, for example.
Read several tags
scan() calls back with each tag until you call stop(), or until the scan ends by itself:
- the person closes iOS's sheet (
canceled) - it times out (
timeout) - the system fails (
failed)
Its error signal then holds the reason, and scanning turns false.
A tag it can't read is skipped, and a tag held near the phone may be read again. On iOS the sheet stays up for the whole scan.
Methods
Only one call is open at a time: another rejects with busy. state is asked again each time the app comes back to the foreground, so it turns ready after the person turns NFC on in Settings.
NfcSessionOptions
NfcTag
NfcRecord
NfcError
Every call rejects with an NfcError (apart from write()'s TypeError for invalid records), whose code is one of:
Platform support
Testing
The testing library fakes the reader. tapNfcTag() brings a tag near the phone:
Related
- Permission reasons: the
nfcreason and the otherpermissions - Turn on device features: every opt-in build option
LynxBarcodeScanner: read a QR code with the camera instead
