如何阅读和编写Android NFC标签?
我认为你找到的代码是指2.3.3时代之前的代码。在这一点上,它不能写一个标签,但对于Android 2.3.3,这是可能的。没有必要试图破解系统并写入这样的标签。
看一看的NFC演示项目:http://developer.android.com/resources/samples/NFCDemo/index.html
的NDEF Tools for Android公用事业项目有助于执行以下操作
该项目还包含所有标准化NDEF记录类型的数据绑定,与使用Android SDK中包含的(基于字节数组的)NDEF类相比,它真正简化了一些事情。
另请参见NFC Eclipse plugin图形NDEF编辑器 - 附带实用程序app,它可以读取和写入标签和光束,还具有NFC读卡器集成功能。
顺便说一句,你正在寻找Android应用程序记录启动应用程序。推出的'功能'不需要任何实际的实施;它内置于Android> = 4.0中,因此将该记录放置在标签上就足够了。
编辑:更新链接
首先,你必须获得针对NFC AndroidManifest.xml文件的权限。权限是:
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
在您的活动onCreate()方法你:
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" />
将执行NFC读/写操作的行为,该活动在AndroidManifest.xml文件中添加此意图过滤器必须初始化NFC适配器和定义待定意图:
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意向。得到意向后,你必须解析检测卡的意图:
@Override
protected void onNewIntent(Intent intent){
getTagInfo(intent)
}
private void getTagInfo(Intent intent) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
现在你有标签。然后,您可以检查标签技术列表以检测该标签。标签检测技术是在这里My Another Answer 全面完整的项目是在这里My github profile
卡里姆先生,我想创建自己的数字名片,请让我知道我需要购买哪张nfc贴纸? – Sun 2015-03-10 11:40:29
您可以使用Mifare Classic 1k。它有巨大的内存(1024字节),每个块可以一次写入16个字节。您也可以锁定数据以防止进一步写入。 – 2015-03-10 15:35:59
首先是把这些在你的清单:
<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)2011 AdamNybä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();
}
}
}
检查:https://stackoverflow.com/a/45773087/5733853 – HPbyP 2017-08-19 15:09:39