我使用adam摇杆源代码为我的NFCTest做了一些教程。我希望能够读取和写入NFC标签,并启动应用程序。
答案 0 :(得分:15)
NDEF Tools for Android实用程序项目有助于执行以下操作
答案 1 :(得分:11)
首先,您必须获得NFC的AndroidManifest.xml文件的权限。权限是:
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" />
将执行NFC读/写操作的Activity,在AndroidManifest.xml文件中的该活动中添加此intent过滤器:
<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()中回调启用Foreground Dispatch以检测NFC意图。
mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
在onPause()回调中,您必须禁用forground dispatch:
if (mAdapter != null) {
mAdapter.disableForegroundDispatch(this);
}
在onNewIntent()回调方法中,您将获得新的Nfc Intent。获得The Intent后,您必须解析检测卡的意图:
@Override
protected void onNewIntent(Intent intent){
getTagInfo(intent)
}
private void getTagInfo(Intent intent) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
现在你有了标签。然后,您可以检查Tag Tech列表以检测该Tag。标签检测技术在My Another Answer 完整的完整项目位于My github profile
答案 2 :(得分:2)
首先将这些放在你的清单中:
<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>
最后像我的活动一样添加前进:
/ * *版权所有(C)2010 Android开源项目 *版权所有(C)2011AdamNybäck * *根据Apache许可证2.0版(“许可证”)获得许可; *除非符合许可,否则您不得使用此文件。 *您可以在以下位置获取许可证副本 * * http://www.apache.org/licenses/LICENSE-2.0 * *除非适用法律要求或书面同意,否则软件 *根据许可证分发的“按现状”分发, *不附带任何明示或暗示的保证或条件。 *有关管理权限的特定语言,请参阅许可证 *许可证下的限制。 * /
package ***.***.***.***;
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();
}
}
}
答案 3 :(得分:1)
我认为您找到的代码是指2.3.3之前的时代。此时它无法编写标签,但使用Android 2.3.3这是可能的。没有必要试图破解系统并编写这样的标签。
查看NFC演示项目:http://developer.android.com/resources/samples/NFCDemo/index.html
答案 4 :(得分:1)
您可以在此找到一个简单的NFC库,其中包含一个示例: https://github.com/mateuyabar/pillowNFC