前台响应机制;直接使用前台activity来捕获NFC事件进行响应
https://blog.csdn.net/qq_37565670/article/details/119240737
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kingbright.nfc_activity">
<uses-permission android:name="android.permission.NFC" />
<!-- 用于指定android程序,是否需要某种硬件或软件资源/功能。
这个是限制安装权限,只给有nfc功能的手机安装 -->
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
<!-- 表示需要设备具有某个功能。 -->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.NFC_activity">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.kingbright.nfc_activity;
import androidx.appcompat.app.AppCompatActivity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
/**
* 2023-3-14
* 在本应用获取NFC内容,不跳转到其他应用
*/
public class MainActivity extends AppCompatActivity {
NfcAdapter mNfcAdapter;
PendingIntent pIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText tv = findViewById(R.id.tv);
initNfc();
Log.i("FlashTestNFC", "onCreate");
tv.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
// 获取NFC读取的内容;获取扫描工号的内容
String CardId = tv.getText().toString();//NFC读取的内容--4166e032
System.out.println("nfc内容--"+CardId);
}
});
}
/**
* 初始化
*/
private void initNfc(){
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
pIntent = PendingIntent.getActivity(this, 0,
//在Manifest里或者这里设置当前activity启动模式,否则每次向阳NFC事件,activity会重复创建
// 当然也要按照具体情况来,你设置成singleTask也不是不行,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
0);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
//这里必须setIntent,set NFC事件响应后的intent才能拿到数据
setIntent(intent);
Log.i("FlashTestNFC", "onNewIntent");
Tag tag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
//TODO 获取数据进行下一步处理
Log.i("FlashTestNFC--Tag", bytesToHex(tag.getId()));
}
@Override
protected void onResume() {
super.onResume();
Log.i("FlashTestNFC", "onResume");
if (mNfcAdapter != null) {
//添加intent-filter
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
IntentFilter tag = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
IntentFilter tech = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
IntentFilter[] filters = new IntentFilter[]{ndef, tag, tech};
//添加 ACTION_TECH_DISCOVERED 情况下所能读取的NFC格式,这里列的比较全,实际我这里是没有用到的,因为测试的卡是NDEF的
String[][] techList = new String[][]{
new String[]{
"android.nfc.tech.Ndef",
"android.nfc.tech.NfcA",
"android.nfc.tech.NfcB",
"android.nfc.tech.NfcF",
"android.nfc.tech.NfcV",
"android.nfc.tech.NdefFormatable",
"android.nfc.tech.MifareClassic",
"android.nfc.tech.MifareUltralight",
"android.nfc.tech.NfcBarcode"
}
};
mNfcAdapter.enableForegroundDispatch(this, pIntent, filters, techList);
}
}
@Override
protected void onPause() {
super.onPause();
Log.i("FlashTestNFC", "onPause");
if (mNfcAdapter != null) {
mNfcAdapter.disableForegroundDispatch(this);
}
}
/**
* 2进制to 16进制
* @param src
* @return
*/
private static String bytesToHex(byte[] src){
StringBuffer sb = new StringBuffer();
if (src == null || src.length <= 0) {
return null;
}
String sTemp;
for (int i = 0; i < src.length; i++) {
sTemp = Integer.toHexString(0xFF & src[i]);
if (sTemp.length() < 2){
sb.append(0);
}
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
}