Skip to content

Instantly share code, notes, and snippets.

@ruan65
Forked from luixal/ReadNfcUIDActivity.java
Created June 8, 2017 13:41
Show Gist options
  • Save ruan65/54ce8799d663ea87bafef0a007859f09 to your computer and use it in GitHub Desktop.
Save ruan65/54ce8799d663ea87bafef0a007859f09 to your computer and use it in GitHub Desktop.

Revisions

  1. @luixal luixal revised this gist Jun 12, 2013. 2 changed files with 99 additions and 0 deletions.
    File renamed without changes.
    99 changes: 99 additions & 0 deletions ReadNfcUIDOnlyInsideActivity.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,99 @@
    package es.luixal.ragbag;

    import android.app.Activity;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.nfc.NfcAdapter;
    import android.nfc.tech.IsoDep;
    import android.nfc.tech.MifareClassic;
    import android.nfc.tech.MifareUltralight;
    import android.nfc.tech.Ndef;
    import android.nfc.tech.NfcA;
    import android.nfc.tech.NfcB;
    import android.nfc.tech.NfcF;
    import android.nfc.tech.NfcV;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.widget.TextView;

    public class MainActivity extends Activity {

    // 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
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_activity, menu);
    return true;
    }

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

    @Override
    protected void onNewIntent(Intent intent) {
    if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
    ((TextView)findViewById(R.id.text)).setText(
    "NFC Tag\n" +
    ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)));
    }
    }

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

    }
  2. @luixal luixal revised this gist Jun 12, 2013. 2 changed files with 14 additions and 9 deletions.
    9 changes: 0 additions & 9 deletions AndroidManifest.xml
    Original file line number Diff line number Diff line change
    @@ -1,9 +0,0 @@
    <!-- Permission for using NFC hardware -->
    <uses-permission android:name="android.permission.NFC"/>
    <!-- Forcing device to have NFC hardware -->
    <uses-feature android:name="android.hardware.nfc" android:required="true"/>
    <!-- Registering app for receiving NFC's TAG_DISCOVERED intent -->
    <intent-filter>
    <action android:name="android.nfc.action.TAG_DISCOVERED"/>
    <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
    14 changes: 14 additions & 0 deletions ReadNfcUIDActivity
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,17 @@
    // Needed in AndroidManifest:
    <!-- Permission for using NFC hardware -->
    <uses-permission android:name="android.permission.NFC"/>
    <!-- Forcing device to have NFC hardware -->
    <uses-feature android:name="android.hardware.nfc" android:required="true"/>
    <!-- Registering app for receiving NFC's TAG_DISCOVERED intent -->
    <intent-filter>
    <action android:name="android.nfc.action.TAG_DISCOVERED"/>
    <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>




    // handling ACTION_TAG_DISCOVERED action from intent:
    @Override
    protected void onResume() {
  3. @luixal luixal renamed this gist Jun 12, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @luixal luixal created this gist Jun 12, 2013.
    9 changes: 9 additions & 0 deletions AndroidManifest.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    <!-- Permission for using NFC hardware -->
    <uses-permission android:name="android.permission.NFC"/>
    <!-- Forcing device to have NFC hardware -->
    <uses-feature android:name="android.hardware.nfc" android:required="true"/>
    <!-- Registering app for receiving NFC's TAG_DISCOVERED intent -->
    <intent-filter>
    <action android:name="android.nfc.action.TAG_DISCOVERED"/>
    <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
    27 changes: 27 additions & 0 deletions MainActivity
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    // handling ACTION_TAG_DISCOVERED action from intent:
    @Override
    protected void onResume() {
    super.onResume();
    if (getIntent().getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
    ((TextView)findViewById(R.id.text)).setText(
    "NFC Tag\n" +
    this.ByteArrayToHexString(getIntent().getByteArrayExtra(NfcAdapter.EXTRA_ID)));
    }
    }

    // Converting byte[] to hex string:
    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;
    }