I would like to write data on the tag (ISO 15963) after the button is clicked. I can write data on the tag when activity is open. I just don't know how to do the application start to cooperate with tag after click button. Is there any way to do this without calling a new activity? Thank you a lot!
CODE:
public class WrBlock extends Activity {
private Button wbutton;
private NfcAdapter myNfcAdapter;
private IntentFilter[] mFilters;
private String[][] mTechLists;
private PendingIntent mPendingIntent;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.blockw);
wbutton = (Button) findViewById(R.id.button2);
myNfcAdapter = NfcAdapter.getDefaultAdapter(this);
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter intnfcv = new IntentFilter(
NfcAdapter.ACTION_TECH_DISCOVERED);
mFilters = new IntentFilter[] { intnfcv, };
mTechLists = new String[][] { new String[] { NfcV.class.getName() },
new String[] { NdefFormatable.class.getName() } };
wbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// HERE I WANT TO WRITE DATA
}
});
}
public void onNewIntent(Intent intent) {
// onResume gets called after this to handle the intent
setIntent(intent);
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(getIntent()
.getAction())) {
Tag tag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
showMessage(null, "Technologies available in this tag="
+ Arrays.toString(tag.getTechList()));
Tag detectedTag = getIntent().getParcelableExtra(
NfcAdapter.EXTRA_TAG);
NfcV nfcv = NfcV.get(detectedTag);
try {
nfcv.connect();
if (nfcv.isConnected()) {
showMessage(null, "Connected to the tag");
showMessage(null,
"\nTag DSF: " + Byte.toString(nfcv.getDsfId()));
byte[] buffer;
buffer = nfcv.transceive(new byte[] {0x00, 0x21, (byte) 0,0x31, 0x31, 0x31, 0x31});
showMessage(null, "\nByte block 10:" + buffer);
nfcv.close();
wrmode = false;
showMessage(null, "Successfully writing");
} else
showMessage(null, "Not connected to the tag");
} catch (IOException e) {
showMessage(null, "Error");
wrmode = false;
}
} else {
showMessage(null, "ELSE ");
wrmode = false;
}
}
@Override
public void onPause() {
super.onPause();
myNfcAdapter.disableForegroundDispatch(this);
}
@Override
public void onResume() {
super.onResume();
if (myNfcAdapter != null) {
myNfcAdapter.enableForegroundDispatch(this, mPendingIntent,
mFilters, mTechLists);
}
}
public void showMessage(View v, CharSequence x) {
Toast.makeText(getApplicationContext(), x, Toast.LENGTH_SHORT).show();
}
}