如何模拟从其他应用程序标签触摸 [英] How to simulate the tag touch from other application
问题描述
我要模拟的触摸事件我的应用程序。我的清单是像
I want to simulate the touch event for my application. My manifest is like
<activity
android:name=".activity.TagActivity_"
android:label="@string/app_name"
android:launchMode="singleTask"
android:noHistory="true"
android:permission="android.permission.NFC"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="ext"
android:pathPrefix="/abc:d"
android:scheme="vnd.android.nfc" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
问题(1):我想从调用其它应用我的应用程序。我怎么做?我现在的code是这样的:
Question (1): I want to invoke my application from other application. How do I do that? My current code is like:
try {
final Intent intent = new Intent(NfcAdapter.ACTION_NDEF_DISCOVERED);
NdefMessage ndefMessage = buildNdefMessage(getTagData());
intent.putExtra(NfcAdapter.EXTRA_NDEF_MESSAGES, new NdefMessage[] {ndefMessage});
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
但这不是调用我的应用程序。大概是因为数据类型和路径preFIX不匹配。我如何通过这一点的同时开始活动?
But this is not invoking my application. probably because the data type and path prefix do not match. How do I pass this while starting the activity?
问题(2):对于温度的目的我已经加入
Question (2): For temp purpose I have added
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
,使其工作,我的应用程序被调用。但是,当读取标签我检查标签类型,因为我不传递任何变量类型,我的应用程序崩溃。那么,如何创建一个标签实例?有没有构造这一点。我的意思是我不能做 NDEF NDEF =新NDEF();
。我基本上不具有标签,所以我不能做大事 NDEF NDEF = Ndef.get(标签);
to make it work, and my application is invoked. But while reading the tag I check the tag type, and since I am not passing any tag type, my app crashes. So how do I create a tag instance? There is no constructor for this. I mean I can't do Ndef ndef = new Ndef();
. And I don't basically have a tag so I can't event do Ndef ndef = Ndef.get(tag);
.
推荐答案
关于问题(1):如何调用 NDEF_DISCOVERED
意图过滤器
Regarding question (1): How to invoke NDEF_DISCOVERED
intent filter
您活动.activity.TagActivity_过滤器有两种不同的 NDEF_DISCOVERED
。如果意图包含以下形式的URI的第一个匹配vnd.android.nfc:// EXT / ABC:D。如果意图包含的数据类型的第二个匹配text / plain的。
Your activity ".activity.TagActivity_" filters has two different NDEF_DISCOVERED
. The first one matches if the intent contains a URI of the form "vnd.android.nfc://ext/abc:d". The second one matches if the intent contains a data type of "text/plain".
因此,你需要或者添加匹配URI或匹配MIME类型,你用它来启动该活动的意图。
Consequently, you need to either add a matching URI or a matching MIME type to the intent that you use to start that activity.
-
MIME类型文本/纯:
MIME type "text/plain":
final Intent intent = new Intent(NfcAdapter.ACTION_NDEF_DISCOVERED);
intent.setType("text/plain");
startActivity(intent);
URIvnd.android.nfc:// EXT / ABC:D:
URI "vnd.android.nfc://ext/abc:d":
final Intent intent = new Intent(NfcAdapter.ACTION_NDEF_DISCOVERED);
intent.setData(Uri.parse("vnd.android.nfc://ext/abc:d"));
startActivity(intent);
任何两个将开始您的活动。
Any of the two will start your activity.
注意您指定安卓权限=android.permission.NFC
为您接收的活动。你通常不会做。指定此权限意味着你发送应用程序需要有这个权限。
NOTE that you specified android:permission="android.permission.NFC"
for your receiving activity. You normally would not do that. Specifying this permission means that your sending application needs to have that permission.
关于问题(2):如何通过一个模拟标记对象
这是可能使用反射(注意,这是不是大众的Android SDK的一部分,所以它可能无法为未来的Android版本),以便创建一个模拟标记对象实例。
It's possible to create a mock tag object instance using reflection (note that this is not part of the public Android SDK, so it might fail for future Android versions).
-
获得
createMockTag()
方法虽然反映:
Class tagClass = Tag.class;
Method createMockTagMethod = tagClass.getMethod("createMockTag", byte[].class, int[].class, Bundle[].class);
为$ P $一些常量pparing有效的模拟NDEF标记实例:
Define some constants for preparing a valid mock NDEF tag instance:
final int TECH_NFC_A = 1;
final String EXTRA_NFC_A_SAK = "sak"; // short (SAK byte value)
final String EXTRA_NFC_A_ATQA = "atqa"; // byte[2] (ATQA value)
final int TECH_NDEF = 6;
final String EXTRA_NDEF_MSG = "ndefmsg"; // NdefMessage (Parcelable)
final String EXTRA_NDEF_MAXLENGTH = "ndefmaxlength"; // int (result for getMaxSize())
final String EXTRA_NDEF_CARDSTATE = "ndefcardstate"; // int (1: read-only, 2: read/write, 3: unknown)
final String EXTRA_NDEF_TYPE = "ndeftype"; // int (1: T1T, 2: T2T, 3: T3T, 4: T4T, 101: MF Classic, 102: ICODE)
创建NDEF消息:
Create an NDEF message:
NdefMessage ndefMessage = new NdefMessage(NdefRecord.createMime("text/plain"), "Text".getBytes("US-ASCII"));
创建与NDEF消息2类标签的技术含量的额外软件包:
Create the tech-extras bundle for a Type 2 tag with that NDEF message:
Bundle nfcaBundle = new Bundle();
nfcaBundle.putByteArray(EXTRA_NFC_A_ATQA, new byte[]{ (byte)0x44, (byte)0x00 }); //ATQA for Type 2 tag
nfcaBundle.putShort(EXTRA_NFC_A_SAK , (short)0x00); //SAK for Type 2 tag
Bundle ndefBundle = new Bundle();
ndefBundle.putInt(EXTRA_NDEF_MAXLENGTH, 48); // maximum message length: 48 bytes
ndefBundle.putInt(EXTRA_NDEF_CARDSTATE, 1); // read-only
ndefBundle.putInt(EXTRA_NDEF_TYPE, 2); // Type 2 tag
ndefBundle.putParcelable(EXTRA_NDEF_MSG, ndefMessage); // add an NDEF message
prepare您标签的防碰撞识别/ UID(见 Tag.getId()
方法)。例如。 7字节UID为2类标签:
Prepare an anti-collision identifier/UID for your tag (see Tag.getId()
method). E.g. a 7-byte-UID for a Type 2 tag:
byte[] tagId = new byte[] { (byte)0x3F, (byte)0x12, (byte)0x34, (byte)0x56, (byte)0x78, (byte)0x90, (byte)0xAB };
然后就可以通过调用 createMockTag()
方法创建一个模拟标记实例
Then you can create a mock tag instance by invoking the createMockTag()
method
Tag mockTag = (Tag)createMockTagMethod.invoke(null,
tagId, // tag UID/anti-collision identifier (see Tag.getId() method)
new int[] { TECH_NFC_A, TECH_NDEF }, // tech-list
new Bundle[] { nfcaBundle, ndefBundle }); // array of tech-extra bundles, each entry maps to an entry in the tech-list
一旦你创建了一个模拟标记对象,你可以把它作为 NDEF_DISCOVERED
意图的一部分:
Once you created that mock tag object, you can send it as part of the NDEF_DISCOVERED
intent:
Intent ndefIntent = new Intent(NfcAdapter.ACTION_NDEF_DISCOVERED);
ndefIntent.setType("text/plain");
ndefIntent.putExtra(NfcAdapter.EXTRA_ID, tagId);
ndefIntent.putExtra(NfcAdapter.EXTRA_TAG, mockTag);
ndefIntent.putExtra(NfcAdapter.EXTRA_NDEF_MESSAGES, new NdefMessage[]{ ndefMessage });
此外,您可以明确地定义你的目标活动为接收组件:
Optionally, you can explicitly define your target activity as the receiving component:
ndefIntent.setComponent(...); // or equivalent
您可以发送这个意图您的活动:
You can then send this intent to your activity:
startActivity(ndefIntent);
然后接收器可以使用标签MOCH对象检索技术类的实例(例如 Ndef.get(标签)
),但需要IO操作将<任何方法STRONG>失败
The receiver can then use the moch tag object to retrieve instances of the technology classes (e.g. Ndef.get(tag)
) but any method that requires IO operations will fail.
这篇关于如何模拟从其他应用程序标签触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!