Question

Hello every one i am working on android application where i need to scan the nfc tag of the device. i completely new to nfc and after reading lots of tutorials i find some ways to check nfc is enable or not in phone but i am not getting how to read nfc tag.

here is my oncreat

protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainmenu);
        mNfcAdapter = NfcAdapter.getDefaultAdapter(MainActivity.this);
        pref = getApplicationContext().getSharedPreferences("Cordinate", 0);
        editor = pref.edit();
        nfc1 = (ImageView) findViewById(R.id.checknfc1);
        mDisplay = (TextView) findViewById(R.id.display);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password1);
        login = (TextView) findViewById(R.id.btnRegister);
        context = getApplicationContext();

        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        imei = telephonyManager.getDeviceId();


        gcm = GoogleCloudMessaging.getInstance(this);

        login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                // Toast.makeText(MainActivity.this, "i  am workin",
                // Toast.LENGTH_LONG).show();

                GPSService gpstest =  new GPSService(imei,MainActivity.this);
                Thread data2 = new Thread(gpstest);
                data2.start();
                gps = new GPSTracker(MainActivity.this);
                if(gps.canGetLocation()){

                    double latitude = gps.getLatitude();
                    double longitude = gps.getLongitude();

                    // \n is for new line
                    //Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();   
                }
                else
                {

                    Toast.makeText(getApplicationContext(), "GPS is not working", Toast.LENGTH_LONG).show(); 
                }
                username1 = username.getText().toString();
                password2 = password.getText().toString();
                username.setText("");
                password.setText("");
                new RegisterBackground().execute();

            }
        });


        nfc1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub


                if (mNfcAdapter != null) {
                   // "Read an NFC tag"
                    Toast.makeText(MainActivity.this, "Read an NFC tag", Toast.LENGTH_LONG).show();
                     // create an intent with tag data and deliver to this activity
                    mPendingIntent = PendingIntent.getActivity(MainActivity.this, 0,
                        new Intent(MainActivity.this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

                    // set an intent filter for all MIME data
                    IntentFilter ndefIntent = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);

                    onNewIntent(getIntent());
                    try {
                        ndefIntent.addDataType("*/*");
                        mIntentFilters = new IntentFilter[] { ndefIntent };
                    } catch (Exception e) {
                        Log.e("TagDispatch", e.toString());
                    }
                } else {
                    Toast.makeText(MainActivity.this, "not able to read NFC tag", Toast.LENGTH_LONG).show();
                }

                //Toast.makeText(MainActivity.this, datanfc, Toast.LENGTH_LONG).show();


            }
        });

    }

my onNewIntent

public void onNewIntent(Intent intent) {
    String action = intent.getAction();
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

   String nfctag =tag.toString();

   Toast.makeText(MainActivity.this, nfctag, Toast.LENGTH_LONG).show();

}
Was it helpful?

Solution

Refer to this docs for more information http://developer.android.com/guide/topics/connectivity/nfc/nfc.html

Basically, first you should what types to tag you want to read in ACTION_TECH_DISCOVERED

Then update manifest accordingly,

<activity>
...
<intent-filter>
    <action android:name="android.nfc.action.TECH_DISCOVERED"/>
</intent-filter>

<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
    android:resource="@xml/nfc_tech_filter" />
...
</activity>

Then get the data from the scanned tag

public void onResume() {
    super.onResume();
    ...
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        if (rawMsgs != null) {
            msgs = new NdefMessage[rawMsgs.length];
            for (int i = 0; i < rawMsgs.length; i++) {
                msgs[i] = (NdefMessage) rawMsgs[i];
            }
        }
    }
    //process the msgs array
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top