1、在dev/nxpnfc节点添加对应的文件权限
on property:sys.boot_completed=1
# nfc add by zm
chmod 777 /dev/nxpnfc
2、在vendor/nxp/nfcdevice-nfc.mk文件中 修改NFC添加到编译路径如下所示,跟平时内置apk方式有点类似
PRODUCT_PACKAGES += NFCTestApp
-include \vendor\nxp\nfc\FactoryTestApp\Android.mk
3、然后执行make命令 这时候就会在out目录下面system/bin目录生成 NFCTestApp可执行文件
4、产测中执行shell命令 代码如下
val nfcShell = Shell.exe("/system/bin/NFCTestApp")
if(nfcShell.contains("xxx"){
requireActivity().runOnUiThread(Runnable {
textView!!.text = "识别NFC成功"
})
}
5、其他原生识别NFC的方式
6、Android原生识别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,"设备不支持NFC",Toast.LENGTH_LONG).show();
return;
}
if (nfcAdapter != null && !nfcAdapter.isEnabled()) {
Toast.makeText(MainActivity.this, "请在系统设置中先启用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);
}
// //字符序列转换为16进制字符串
// 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();
// }
/**
* 数组转换成十六进制字符串
*
* @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,"请在系统设置中先启用NFC功能",Toast.LENGTH_SHORT).show();
}
if (nfcAdapter != null)
nfcAdapter.enableForegroundDispatch(this, pendingIntent,
null, null);
}
protected void resolveIntent(Intent intent) {
// 得到是否检测到TAG触发
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);
// 获取标签id数组
byte[] bytesId = tag.getId();
//获取消息内容
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 += "内容:" + 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 += "最大数据尺寸:" + ndef.getMaxSize() + "字节";
}
}
info += cardType;
tvUid.setText("NFC信息如下:\n" + info);
}
}
}
7、NFC解析数据类
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
* 功能
* 创建日期 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数据
private NdefMessage[] getTagNdef(Intent intent) {
// TODO Auto-generated method stub
NdefMessage[] msgs = null;
Parcelable[] rawMsgs = intent
.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
//把序列化数据转成Messaeg对象
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转成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中的信息系转化为Record,并最终转化为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;
}
// 字符序列转换为16进制字符串
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文件 内容如下
、在AndroidManifest.xml中添加以下权限以及NFC TAG标签
<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、然后在手机界面打开NFC开关,不知道怎么打开或者手机设置搜索NFC哈,说明NFC功能正常了。
10、总结:
1、不管是上面二种那种方式 第一种如果使用这种.c执行shell脚本记得将系统nfc功能开关关闭 否则会冲突不生效哈
2、第二种系统原生nfc 需要注意Android10以后PendingIntent类型发生变化,否则会报错提示。Android12以上PendingIntent需要强制增加FLAG_IMMUTABLE或FLAG_MUTABLE
3、调试的时候可以多拿几种不同NFC卡进行调试。文章来源:https://www.toymoban.com/news/detail-489794.html
到这里所有NFC调试流程基本结束了,转载请注明出处高通 Android 12 调试产测NFC功能_KdanMin的博客-CSDN博客。谢谢!文章来源地址https://www.toymoban.com/news/detail-489794.html
到了这里,关于高通 Android 12 调试产测NFC功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!