添加NFC数据读取功能,数据读取功能未测试。

This commit is contained in:
2026-03-14 13:53:53 +08:00
parent 37173c7c3a
commit 94bfb3e878
3 changed files with 72 additions and 17 deletions

View File

@@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle #Created by .winboll/winboll_app_build.gradle
#Sat Mar 14 05:43:18 GMT 2026 #Sat Mar 14 05:50:51 GMT 2026
stageCount=0 stageCount=0
libraryProject= libraryProject=
baseVersion=15.11 baseVersion=15.11
publishVersion=15.0.0 publishVersion=15.0.0
buildCount=3 buildCount=4
baseBetaVersion=15.0.1 baseBetaVersion=15.0.1

View File

@@ -24,6 +24,25 @@ import cc.winboll.studio.autonfc.R;
* @Author 豆包&ZhanGSKen<zhangsken@qq.com> * @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @Date 2026/03/14 13:35 * @Date 2026/03/14 13:35
*/ */
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcManager;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.nio.charset.Charset;
public class NFCInterfaceActivity extends Activity { public class NFCInterfaceActivity extends Activity {
private NfcAdapter mNfcAdapter; private NfcAdapter mNfcAdapter;
@@ -35,10 +54,14 @@ public class NFCInterfaceActivity extends Activity {
private TextView tvStatus; private TextView tvStatus;
private TextView tvData; private TextView tvData;
private Button btnWrite; private Button btnWrite;
private Button btnReadNfc; // 新增:主动读取按钮
// 当前标签 // 当前标签
private Tag mCurrentTag; private Tag mCurrentTag;
// 标记是否等待NFC读取
private boolean mIsWaitForRead = false;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@@ -47,15 +70,34 @@ public class NFCInterfaceActivity extends Activity {
tvStatus = findViewById(R.id.tv_nfc_status); tvStatus = findViewById(R.id.tv_nfc_status);
tvData = findViewById(R.id.tv_nfc_data); tvData = findViewById(R.id.tv_nfc_data);
btnWrite = findViewById(R.id.btn_write_nfc); btnWrite = findViewById(R.id.btn_write_nfc);
btnReadNfc = findViewById(R.id.btn_read_nfc); // 绑定
initNfc(); initNfc();
// 写入按钮
btnWrite.setOnClickListener(new View.OnClickListener() { btnWrite.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
writeNfc("Test NFC Data Java7"); writeNfc("Test NFC Data Java7");
} }
}); });
// ========== 新增:主动读取按钮点击 ==========
btnReadNfc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startWaitForNfcRead();
}
});
}
/**
* 开始等待NFC卡片靠近测试用
*/
public void startWaitForNfcRead() {
mIsWaitForRead = true;
updateUiStatus("请靠近NFC卡片即将自动读取...");
showToast("请靠近NFC卡片");
} }
private void initNfc() { private void initNfc() {
@@ -84,7 +126,6 @@ public class NFCInterfaceActivity extends Activity {
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
// 静态启动监听
NfcStateMonitor.startMonitor(); NfcStateMonitor.startMonitor();
enableForeground(); enableForeground();
} }
@@ -92,7 +133,6 @@ public class NFCInterfaceActivity extends Activity {
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
// 静态停止监听
NfcStateMonitor.stopMonitor(); NfcStateMonitor.stopMonitor();
disableForeground(); disableForeground();
} }
@@ -118,31 +158,35 @@ public class NFCInterfaceActivity extends Activity {
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) { || NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
// 通知接口:已连接
NfcStateMonitor.notifyNfcConnected(); NfcStateMonitor.notifyNfcConnected();
updateUiStatus("卡片已靠近"); updateUiStatus("NFC卡片已靠近");
mCurrentTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); mCurrentTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
readNfc(intent);
// ========== 新增:如果是等待读取,则自动读取 ==========
if (mIsWaitForRead) {
mIsWaitForRead = false;
readNfc(intent);
}
} }
} }
// ========== 公开读取方法 ========== // 公开读取方法
public void readNfc(Intent intent) { public void readNfc(Intent intent) {
if (intent == null) { if (intent == null) {
NfcStateMonitor.notifyReadFail("intent null"); NfcStateMonitor.notifyReadFail("Intent 为空");
return; return;
} }
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (tag == null) { if (tag == null) {
NfcStateMonitor.notifyReadFail("tag null"); NfcStateMonitor.notifyReadFail("标签为空");
return; return;
} }
Ndef ndef = Ndef.get(tag); Ndef ndef = Ndef.get(tag);
if (ndef == null) { if (ndef == null) {
NfcStateMonitor.notifyReadFail("不支持NDEF"); NfcStateMonitor.notifyReadFail("不支持NDEF格式");
return; return;
} }
@@ -150,24 +194,29 @@ public class NFCInterfaceActivity extends Activity {
ndef.connect(); ndef.connect();
NdefMessage msg = ndef.getNdefMessage(); NdefMessage msg = ndef.getNdefMessage();
if (msg == null) { if (msg == null) {
NfcStateMonitor.notifyReadFail("标签"); NfcStateMonitor.notifyReadFail("标签为空数据");
ndef.close(); ndef.close();
return; return;
} }
NdefRecord[] records = msg.getRecords(); NdefRecord[] records = msg.getRecords();
if (records != null && records.length > 0) { if (records != null && records.length > 0) {
String data = new String(records[0].getPayload(), Charset.forName("UTF-8")); // 读取真实内容(去掉语言位)
NfcStateMonitor.notifyReadSuccess(data); byte[] payload = records[0].getPayload();
String data = new String(payload, 1, payload.length - 1, Charset.forName("UTF-8"));
updateUiData(data); updateUiData(data);
updateUiStatus("读取成功");
NfcStateMonitor.notifyReadSuccess(data);
} }
ndef.close(); ndef.close();
} catch (Exception e) { } catch (Exception e) {
NfcStateMonitor.notifyReadFail(e.getMessage()); NfcStateMonitor.notifyReadFail(e.getMessage());
updateUiStatus("读取失败:" + e.getMessage());
} }
} }
// ========== 公开写入方法 ========== // 公开写入方法
public boolean writeNfc(String text) { public boolean writeNfc(String text) {
if (mCurrentTag == null) { if (mCurrentTag == null) {
NfcStateMonitor.notifyWriteFail("未检测到标签"); NfcStateMonitor.notifyWriteFail("未检测到标签");
@@ -231,7 +280,7 @@ public class NFCInterfaceActivity extends Activity {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
tvData.setText(s); tvData.setText("读取数据:\n" + s);
} }
}); });
} }

View File

@@ -19,6 +19,13 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="写入测试数据"/> android:text="写入测试数据"/>
<!-- 新增主动读取NFC按钮 -->
<Button
android:id="@+id/btn_read_nfc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击后靠近NFC卡片读取数据"/>
<TextView <TextView
android:id="@+id/tv_nfc_data" android:id="@+id/tv_nfc_data"
android:layout_width="match_parent" android:layout_width="match_parent"
@@ -28,7 +35,6 @@
android:layout_marginTop="10dp" android:layout_marginTop="10dp"
android:text="数据"/> android:text="数据"/>
</LinearLayout> </LinearLayout>