Androidプログラマへの道 〜 Moonlight 明日香 〜 - Ndefメッセージを取得する
NFCカード/タグのNdefメッセージを取得するには, NFCカード/タグがNdefに対応している必要があるので, インテント"android.nfc.action.NDEF_DISCOVERED"が発行されることを利用する.
注) Ndefに未対応のNFCカード/タグでは, インテント"android.nfc.action.NDEF_DISCOVERED"は発行されない.

Ndefメッセージの取得

  • Nfc03.java
    • Intent#getIntentメソッドで, Intentのインスタンスを取得する.
    • インテントのアクションが, "ACTION_NDEF_DISCOVERED"かチェックする.
    • Intent#getParcelableArrayExtraメソッドに, NfcAdapter.EXTRA_NDEF_MESSAGEを指定して, カード内の情報を取得する.
    • NdefMessage#getRecordsメソッドで, Ndefメッセージ内のレコードを取得する.
    • NdefRecord#getTypeメソッド, getTnfメソッドで, レコードのデータ構造を知る.
    • NdefRecord#getPayloadメソッドで, レコード内のペイロードを取得する.
  注) タグタイプによりデータ構造が異なるので, 各タイプの仕様書を確認のこと.
package com.moonlight_aska.android.nfc03;

import android.app.Activity;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.TextView;

public class Nfc03 extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nfc03);

    TextView text = (TextView)findViewById(R.id.tag_id);
    // インテントの取得
    Intent intent = getIntent();
    // NDEF対応カードの検出かチェック
    String action = intent.getAction();
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
      // Ndefメッセージの取得
      Parcelable[] raws = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
      NdefMessage[] msgs = new NdefMessage[raws.length];
      String str = "";
      for (int i=0; i<raws.length; i++) {
        msgs[i] = (NdefMessage)raws[i];
        for (NdefRecord record : msgs[i].getRecords()) {
          str += "Type : " + new String(record.getType()) + "\n";
          str += "TNF : " + record.getTnf() + "\n";
          byte[] payload = record.getPayload();
          if (payload == null)
            break;
          int idx = 0;
          for(byte data : payload) {
            str += String.format("Payload[%2d] : 0x%02x / %c\n", idx, data, data);
            idx++;
          }
        }
      }
      text.setText(str);
    }
  }
}
  • AndroidManifest.xml
    • NFCを使用する場合, "android.permission.NFC"のパーミッションを設定する.
    • インストール時に, NFCデバイスが必要であることを設定する.
    • "android.nfc.action.NDEF_DISCOVERED"でアプリが起動するよう, インテントフィルタを設定する.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.moonlight_aska.android.nfc03"
  android:versionCode="1"
  android:versionName="1.0" >

  <uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="15" />
  <uses-permission android:name="android.permission.NFC"/>
  <uses-feature android:name="android.hardware.nfc"
    android:required="true"/>

  <application}
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
      android:name=".Nfc03"
      android:label="@string/title_activity_nfc03" >
      <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
      </intent-filter>
    </activity>
  </application>
</manifest>
  • 動作例
MiniTrack NXP NTAG203
    • タグタイプ : TEXT
    • 言語 : 英語(en)
    • データ : This is a test tag. (事前に書き込んだデータ)