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();
}
}
|