Reputation: 187
I want to write data to a mifare classic 1K tags. does anyone have a working sample code to do that? I can't find enough information on that on the web. Thanks!
Upvotes: 3
Views: 10812
Reputation: 328
If you have an intent of a NFC discovering, you can use this snippet:
private void WriteCard(Intent intent) {
String action = intent.getAction();
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
MifareClassic mfc = MifareClassic.get(tagFromIntent);
try {
mfc.connect();
boolean authA = mfc.authenticateSectorWithKeyA(1,
MifareClassic.KEY_DEFAULT);
Log.d("MainActivity.WriteCard()", String.valueOf(authA) + " ");
mfc.writeBlock(mfc.sectorToBlock(1), new byte[] { 'A', 'l','v', 'a', 'r', 'e', 'z', ' ', ' ', ' ', ' ', ' ', ' ',' ', ' ', ' ' });
mfc.close();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
return;
}
At this example I'm writting at Sector 1 Block 0. Be sure your trying to write in an "valid" sector with the appropiate key.
Upvotes: 3
Reputation: 10228
There is example code availble at http://nearfieldcommunication.com/developers/android/
Upvotes: 2