[英]How to read and write Android NFC tags?
我按照一些教程使用我的 NFCTest 的 Adam Rocker 源代碼。 我希望能夠讀寫 NFC 標簽並啟動應用程序。
首先,您必須在 AndroidManifest.xml 文件中獲得 NFC 的許可。 權限是:
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" />
將執行 NFC 讀/寫操作的活動,在 AndroidManifest.xml 文件中的該活動中添加此意圖過濾器:
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
在您的活動 onCreate() 方法中,您必須初始化 NFC 適配器並定義 Pending Intent :
NfcAdapter mAdapter;
PendingIntent mPendingIntent;
mAdapter = NfcAdapter.getDefaultAdapter(this);
if (mAdapter == null) {
//nfc not support your device.
return;
}
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
在 onResume() 回調中啟用前台調度以檢測 NFC 意圖。
mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
在 onPause() 回調中,您必須禁用前台調度:
if (mAdapter != null) {
mAdapter.disableForegroundDispatch(this);
}
在 onNewIntent() 回調方法中,您將獲得新的 Nfc Intent。 獲得 The Intent 后,您必須解析 Intent 以檢測卡片:
@Override
protected void onNewIntent(Intent intent){
getTagInfo(intent)
}
private void getTagInfo(Intent intent) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
現在你有了標簽。 然后您可以檢查標簽技術列表以檢測該標簽。 標簽檢測技術在我的另一個答案中完整的項目在我的 github 個人資料中
把這些放在你的清單中:
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.INTERNET" />
然后閱讀NFC把這部分放在你的活動中:
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
最后添加像我的 Activity 一樣前進:
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.balysv.materialripple.MaterialRippleLayout;
import com.blogspot.android_er.androidnfctagdiscovered.R;
import com.pixelcan.inkpageindicator.InkPageIndicator;
import hpbyp.ir.app.hojre.fragment.slider.SliderPagerAdapter;
import hpbyp.ir.app.hojre.utiles.G;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
/**
* An {@link Activity} which handles a broadcast of a new tag that the device just discovered.
*/
public class ActivityLoadDataFromNFC extends AppCompatActivity {
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load_data_from_nfc);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mAdapter = NfcAdapter.getDefaultAdapter(this);
if (mAdapter == null) {
//nfc not support your device.
return;
}
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
}
NfcAdapter mAdapter;
PendingIntent mPendingIntent;
@Override
protected void onResume() {
super.onResume();
mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
}
@Override
protected void onPause() {
super.onPause();
if (mAdapter != null) {
mAdapter.disableForegroundDispatch(this);
}
}
@Override
protected void onNewIntent(Intent intent) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
GetDataFromTag(tag, intent);
}
private void GetDataFromTag(Tag tag, Intent intent) {
Ndef ndef = Ndef.get(tag);
try {
ndef.connect();
// txtType.setText(ndef.getType().toString());
// txtSize.setText(String.valueOf(ndef.getMaxSize()));
// txtWrite.setText(ndef.isWritable() ? "True" : "False");
Parcelable[] messages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (messages != null) {
NdefMessage[] ndefMessages = new NdefMessage[messages.length];
for (int i = 0; i < messages.length; i++) {
ndefMessages[i] = (NdefMessage) messages[i];
}
NdefRecord record = ndefMessages[0].getRecords()[0];
byte[] payload = record.getPayload();
String text = new String(payload);
Log.e("tag", "vahid" + text);
ndef.close();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Cannot Read From Tag.", Toast.LENGTH_LONG).show();
}
}
}
您可以在此處找到一個帶有示例的簡單 NFC 庫: https : //github.com/mateuyabar/pillowNFC
我認為您找到的代碼是指 2.3.3 之前的時代。 此時它無法寫入標簽,但在 Android 2.3.3 中這是可能的。 沒有必要試圖破解系統並編寫這樣的標簽。
看看NFC演示項目: http : //developer.android.com/resources/samples/NFCDemo/index.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.