Android app to add mutiple record in NFC tag

2019-03-01 11:22发布

问题:

everyone, I am a beginner of developing android app, currently I need to write some data into a NFC tag to check my own NFC reader program, however, those app I can try can only write one record into the tag, which do not match with my requirement to have several record inside and an AAR record at the end, so I would like to ask if anyone know any app which can provide this functionality or anyone has written such a program before?Thank you!

public class Writer extends Activity {

NfcAdapter mAdapter;
PendingIntent mPendingIntent;
IntentFilter mWriteTagFilters[];
boolean mWriteMode;
Tag detectedTag;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_writer);
    mAdapter = NfcAdapter.getDefaultAdapter(this);
    mPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    mWriteTagFilters = new IntentFilter[] { tagDetected };

    //enableTagWriteMode();
    //Intent intent = getIntent();

}

private void enableTagWriteMode(){
    mWriteMode = true;
    mAdapter.enableForegroundDispatch(this, mPendingIntent, mWriteTagFilters, null);
}

private void disableTagWriteMode(){
    mWriteMode = false;
    mAdapter.disableForegroundDispatch(this);
}

public void SetTag(View view){
    EditText editText1 = (EditText) findViewById(R.id.edit_message1);
    EditText editText2 = (EditText) findViewById(R.id.edit_message2);
    String message1 = editText1.getText().toString();
    String message2 = editText2.getText().toString();
    byte[] textBytes1 = message1.getBytes();
    byte[] textBytes2 = message2.getBytes();
    NdefRecord textRecord1 = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, 
            message1.getBytes(), new byte[]{}, textBytes1);
    NdefRecord textRecord2 = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, 
            message2.getBytes(), new byte[]{}, textBytes2);
    NdefMessage mNdefMessage = new NdefMessage(
        new NdefRecord[]{
                textRecord1,
                textRecord2,
                NdefRecord.createApplicationRecord("android.reader")
        }
    );
    writeTag(mNdefMessage, detectedTag);    
}

public static void writeTag(NdefMessage message, Tag tag){
    int size = message.toByteArray().length;
    try {
        Ndef ndef = Ndef.get(tag);
        if (ndef != null){
            ndef.connect();
            if (ndef.isWritable() && ndef.getMaxSize() > size)
                ndef.writeNdefMessage(message);
        }else{
            NdefFormatable format = NdefFormatable.get(tag);
            if (format != null) {
                try {
                    format.connect();
                    format.format(message);
                }catch(IOException e){

                }
            }
        }
    }catch(Exception e){

    }
}

@Override
protected void onNewIntent(Intent intent){
    if(mWriteMode && NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction()))
        detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);        
}

@Override
public void onPause(){
      super.onPause();
    disableTagWriteMode();
}

@Override
public void onResume(){
      super.onResume();
    enableTagWriteMode();
}


}

回答1:

You need to give your application NFC support in the manifest file.



回答2:

Have a look at this(shameless plug) NFC Eclipse plugin for editing NDEF messages, including multiple records in a single message. Comes with a project boilerplate for android too, which I guess is what you're looking for (?).



标签: android nfc ndef