NFC读写电子便签总结

Posted spps

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NFC读写电子便签总结相关的知识,希望对你有一定的参考价值。

 

编写NFC程序的基本步骤

1)设置权限,限制android版本、安装的设备:

1
2
3
4
<uses-sdk android:minSdkVersion="14"/>
<uses-permission android:name="android.permission.NFC" />
<!-- 要求当前设备必须要有NFC芯片 -->
<uses-feature android:name="android.hardware.nfc" android:required="true"

 

 

public class NfcManager {

private NfcAdapter mNfcAdapter;
private boolean mEnabled;
private WeakReference<Activity> mActivityWeakReference;
private MessageListener mMessageListener;
private NfcAdapter.ReaderCallback mReaderCallback = new NfcAdapter.ReaderCallback() {
@Override
public void onTagDiscovered(Tag tag) {
processTag(tag);
}
};

private NfcManager(NfcAdapter nfcAdapter) {
mNfcAdapter = nfcAdapter;
}
public static NfcManager get(Context context) {
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(context);
if (nfcAdapter != null) {
return new NfcManager(nfcAdapter);
} else {
return null;
}
}
/**
* 设备是否支持nfc
*/
public static boolean testSupportNfc(Context context) {
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(context);
if (nfcAdapter == null) {
T.showShort(InspectionApplication_.getApplication(), Tools.getStringValue(
InspectionApplication_.getApplication(), R.string.dialog_not_support_NFC));
return false;
}
if (!nfcAdapter.isEnabled()) {
T.showShort(InspectionApplication_.getApplication(), Tools.getStringValue(
InspectionApplication_.getApplication(), R.string.please_open_nfc));
context.startActivity(new Intent("android.settings.NFC_SETTINGS"));
return false;
}
return true;
}
public synchronized boolean enable(Activity activity, MessageListener listener) {
if (mEnabled) {
return true;
}
if (!mNfcAdapter.isEnabled()) {
return false;
}
mNfcAdapter.enableReaderMode(activity, mReaderCallback, NfcAdapter.FLAG_READER_NFC_A, null);
mActivityWeakReference = new WeakReference<>(activity);
mMessageListener = listener;
mEnabled = true;
return true;
}
public synchronized boolean disable(Activity activity) {
if (!mEnabled) {
return true;
}
if (activity != mActivityWeakReference.get()) {
return false;
}
mNfcAdapter.disableReaderMode(activity);
mActivityWeakReference.clear();
mActivityWeakReference = null;
mMessageListener = null;
mEnabled = false;
return true;
}
private void processTag(Tag tag) {
try {
List<String> techs = Arrays.asList(tag.getTechList());
if (!techs.contains("android.nfc.tech.Ndef")) {
Log.e("NDEF not included");
return;
}
String msg = "BEGIN:VCARD\n" + "VERSION:2.1\n" + "中国湖北省武汉市\n"
+ "武汉大学计算机学院\n" + "END:ABCDE";
byte[] textBytes = msg.getBytes();
//将字节数组封装到一个NdefRecord实例中去
NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
NdefRecord.RTD_TEXT, new byte[] {}, textBytes);
//新建NdefRecord数组,本例中数组只有一个元素
NdefRecord[] records2 = {textRecord};
//新建一个NdefMessage实例
NdefMessage message = new NdefMessage(records2);

Ndef ndef2 = Ndef.get(tag);
// 打开连接
ndef2.connect();
// 写入NDEF信息
ndef2.writeNdefMessage(message);
// 关闭连接
ndef2.close();
Ndef ndef = Ndef.get(tag);
NdefMessage ndefMessage = ndef.getCachedNdefMessage();
NdefRecord[] records = ndefMessage.getRecords();
if (records == null || records.length == 0) {
KLog.e("NDEF record not found");
return;
}
NdefRecord record = records[0];
final String textType = "text/plain";
if (!record.toMimeType().equals(textType)) {
Log.e("NDEF record is not " + textType);
return;
}
if (mMessageListener == null) {
return;
}
byte[] payload = record.getPayload();
byte length = payload[0];
int start = 1 + length;
mMessageListener
.onMessage(new String(Arrays.copyOfRange(payload, start, payload.length)));
} catch (Exception e) {
Log.e(e);
}
}
public interface MessageListener {
void onMessage(String message);
}

}
/////////////////////////////////

NFCSActivity 

onResume(){

   closeToNfc()

}

public void closeToNfc() {
if (!NfcManager.testSupportNfc(mContext)) {
return;
}
final NfcManager manager = NfcManager.get(mContext);
if (manager == null) {
return;
}
final DataWrapper<AlertDialog> dataWrapper = new DataWrapper<>();
final NfcManager.MessageListener listener = new NfcManager.MessageListener() {
@Override
public void onMessage(final String message) {
Message msg=new Message();
msg.what=1;
msg.obj=message;
handler.sendMessage(msg);

}
};
manager.enable(mContext, listener);

}
///////////////////////////////
LinearLayout rootlayout = new LinearLayout(mContext);
rootlayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
UnitUtils.dip2px(mContext, 48)));
rootlayout.setOrientation(LinearLayout.HORIZONTAL);
TextView text_Name = new TextView(mContext);
LinearLayout.LayoutParams text_Name_Params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
text_Name.setLayoutParams(text_Name_Params);
text_Name.setId(View.generateViewId());
text_Name.setText(R.string.activity_nfc_id);
rootlayout.addView(text_Name);

EditText et_name = new EditText(mContext);
LinearLayout.LayoutParams et_Name_Params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
et_name.setLayoutParams(et_Name_Params);
et_name.setId(View.generateViewId());
et_name.setMaxLines(1);
if(msg.obj!=null){
et_name.setText(msg.obj.toString());
mNFCs.add(msg.obj.toString());
}
et_name.setSelection(et_name.getText().length());
rootlayout.addView(et_name);
linear.addView(rootlayout);


}

}
};



































































































































































以上是关于NFC读写电子便签总结的主要内容,如果未能解决你的问题,请参考以下文章

Android NFC 标签读写Demo与历史漏洞概述

Android NFC 标签读写Demo与历史漏洞概述

NFC读写器是啥

android NFC 请问怎么读写NfcA标签

nfc的工作原理是啥?

nfc功能读写 demo