如何使NFC标签写保护?

时间:2017-02-12 20:49:51

标签: java android nfc

我正在构建一个从NFC标签写入和读取的应用程序,但我需要在写入之后不修改数据。 我尝试过MakeReadOnly(),但我不知道它为什么不起作用!

     protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if(intent.hasExtra(NfcAdapter.EXTRA_TAG))
    {
            Toast.makeText(this,"NFC intent!",Toast.LENGTH_SHORT).show();// After detecting a tag on my system, I can get an tag object to write or read from it
            Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            NdefMessage ndefMessage = creatNdefMessage(StudentId.getText()+"");
            writeNdefMessage(tag,ndefMessage);
        // it writes on the tag but cannot makeit read only !!!

       try{ Ndef ndef = Ndef.get(tag);
        if (ndef != null) {
            ndef.connect();
            if (ndef.canMakeReadOnly()) {
              Toast.makeText(this,"can make it read only !",Toast.LENGTH_SHORT).show();
               ndef.makeReadOnly();
              }
             ndef.close();
          }
      } catch (IOException e) {
         e.printStackTrace();
     }

     }
       }

在这段代码中,我可以检查它是否访问了if语句(ndef.canMakeReadOnly()),这意味着只有当我调用MakeReadOnly方法时标签才能为红色,但是在运行它之后我仍然可以覆盖标签!

请帮助..

0 个答案:

没有答案