I followed some tutorial using the adam rocker source code for my NFCTest. I want to be able to read and write NFC tags and also launch an application.
Put these in your manifest :
Then to read NFC put this part in your activity:
at the end add go forward like my Activity :
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.balysv.materialripple.MaterialRippleLayout;
import com.blogspot.android_er.androidnfctagdiscovered.R;
import com.pixelcan.inkpageindicator.InkPageIndicator;
import hpbyp.ir.app.hojre.fragment.slider.SliderPagerAdapter;
import hpbyp.ir.app.hojre.utiles.G;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
/**
* An {@link Activity} which handles a broadcast of a new tag that the device just discovered.
*/
public class ActivityLoadDataFromNFC extends AppCompatActivity {
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load_data_from_nfc);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mAdapter = NfcAdapter.getDefaultAdapter(this);
if (mAdapter == null) {
//nfc not support your device.
return;
}
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
}
NfcAdapter mAdapter;
PendingIntent mPendingIntent;
@Override
protected void onResume() {
super.onResume();
mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
}
@Override
protected void onPause() {
super.onPause();
if (mAdapter != null) {
mAdapter.disableForegroundDispatch(this);
}
}
@Override
protected void onNewIntent(Intent intent) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
GetDataFromTag(tag, intent);
}
private void GetDataFromTag(Tag tag, Intent intent) {
Ndef ndef = Ndef.get(tag);
try {
ndef.connect();
// txtType.setText(ndef.getType().toString());
// txtSize.setText(String.valueOf(ndef.getMaxSize()));
// txtWrite.setText(ndef.isWritable() ? "True" : "False");
Parcelable[] messages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (messages != null) {
NdefMessage[] ndefMessages = new NdefMessage[messages.length];
for (int i = 0; i < messages.length; i++) {
ndefMessages[i] = (NdefMessage) messages[i];
}
NdefRecord record = ndefMessages[0].getRecords()[0];
byte[] payload = record.getPayload();
String text = new String(payload);
Log.e("tag", "vahid" + text);
ndef.close();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Cannot Read From Tag.", Toast.LENGTH_LONG).show();
}
}
}