Skip to content

Instantly share code, notes, and snippets.

@KazuyukiEguchi
Last active June 17, 2018 01:09
Show Gist options
  • Save KazuyukiEguchi/120601dd647580807d2c1471dd2cd360 to your computer and use it in GitHub Desktop.
Save KazuyukiEguchi/120601dd647580807d2c1471dd2cd360 to your computer and use it in GitHub Desktop.
Android端末を使って、NFC Tagに書き込むプログラムをkotlinで書いてみた ref: https://qiita.com/KazuyukiEguchi/items/8fe6b4b55ef08cc1db93
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jp.eguchi.android.nfc_writer">
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
override fun onNewIntent(intent: Intent) {
Log.d(TAG,"onNewIntent")
if (intent == null) {
return
}
Log.d(TAG,"Action" + intent.action)
if(NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.action)) {
val tag = intent.getParcelableExtra<Tag>(NfcAdapter.EXTRA_TAG)
if (tag == null) {
return
}
val ndef = Ndef.get(tag);
if(ndef == null) {
return;
}
val raws = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if(raws == null){
return;
}
var msgs = arrayOfNulls<NdefMessage>(raws.size)
for(i in 0..raws.size-1) {
msgs[i] = raws.get(i) as NdefMessage?
for(records in msgs) {
for(record in records?.records!!){
Log.d(TAG,"TNF=" + record.tnf)
Log.d(TAG,"mime=" + record.toMimeType())
Log.d(TAG,"payload=" + String(record.payload));
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment