Android 如果我通过应用程序编写NFC标记,如何通过应用程序读取NFC标记
我正在尝试操作闪光灯的功能,可以通过NFC标签打开/关闭闪光灯 在写下标签后,我试图设置是否会在广播接收器上读取特定信息,闪光灯应该是开/关的。然而,接受者从未回应。我不知道为什么 所以,我真正想知道的是以下内容 “我的应用程序可以通过我的应用程序首先读取标记中写入的信息。” 正如您在下面看到的代码,我尝试操作该功能 此代码用于编写NFC标记Android 如果我通过应用程序编写NFC标记,如何通过应用程序读取NFC标记,android,android-intent,nfc,ndef,Android,Android Intent,Nfc,Ndef,我正在尝试操作闪光灯的功能,可以通过NFC标签打开/关闭闪光灯 在写下标签后,我试图设置是否会在广播接收器上读取特定信息,闪光灯应该是开/关的。然而,接受者从未回应。我不知道为什么 所以,我真正想知道的是以下内容 “我的应用程序可以通过我的应用程序首先读取标记中写入的信息。” 正如您在下面看到的代码,我尝试操作该功能 此代码用于编写NFC标记 private void enableTagWriteMode() { mWriteMode = true; //IntentFilte
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代码
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中的筛选器
<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发现意图仅发送给活动。您不能通过广播接收器接收它们。因此,有两个选项可用于读取标记:
<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>
并为其筛选:
<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发现意图仅发送给活动。您不能通过广播接收器接收它们。因此,有两个选项可用于读取标记:
<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>
并为其筛选:
<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>