【问题标题】:Android app to add mutiple record in NFC tagAndroid 应用程序在 NFC 标签中添加多条记录
【发布时间】:2012-07-11 07:51:49
【问题描述】:

各位,我是一个开发android应用程序的初学者,目前我需要在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();
}


}

【问题讨论】:

  • 你自己尝试了什么?请显示一些源代码。
  • 请接受我最近不能在这里发布我的代码,因为我仍然不清楚逻辑,这就是为什么我想要一些示例代码来帮助自己。我正在使用这个示例jessechen.net/blog/how-to-nfc-on-the-android-platform 我感到困惑的原因是因为我正在尝试添加一些EditView和一个Button,所以我可以自己输入值,但是使用这种设计,我不确定应该如何我修改了意图,因为写入操作应该在一个新函数中,当我单击按钮时将执行该函数
  • 很抱歉昨天晚上没能提供源代码,因为我把它留在了大学里。
  • 07-12 10:01:27.023: E/AndroidRuntime(22376): java.lang.RuntimeException: 无法启动活动 ComponentInfo{android.writer/android.writer.Writer}: java.lang .SecurityException:需要 NFC 权限:用户 10061 和当前进程都没有 android.permission.NFC。
    07-12 10:01:27.023:E/AndroidRuntime(22376):原因:java.lang.SecurityException:NFC 权限必需:用户 10061 和当前进程都没有 android.permission.NFC。
  • 当我隐藏 SetTag 和 WriteTag 方法时,它回复了我这两个错误,似乎我需要 NFC 权限,但是这次我什么时候需要它,因为上次我写阅读器程序的时候,没有出现这样的消息

标签: android nfc ndef


【解决方案1】:

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

【讨论】:

  • 哦,谢谢你,我添加了意图过滤器但忘记添加权限。
  • 如果您认为我的答案就是答案,请点击勾号将其标记。
  • 我现在可以成功打开应用程序并在EditText中输入数据,但是SetTag和Write Tag功能不起作用。我已经在按钮中声明了在点击时调用 setTag 函数
  • 我有必要建立一个新的类和一个新的意图来处理标签的书写部分吗?
【解决方案2】:

查看this(无耻插件)NFC Eclipse 插件,用于编辑 NDEF 消息,包括单个消息中的多个记录。还附带了一个适用于 android 的项目样板文件,我猜这就是你正在寻找的(?)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多