android如何讀取id門禁卡?
[普通]查看普通副本打印?
& ltuses-permission Android:name = " Android . permission . NFC "/& gt;
& lt用途-功能
Android:name = " Android . hardware . NFC "
Android:required = " true "/& gt;
添加:
[普通]查看普通副本打印?
& lt意向過濾器& gt
& ltaction Android:name = " Android . NFC . action . tag _ DISCOVERED "/& gt;
& lt類別Android:name = " Android . intent . category . default "/& gt;
& lt/intent-filter & gt;
2.初始化代碼:
[java]查看普通副本打印?
Intent nfcIntent = new Intent(this,getClass());
nfcIntent.addFlags(Intent。FLAG _ ACTIVITY _ SINGLE _ TOP);
mPendingIntent =
PendingIntent.getActivity(this,0,nfcIntent,0);
ma dapter = NFC adapter . getdefaultadapter(this);
if (mAdapter == null) {
toast . make text(getApplicationContext(),"此設備支持NFC功能。",吐司。LENGTH_SHORT)。show();
返回;
}
需要覆蓋的功能:
[java]查看普通副本打印?
private NfcAdapter mAdapter
私人PendingIntent mPendingIntent
@TargetApi(Build。版本代碼。姜餅_MR1)
受保護的void onResume() {
super . on resume();
madapter . enableforegrounddispatch(this,mPendingIntent,null,null);
}
@覆蓋
受保護的void on win content(Intent Intent){
getTagInfo(意向);
}
@TargetApi(Build。版本代碼。姜餅_MR1)
私有void getTagInfo(意圖intent) {
tag tag = intent . getparcelableextra(NFC adapter。EXTRA _ TAG);
byte[]tagId = tag . getid();
string str = ByteArrayToHexString(tagId);
str = flipHexStr(str);
Long cardNo = Long.parseLong(str,16);
string ignore operation id = m _ operation id;
if(m_isOnline){
//如果選擇全部,則傳入票證中編碼的操作id
//操作應該不多,取唯壹的壹個
新的CardValidationAsyncTask()。execute(卡號tostring());
}
}
@TargetApi(Build。版本代碼。姜餅_MR1)
@覆蓋
受保護的void onPause() {
super . on pause();
如果(mAdapter!= null) {
madapter . disableforegrounddispatch(this);
}
}
下面是幫助器函數:
[java]查看普通副本打印?
私有字符串flipHexStr(String s){
StringBuilder result = new StringBuilder();
for(int I = 0;我& lt= s . length()-2;i=i+2) {
result . append(new StringBuilder(s . substring(I,i+2))。反向());
}
返回result.reverse()。toString();
}
私有字符串ByteArrayToHexString(byte[]in array){
int i,j,in;
String[] hex = { "0 "," 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "," 9 "," A ",
“B”、“C”、“D”、“E”、“F”};
String out =
for(j = 0;j & ltinarray.length++j) {
in =(int)inarray[j]& amp;0xff
I =(in & gt;& gt4)& amp;0x0f
out+= hex[I];
i = in & amp0x0f
out+= hex[I];
}
退了出來;
}