Skip to content

NFC reader API for tools - #158

Merged
dupontgu merged 2 commits into
lightphone:mainfrom
BoltyBolterson:johnny/feat/nfc-reader
Aug 13, 2026
Merged

NFC reader API for tools#158
dupontgu merged 2 commits into
lightphone:mainfrom
BoltyBolterson:johnny/feat/nfc-reader

Conversation

@BoltyBolterson

Copy link
Copy Markdown
Contributor

Closes #156.

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 dupontgu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@BoltyBolterson

Copy link
Copy Markdown
Contributor Author

Changes are live.

@dupontgu dupontgu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful! Thank you. I'll merge now.

@dupontgu
dupontgu merged commit 2264cad into lightphone:main Aug 13, 2026
1 check passed
@BoltyBolterson

Copy link
Copy Markdown
Contributor Author

Woot! Thanks for the review. Bringing the other one up to latest main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NFC reader API for tools

2 participants