KNFC is a lightweight Kotlin Multiplatform library designed to simplify common NFC tag reading and writing operations across Android and iOS, abstracting away the platform-specific complexities of NfcAdapter and CoreNFC.
The library uses KNFC to set up all required parameters before reading or writing. Configuration is split into two main blocks: Common (shared configuration) and Platform (Android/iOS specific configuration).
val knfc =
KNFC {
withConfiguration {
requireNdefSupport(true)
requireTechnologies(NfcTech.Ndef)
withTimeout(10.toDuration(DurationUnit.SECONDS))
}
withPlatformConfiguration {
configurePlatform()
}
} Once the client is built, you can use the functions from the KNFC interface in your shared code:
| Method | Type | Description |
|---|---|---|
read(): Flow<NFCTag> |
Shared | Starts an NFC reading session and returns a Flow of discovered NFCTag objects. |
write() |
Shared | Suspends until an NDEF message is successfully written to a discovered tag. |
| Method | Type | Description |
|---|---|---|
withTimeout(duration: Duration) |
Shared | Sets the global timeout for the NFC session. |
requireTechnologies(vararg techs: NfcTech) |
Shared | Adds one or more required NFC technologies for tag filtering. |
requireNdefSupport(isRequired: Boolean) |
Shared | A quick way to ensure the detected tag must contain valid NDEF data. |