?1、在dev/nxpnfc節(jié)點(diǎn)添加對(duì)應(yīng)的文件權(quán)限
on property:sys.boot_completed=1
# nfc add by zm
chmod 777 /dev/nxpnfc
2、在vendor/nxp/nfcdevice-nfc.mk文件中 修改NFC添加到編譯路徑如下所示,跟平時(shí)內(nèi)置apk方式有點(diǎn)類似
PRODUCT_PACKAGES += NFCTestApp
-include \vendor\nxp\nfc\FactoryTestApp\Android.mk
3、然后執(zhí)行make命令 這時(shí)候就會(huì)在out目錄下面system/bin目錄生成?NFCTestApp可執(zhí)行文件
4、產(chǎn)測(cè)中執(zhí)行shell命令 代碼如下
val nfcShell = Shell.exe("/system/bin/NFCTestApp")
if(nfcShell.contains("xxx"){
requireActivity().runOnUiThread(Runnable {
textView!!.text = "識(shí)別NFC成功"
})
}
5、其他原生識(shí)別NFC的方式
6、Android原生識(shí)別NFC
package com.example.nfctest;
import androidx.appcompat.app.AppCompatActivity;
import android.nfc.tech.Ndef;
import android.os.Bundle;
import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private NfcAdapter nfcAdapter;
private PendingIntent pendingIntent;
private TextView tvUid;
String datas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvUid = (TextView) findViewById(R.id.tv_uid);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
/*pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);*/
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
mPendingPollIntent = PendingIntent.getBroadcast(mContext, POLL_REQUEST, pollIntent, PendingIntent.FLAG_IMMUTABLE);
}else{
mPendingPollIntent = PendingIntent.getBroadcast(mContext, POLL_REQUEST, pollIntent, 0);
}
if (nfcAdapter == null) {
Toast.makeText(MainActivity.this,"設(shè)備不支持NFC",Toast.LENGTH_LONG).show();
return;
}
if (nfcAdapter != null && !nfcAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "請(qǐng)?jiān)谙到y(tǒng)設(shè)置中先啟用NFC功能", Toast.LENGTH_LONG).show();
return;
}
//onNewIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
resolveIntent(intent);
// resolveIntent(intent);
}
// void resolveIntent(Intent intent) {
// Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
// if (tag != null) {
// processTag(intent);
// }
// }
public void processTag(Intent intent) {//處理tag
String uid = "";
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] aa = tagFromIntent.getId();
uid += bytesToHexString(aa);//獲取卡的UID
Log.e("processTag","uid="+uid);
tvUid.setText(uid);
}
// //字符序列轉(zhuǎn)換為16進(jìn)制字符串
// private String bytesToHexString(byte[] src) {
// StringBuilder stringBuilder = new StringBuilder("0x");
// if (src == null || src.length <= 0) {
// return null;
// }
// char[] buffer = new char[2];
// for (int i = 0; i < src.length; i++) {
// buffer[0] = Character.forDigit((src[i] >>> 4) & 0x0F, 16);
// buffer[1] = Character.forDigit(src[i] & 0x0F, 16);
// stringBuilder.append(buffer);
// }
// return stringBuilder.toString();
// }
/**
* 數(shù)組轉(zhuǎn)換成十六進(jìn)制字符串
*
* @param bArray
* @return
*/
public static String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i < bArray.length; i++) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
@Override
protected void onPause() {
super.onPause();
if (nfcAdapter != null)
nfcAdapter.disableForegroundDispatch(this);
}
@Override
protected void onResume() {
super.onResume();
if (!this.nfcAdapter.isEnabled()) {
Toast.makeText(this,"請(qǐng)?jiān)谙到y(tǒng)設(shè)置中先啟用NFC功能",Toast.LENGTH_SHORT).show();
}
if (nfcAdapter != null)
nfcAdapter.enableForegroundDispatch(this, pendingIntent,
null, null);
}
protected void resolveIntent(Intent intent) {
// 得到是否檢測(cè)到TAG觸發(fā)
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())
|| NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())
|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
// 處理該intent
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
// 獲取標(biāo)簽id數(shù)組
byte[] bytesId = tag.getId();
//獲取消息內(nèi)容
NfcMessageParser nfcMessageParser = new NfcMessageParser(intent);
List<String> tagMessage = nfcMessageParser.getTagMessage();
if (tagMessage == null || tagMessage.size() == 0) {
//Toast.makeText(this, "NFC格式不支持...", Toast.LENGTH_LONG).show();
} else {
for (int i = 0; i < tagMessage.size(); i++) {
Log.e("tag", tagMessage.get(i));
}
datas = tagMessage.get(0);
}
String info = "";
if (datas != null) {
info += "內(nèi)容:" + datas + "\n卡片ID:" + bytesToHexString(bytesId) + "\n";
} else {
info += "卡片ID:" + bytesToHexString(bytesId) + "\n";
}
String[] techList = tag.getTechList();
//分析NFC卡的類型: Mifare Classic/UltraLight Info
String cardType = "";
for (String aTechList : techList) {
if (TextUtils.equals(aTechList, "android.nfc.tech.Ndef")) {
Ndef ndef = Ndef.get(tag);
cardType += "最大數(shù)據(jù)尺寸:" + ndef.getMaxSize() + "字節(jié)";
}
}
info += cardType;
tvUid.setText("NFC信息如下:\n" + info);
}
}
}
?7、NFC解析數(shù)據(jù)類
package com.example.nfctest;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Parcelable;
import java.util.ArrayList;
import java.util.List;
/**
* 類名 NfcMessageParser
* 作者 dy
* 功能
* 創(chuàng)建日期 2017/3/14 15:54
* 修改日期 2017/3/14 15:54
*/
public class NfcMessageParser {
private Intent tagIntent;
private String TAG = "NfcMessageParser";
public NfcMessageParser() {
}
public NfcMessageParser(Intent intent) {
this.tagIntent = intent;
}
// 解析NFC信息,
public List<String> getTagMessage() {
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(tagIntent.getAction())) {
NdefMessage[] msgs = getTagNdef(tagIntent);
List<String> ndefList = getNdefString(msgs);
if (ndefList != null && ndefList.size() != 0) {
return ndefList;
}
}
return null;
}
// 得到Intent中的NDEF數(shù)據(jù)
private NdefMessage[] getTagNdef(Intent intent) {
// TODO Auto-generated method stub
NdefMessage[] msgs = null;
Parcelable[] rawMsgs = intent
.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
//把序列化數(shù)據(jù)轉(zhuǎn)成Messaeg對(duì)象
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
} else {
// Unknown tag type
byte[] empty = new byte[]{};
NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty,
empty, empty);
NdefMessage msg = new NdefMessage(new NdefRecord[]{record});
msgs = new NdefMessage[]{msg};
}
return msgs;
}
// 把Message轉(zhuǎn)成List
private List<String> getNdefString(NdefMessage[] msgs) {
// TODO Auto-generated method stub
if (msgs != null && msgs.length != 0) {
List<String> tagMessage = parser(msgs[0]);
return tagMessage;
}
return null;
}
// 把NDEF中的信息系轉(zhuǎn)化為Record,并最終轉(zhuǎn)化為String
private List<String> parser(NdefMessage ndefMessage) {
// TODO Auto-generated method stub
NdefRecord[] records = ndefMessage.getRecords();
List<String> elements = new ArrayList<>();
for (NdefRecord ndefRecord : records) {
if (!TextRecord.isText(ndefRecord)) {
return null;
}
elements.add(TextRecord.parse(ndefRecord));
}
return elements;
}
// 字符序列轉(zhuǎn)換為16進(jìn)制字符串
private String bytesToHexString(byte[] src) {
return bytesToHexString(src, true);
}
private String bytesToHexString(byte[] src, boolean isPrefix) {
StringBuilder stringBuilder = new StringBuilder();
if (isPrefix) {
stringBuilder.append("0x");
}
if (src == null || src.length <= 0) {
return null;
}
char[] buffer = new char[2];
for (int i = 0; i < src.length; i++) {
buffer[0] = Character.toUpperCase(Character.forDigit(
(src[i] >>> 4) & 0x0F, 16));
buffer[1] = Character.toUpperCase(Character.forDigit(src[i] & 0x0F,
16));
System.out.println(buffer);
stringBuilder.append(buffer);
}
return stringBuilder.toString();
}
}
8、在xml中添加nfc_tech_filter.xml文件 內(nèi)容如下
、在AndroidManifest.xml中添加以下權(quán)限以及NFC TAG標(biāo)簽
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true"/>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
9、然后在手機(jī)界面打開NFC開關(guān),不知道怎么打開或者手機(jī)設(shè)置搜索NFC哈,說明NFC功能正常了。
?10、總結(jié):
? 1、不管是上面二種那種方式 第一種如果使用這種.c執(zhí)行shell腳本記得將系統(tǒng)nfc功能開關(guān)關(guān)閉 否則會(huì)沖突不生效哈
2、第二種系統(tǒng)原生nfc 需要注意Android10以后PendingIntent類型發(fā)生變化,否則會(huì)報(bào)錯(cuò)提示。Android12以上PendingIntent需要強(qiáng)制增加FLAG_IMMUTABLE或FLAG_MUTABLE
3、調(diào)試的時(shí)候可以多拿幾種不同NFC卡進(jìn)行調(diào)試。文章來源:http://www.zghlxwxcb.cn/news/detail-489794.html
到這里所有NFC調(diào)試流程基本結(jié)束了,轉(zhuǎn)載請(qǐng)注明出處高通 Android 12 調(diào)試產(chǎn)測(cè)NFC功能_KdanMin的博客-CSDN博客。謝謝!文章來源地址http://www.zghlxwxcb.cn/news/detail-489794.html
到了這里,關(guān)于高通 Android 12 調(diào)試產(chǎn)測(cè)NFC功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!