2014年11月25日 星期二

Android NFC tag 近距離無線通訊

需有NFC功能手機與感應卡(悠遊卡、學生證之類的)
首先在權限上將minSdkVersion修改為10並加入

1.AndroidManifest.xml



2.layout的activity_main.xml需有一個TextView顯示

3.MainActivity.java
public class Inventory extends Activity {
 
 /* NFC */
 // list of NFC technologies detected:
 private final String[][] techList = new String[][] { new String[] {
   NfcA.class.getName(), NfcB.class.getName(), NfcF.class.getName(),
   NfcV.class.getName(), IsoDep.class.getName(),
   MifareClassic.class.getName(), MifareUltralight.class.getName(),
   Ndef.class.getName() } };

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.inventory);

 }

 /* NFC */
 @Override
 protected void onResume() {
  super.onResume();
  // creating pending intent:
  PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
    new Intent(this, getClass())
      .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
  // creating intent receiver for NFC events:
  IntentFilter filter = new IntentFilter();
  filter.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
  filter.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
  filter.addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
  // enabling foreground dispatch for getting intent from NFC event:
  NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
  nfcAdapter.enableForegroundDispatch(this, pendingIntent,
    new IntentFilter[] { filter }, this.techList);
 }

 @Override
 protected void onPause() {
  super.onPause();
  // disabling foreground dispatch:
  NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
  nfcAdapter.disableForegroundDispatch(this);
 }
 //將tag到的ID顯示出來
 @Override
 protected void onNewIntent(Intent intent) {
  if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
   ((TextView) findViewById(R.id.tag)).setText("Tag ID(16-14):"
     + ByteArrayToHexString(intent
       .getByteArrayExtra(NfcAdapter.EXTRA_ID))
     + "\n Tag ID (16-8):"
     + getHex(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID))
     + "\n Tag ID (dec):"
     + getDec(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID))
     + "\n ID (reversed):"
     + getReversed(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)));
  }
 }
 //16進位14碼
 private String ByteArrayToHexString(byte[] inarray) {
  int i, j, in;
  String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
    "B", "C", "D", "E", "F" };
  String out = "";
  for (j = 0; j < inarray.length; ++j) {
   in = (int) inarray[j] & 0xff;
   i = (in >> 4) & 0x0f;
   out += hex[i];
   i = in & 0x0f;
   out += hex[i];
  }
  return out;
 }
 //16進位8碼
 private String getHex(byte[] bytes) {
  StringBuilder sb = new StringBuilder();
  for (int i = bytes.length - 1; i >= 0; --i) {
   int b = bytes[i] & 0xff;
   if (b < 0x10)
    sb.append('0');
   sb.append(Integer.toHexString(b));
   if (i > 0) {
    sb.append(" ");
   }
  }
  return sb.toString();
 }
 //12進位10碼
 private long getDec(byte[] bytes) {
  long result = 0;
  long factor = 1;
  for (int i = 0; i < bytes.length; ++i) {
   long value = bytes[i] & 0xffl;
   result += value * factor;
   factor *= 256l;
  }
  return result;
 }
 //12進位10碼反轉
 private long getReversed(byte[] bytes) {
  long result = 0;
  long factor = 1;
  for (int i = bytes.length - 1; i >= 0; --i) {
   long value = bytes[i] & 0xffl;
   result += value * factor;
   factor *= 256l;
  }
  return result;
 }
} 
參考連結
https://gist.github.com/luixal/5768921
https://github.com/nadam/nfc-reader
 

沒有留言 :

張貼留言