▼Androidメモ▼

NFCの利用



NFCを利用するプログラムを作成する。
【参考】トップゲートさんのNFC解説ページ


ライブラリの追加
オープンソースライブラリ「guava-library」を使用する。「guava-r07.jar」をダウンロードしてパスを通す。ライブラリの追加方法は「Package Explorerのプロジェクト名を右クリック→Properties→Java Build Path→Libraries」でAdd JARsボタン。

ソースコード
NFCEx.java
package net.npaka.nfcex;
import com.google.common.primitives.UnsignedBytes;
import android.app.Activity;
import android.graphics.Color;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

//NFCの利用
public class NFCEx extends Activity {
    private final static int WC=LinearLayout.LayoutParams.WRAP_CONTENT;
    private TextView textView;
    
    //アクティビティ起動時に呼ばれる
    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //レイアウトの生成
        LinearLayout layout=new LinearLayout(this);
        layout.setBackgroundColor(Color.rgb(255,255,255));
        layout.setOrientation(LinearLayout.VERTICAL);
        setContentView(layout); 

        //テキストビューの生成
        textView=new TextView(this);
        textView.setTextSize(16.0f);
        textView.setTextColor(Color.rgb(0,0,0));
        textView.setLayoutParams(new LinearLayout.LayoutParams(WC,WC));
        layout.addView(textView);
        
    }
    
    //アプリ開始時に呼ばれる
    @Override
    public void onResume() {
        super.onResume();
        try {
            //FeliCa IDmの取得
            byte[] mID=getIntent().getByteArrayExtra(NfcAdapter.EXTRA_ID);             
            
            //バイトデータを16進数文字列に変換
            String hexStr=data2hex(mID);
            textView.setText("NFCEx>"+hexStr);
        } catch (Exception e) {
            textView.setText("NFCEx>");
        }
    }
    
    //バイトデータを16進数文字列に変換
    private String data2hex(byte[] data) {
        StringBuilder sb=new StringBuilder();
        for (int i=0;i<data.length;i++) {
            String str=Integer.toHexString(
                UnsignedBytes.toInt(data[i]));
            if (str.length()==1) sb.append("0");
            sb.append(str);
        }
        return sb.toString();
    }
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.npaka.nfcex"
    android:versionCode="1"
    android:versionName="1.0">
    <application 
        android:icon="@drawable/icon" 
        android:label="@string/app_name">
        <activity 
            android:name=".NFCEx"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.nfc.action.TAG_DISCOVERED"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.NFC" />
    <uses-sdk android:minSdkVersion="9" />
</manifest> 


−戻る−