Skip to content

Instantly share code, notes, and snippets.

@loisenjoki
Last active April 7, 2020 10:30
Show Gist options
  • Save loisenjoki/0a7519d5adf3b2223c44 to your computer and use it in GitHub Desktop.
Save loisenjoki/0a7519d5adf3b2223c44 to your computer and use it in GitHub Desktop.
NFC Write
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))
ndef.close()
//Write Successful
mTvMessage!!.text = "Success"
Log.e(TAG,"Success")
} catch (e: IOException) {
e.printStackTrace()
mTvMessage!!.text = "something is wrong"
} catch (e: FormatException) {
e.printStackTrace()
mTvMessage!!.text = "something is wrong"
} finally {
mProgress!!.visibility = View.GONE
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment