NFC reader API for tools - #158
Conversation
LightNfc exposes NFC availability and hands out LightNfcReader, which provides taps as a Flow<LightNfcTap> and holds Android's reader mode for exactly as long as it is collected. Tags decode to typed NDEF records — Uri, Text, or Binary, classified by TNF — with the tag UID always available, so a bare UID badge reads successfully with no records. Reader mode is ref-counted per Activity: concurrent collectors share the single mode the platform grants, the newest receives taps, earlier ones resume as later ones stop, and the last to stop releases the radio. Holding it longer than needed is not harmless — while reader mode is on, no other app sees taps. LightNfcTapReader is the ready-made prompt, the NFC counterpart to LightQrCodeScanner. android.permission.NFC is already on the tool allow-list, and this adds no new dependency.
dupontgu
left a comment
There was a problem hiding this comment.
This is awesome and works great. Just a few tiny things if you don't mind.
The other thing I wanted to mention is that the LP3 has kind of a weird behavior if you scan a tag that would normally be "launchable" by Android itself (like a normal https link). It plays the scanning sound and often freezes LightOS? Dunno if you've tried that. Anyways - no action on your part, we need to fix that in a future firmware update, but I may add a note to the docs after this is merged.
| val onTapState = rememberUpdatedState(onTap) | ||
| var availability by remember { mutableStateOf(LightNfcAvailability.Unsupported) } | ||
| var readFailureMessage by remember { mutableStateOf<String?>(null) } | ||
| var readerRestarts by remember { mutableStateOf(0) } |
There was a problem hiding this comment.
nit - im getting an IDE reminder to use mutableIntStateOf here
| val lifecycleOwner = LocalLifecycleOwner.current | ||
| val nfc = rememberLightNfc() | ||
| val onTapState = rememberUpdatedState(onTap) | ||
| var availability by remember { mutableStateOf(LightNfcAvailability.Unsupported) } |
There was a problem hiding this comment.
One note using this is that, after I add the permission, whenever I show LightNfcTabReader, I can see the text that maps to this initial Unsupported value flash very briefly. Can I request we either start with null here or add LightNfcAvailability.Unknown (or Empty maybe?) and have that value map to an empty string or otherwise invisible prompt message? I think the loading of the nfc availability takes juuuust enough time for that first value to render.
There was a problem hiding this comment.
on the changes, we'll get that invisible. thx.
The tap reader started at Unsupported, so its message rendered for the frame or two before the real availability arrived. Start at no value and add a tap state that shows nothing until one is known. Also use mutableIntStateOf for the restart counter.
|
Changes are live. |
dupontgu
left a comment
There was a problem hiding this comment.
Beautiful! Thank you. I'll merge now.
|
Woot! Thanks for the review. Bringing the other one up to latest main. |
Closes #156.