如果通过应用程序编写了NFC标签,如何通过应用程序读取NFC标签 [英] How to read NFC Tag through my Application if I wrote NFC Tag through my Application
问题描述
我正在尝试操作可通过NFC标签打开/关闭的闪光灯功能.
I am trying to operate the function of flash light which can be on/off through NFC tag.
写完标签后,我尝试设置是否可以在broadcastReceiver上读取某些信息,闪光灯应该打开/关闭.但是,接收方从未回应.我不知道为什么.
After writing tag, I tried to set up if a certain information would be read on broadcastReceiver, Flash light should be on/off. However, the receiver never responded. I don't know Why..
所以,我真正想知道的是下面的
So, what I really want to know is following below as
我的应用程序可以通过我的应用程序首先读取标记中写入位置的信息."
"My application can read the information first where is written in the tag through My application. "
如您在下面的代码中看到的,我试图操作该功能.
as you saw below codes, I tried to operate the function.
此代码用于写入NFC标签
This code For write NFC tag
private void enableTagWriteMode() {
mWriteMode = true;
//IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
//this.registerReceiver(Flash.class, tagDetected);
//IntentFilter[] mWriteTagFilters = new IntentFilter[] { tagDetected };
mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent, null, null);
}
private void disableTagWriteMode() {
mWriteMode = false;
mNfcAdapter.disableForegroundDispatch(this);
}
protected void onNewIntent(Intent intent) {
// Tag writing mode
String action = intent.getAction();
System.out.println("aa: " + action);
//android.nfc.action.NDEF_DISCOVERED
if (mWriteMode) {
Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
String flash = "Flash";
byte[] textBytes = flash.getBytes();
NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
"text/plain".getBytes(), new byte[] {}, textBytes);
NdefMessage message= new NdefMessage(new NdefRecord[] { textRecord });
boolean write = writeTag(message, detectedTag);
System.out.println(""+write);
if (write) {
Toast.makeText(this, "Success: Wrote placeid to nfc tag", Toast.LENGTH_LONG)
.show();
}
}
}
此代码,用于读取NFC代码
This code, For Read NFC code
public class Flash extends BroadcastReceiver {
private boolean isLighOn = false;
private Camera camera;
@Override
public void onReceive(Context context, Intent intent) {
if ( NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
System.out.println("call?");
//LayoutInflater mInflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
PackageManager pm = context.getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Log.e("err", "Device has no camera!");
return;
}
camera = Camera.open();
final Parameters p = camera.getParameters();
if (isLighOn) {
Log.i("info", "torch is turn off!");
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLighOn = false;
}
else {
Log.i("info", "torch is turn on!");
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isLighOn = true;
}
}
}
}
这是manifest.xml中的过滤器
This is Filter in manifest.xml
<receiver android:name =".Flash">
<intent-filter android:priority="10000">
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<data android:mimeType="text/plain" />
</intent-filter>
</receiver>
推荐答案
NFC发现意图仅发送到活动.您无法通过BroadcastReceiver接收它们.因此,您有两种选择来读取标签:
The NFC discovery intents are sent to activities only. You cannot receive them through a BroadcastReceiver. Thus, you have two options for reading the tag:
-
如果您只想在活动状态处于前台时读取标签,则可以使用前台分派系统(就像您为编写标签所做的一样).
If you only want to read the tag while your avtivity is in the foreground, you can use the foreground dispatch system (as you already did for writing the tag).
如果您想实现与BroadcastReceiver尝试的类似的功能,则可以为NDEF_DISOVERED意向注册一个活动:
If you want to achieve something similar to what you tried with the BroadcastReceiver, you can register an activity for the NDEF_DISOVERED intent:
<activity ...>
<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>
注意,您不能使用android:priority="10000"
为应用程序赋予优先级.如果为同一数据类型注册了多个活动,则无论优先级值如何,都会显示一个活动选择器.在您的情况下,数据类型text/plain
可能会与其他应用程序发生冲突,因此我建议您改用NFC论坛外部类型.使用NFC论坛外部类型,您可以创建自己的特定于应用程序的NDEF记录类型:
Note that you cannot give priority to your application using android:priority="10000"
. If multiple activities are registered for the same data type, an activity chooser will be shown regardless of priority values. In your case, the data type text/plain
is likely to collide with other applications, so I suggest that you use an NFC Forum external type instead. With the NFC Forum external type, you can create your own application-specific NDEF record types:
NdefRecord extRecord = NdefRecord.createExternal(
"yourdomain.com", // your domain name
"yourtype", // your type name
textBytes); // payload
并对它们进行过滤:
<activity ...>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="vnd.android.nfc" android:host="ext"
android:pathPrefix="/yourdomain.com:yourtype" />
</intent-filter>
</activity>
这篇关于如果通过应用程序编写了NFC标签,如何通过应用程序读取NFC标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!