Android应用程序在NFC标签中添加多条记录

标签 android nfc ndef

各位,我是开发android app的初学者,目前我需要将一些数据写入NFC标签以检查我自己的NFC阅读器程序,但是,我可以尝试的那些应用程序只能将一条记录写入标签,这不符合我要求里面有几条记录,最后一条AAR记录,所以我想问一下有没有人知道有什么应用程序可以提供这个功能或者以前有人写过这样的程序?谢谢!

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();
}


}

最佳答案

您需要在 list 文件中为您的应用程序提供 NFC 支持。

关于Android应用程序在NFC标签中添加多条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11427997/

相关文章:

android - 我可以使用同一个 keystore 文件来签署两个不同的应用程序吗?

android - 上下文菜单安卓

android - 将我的 NFC 标签绑定(bind)到我的应用程序

android - 在 Android 中访问安全元素

android - NFC交接WIFI+加密

android - 如何检查用户是否真正启动了 android 应用程序,而不仅仅是返回到 Activity ?

android - 用 Android 写 "NfcA only"标签

java - 检测 Activity 是否正常启动或在读取 NFC 标签后(无任何提示)

适用于多种 NFC 类型的 Android Intent 过滤器

android - 将图标添加到 Android 快速设置