Skip to content

Instantly share code, notes, and snippets.

private fun writeToNfc(ndef: Ndef, message: String) {
mTvMessage!!.text = "write semething"
if (ndef != null) {
try {
ndef.connect()
val mimeRecord = NdefRecord.createMime(
"text/plain",
message.toByteArray(Charset.forName("US-ASCII"))
)
ndef.writeNdefMessage(NdefMessage(mimeRecord))
@loisenjoki
loisenjoki / NFCReadFragment.kt
Last active April 7, 2020 10:34
Read NFC Data
private fun readFromNFC(ndef: Ndef) {
try {
ndef.connect()
val ndefMessage = ndef.ndefMessage
val message = String(ndefMessage.records[0].payload)
Log.d(TAG, "readFromNFC: $message")
mTvMessage.text = message
ndef.close()
} catch (e: IOException) {
e.printStackTrace()