添加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
#Sat Mar 14 05:43:18 GMT 2026
#Sat Mar 14 05:50:51 GMT 2026
stageCount=0
libraryProject=
baseVersion=15.11
publishVersion=15.0.0
buildCount=3
buildCount=4
baseBetaVersion=15.0.1

View File

@@ -24,6 +24,25 @@ import cc.winboll.studio.autonfc.R;
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @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 {
private NfcAdapter mNfcAdapter;
@@ -35,10 +54,14 @@ public class NFCInterfaceActivity extends Activity {
private TextView tvStatus;
private TextView tvData;
private Button btnWrite;
private Button btnReadNfc; // 新增:主动读取按钮
// 当前标签
private Tag mCurrentTag;
// 标记是否等待NFC读取
private boolean mIsWaitForRead = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -47,15 +70,34 @@ public class NFCInterfaceActivity extends Activity {
tvStatus = findViewById(R.id.tv_nfc_status);
tvData = findViewById(R.id.tv_nfc_data);
btnWrite = findViewById(R.id.btn_write_nfc);
btnReadNfc = findViewById(R.id.btn_read_nfc); // 绑定
initNfc();
// 写入按钮
btnWrite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
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() {
@@ -84,7 +126,6 @@ public class NFCInterfaceActivity extends Activity {
@Override
protected void onResume() {
super.onResume();
// 静态启动监听
NfcStateMonitor.startMonitor();
enableForeground();
}
@@ -92,7 +133,6 @@ public class NFCInterfaceActivity extends Activity {
@Override
protected void onPause() {
super.onPause();
// 静态停止监听
NfcStateMonitor.stopMonitor();
disableForeground();
}
@@ -118,31 +158,35 @@ public class NFCInterfaceActivity extends Activity {
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
// 通知接口:已连接
NfcStateMonitor.notifyNfcConnected();
updateUiStatus("卡片已靠近");
updateUiStatus("NFC卡片已靠近");
mCurrentTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
readNfc(intent);
// ========== 新增:如果是等待读取,则自动读取 ==========
if (mIsWaitForRead) {
mIsWaitForRead = false;
readNfc(intent);
}
}
}
// ========== 公开读取方法 ==========
// 公开读取方法
public void readNfc(Intent intent) {
if (intent == null) {
NfcStateMonitor.notifyReadFail("intent null");
NfcStateMonitor.notifyReadFail("Intent 为空");
return;
}
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (tag == null) {
NfcStateMonitor.notifyReadFail("tag null");
NfcStateMonitor.notifyReadFail("标签为空");
return;
}
Ndef ndef = Ndef.get(tag);
if (ndef == null) {
NfcStateMonitor.notifyReadFail("不支持NDEF");
NfcStateMonitor.notifyReadFail("不支持NDEF格式");
return;
}
@@ -150,24 +194,29 @@ public class NFCInterfaceActivity extends Activity {
ndef.connect();
NdefMessage msg = ndef.getNdefMessage();
if (msg == null) {
NfcStateMonitor.notifyReadFail("标签");
NfcStateMonitor.notifyReadFail("标签为空数据");
ndef.close();
return;
}
NdefRecord[] records = msg.getRecords();
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);
updateUiStatus("读取成功");
NfcStateMonitor.notifyReadSuccess(data);
}
ndef.close();
} catch (Exception e) {
NfcStateMonitor.notifyReadFail(e.getMessage());
updateUiStatus("读取失败:" + e.getMessage());
}
}
// ========== 公开写入方法 ==========
// 公开写入方法
public boolean writeNfc(String text) {
if (mCurrentTag == null) {
NfcStateMonitor.notifyWriteFail("未检测到标签");
@@ -231,7 +280,7 @@ public class NFCInterfaceActivity extends Activity {
runOnUiThread(new Runnable() {
@Override
public void run() {
tvData.setText(s);
tvData.setText("读取数据:\n" + s);
}
});
}

View File

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