Carlo De Chellis
09/26/2023, 2:32 PMprivate var session: NFCTagReaderSession? = null
private var tag: NFCISO7816TagProtocol? = null
override fun connect(iosMessage: String, actionDone: () -> Unit) {
session = NFCTagReaderSession(NFCPollingISO14443, NfcReaderIos(readingInterface) { ok, mTag ->
if (ok)
tag = mTag
}, null)
session?.alertMessage = iosMessage
session?.beginSession()
}
private class NfcReaderIos(
private val readingInterface: NfcReading,
private val itsOk: (ok: Boolean, tag: NFCISO7816TagProtocol?) -> Unit
) : NSObject(), NFCTagReaderSessionDelegateProtocol {
override fun tagReaderSessionDidBecomeActive(session: NFCTagReaderSession) {
Log.i("NFC TAG", "ACTIVE")
}
override fun tagReaderSession(session: NFCTagReaderSession, didDetectTags: List<*>) {
val tag = didDetectTags.firstOrNull() as? NFCISO7816TagProtocol
if (tag == null)
itsOk.invoke(false, null)
else {
session.connectToTag(tag) {
if (it != null) {
readingInterface.onTransmit("connected")
itsOk.invoke(true, tag)
}
}
}
}
override fun tagReaderSession(session: NFCTagReaderSession, didInvalidateWithError: NSError) {
Log.e("IOS ERROR", didInvalidateWithError.code.toString())
}
}