Testing an app by faking NFC tag scan

扶醉桌前 提交于 2019-12-06 09:33:46
Michael Roland

You specify a MIME type filter for the NDEF_DISCOVERED intent filter:

<data android:mimeType="text/plain"/>
<data android:mimeType="image/*" />

Consequently, the fake NFC intent needs to contain one of these MIME types to match the intent filter. You can add the type information to the intent using the setType() method:

public void scan_tag (View view) {
    final Intent intent = new Intent(NfcAdapter.ACTION_NDEF_DISCOVERED);
    intent.setType("text/plain");
    intent.putExtra(NfcAdapter.EXTRA_NDEF_MESSAGES, ...);
    startActivity(intent);
}

Also note that the above code won't add a tag handle to the NFC intent. Hence, you can't obtain a Tag object with

detected_tag = getIntent().getParcelableExtra(nfcAdapter.EXTRA_TAG);

Consequently, you also can't obtain an instance of the Ndef connection class using

Ndef ndef = Ndef.get(detected_tag);

You might want to look into the following questions/answers regarding mock tag objects:

Finally, be aware that there are several other issues in your code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!