初步调整NFC模块构造为服务加管理窗口模式,修改调试框架。
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#Created by .winboll/winboll_app_build.gradle
|
||||||
#Sat Mar 14 06:09:03 GMT 2026
|
#Mon Mar 16 02:31:00 GMT 2026
|
||||||
stageCount=0
|
stageCount=0
|
||||||
libraryProject=
|
libraryProject=
|
||||||
baseVersion=15.11
|
baseVersion=15.11
|
||||||
publishVersion=15.0.0
|
publishVersion=15.0.0
|
||||||
buildCount=6
|
buildCount=12
|
||||||
baseBetaVersion=15.0.1
|
baseBetaVersion=15.0.1
|
||||||
|
|||||||
@@ -8,6 +8,8 @@
|
|||||||
|
|
||||||
<!-- 拥有完全的网络访问权限 -->
|
<!-- 拥有完全的网络访问权限 -->
|
||||||
<uses-permission android:name="android.permission.INTERNET"/>
|
<uses-permission android:name="android.permission.INTERNET"/>
|
||||||
|
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||||
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||||
|
|
||||||
<uses-feature
|
<uses-feature
|
||||||
android:name="android.hardware.nfc"
|
android:name="android.hardware.nfc"
|
||||||
@@ -52,10 +54,21 @@
|
|||||||
|
|
||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
|
<service
|
||||||
|
android:name=".nfc.AutoNFCService"
|
||||||
|
android:exported="false" />
|
||||||
|
|
||||||
|
<receiver
|
||||||
|
android:name=".nfc.AutoNFCBootReceiver"
|
||||||
|
android:exported="true">
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.BOOT_COMPLETED" />
|
||||||
|
</intent-filter>
|
||||||
|
</receiver>
|
||||||
|
|
||||||
<meta-data
|
<meta-data
|
||||||
android:name="android.max_aspect"
|
android:name="android.max_aspect"
|
||||||
android:value="4.0"/>
|
android:value="4.0"/>
|
||||||
|
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|||||||
@@ -1,38 +1,70 @@
|
|||||||
package cc.winboll.studio.autonfc;
|
package cc.winboll.studio.autonfc;
|
||||||
|
|
||||||
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.view.Menu;
|
||||||
|
import android.view.MenuItem;
|
||||||
|
import android.view.View;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.appcompat.widget.Toolbar;
|
import androidx.appcompat.widget.Toolbar;
|
||||||
import cc.winboll.studio.libappbase.LogView;
|
import cc.winboll.studio.autonfc.nfc.AutoNFCService;
|
||||||
import cc.winboll.studio.libappbase.ToastUtils;
|
|
||||||
import android.view.View;
|
|
||||||
import android.content.Intent;
|
|
||||||
import cc.winboll.studio.autonfc.nfc.NFCInterfaceActivity;
|
import cc.winboll.studio.autonfc.nfc.NFCInterfaceActivity;
|
||||||
|
import cc.winboll.studio.libappbase.LogActivity;
|
||||||
|
import cc.winboll.studio.libappbase.ToastUtils;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
LogView mLogView;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
||||||
setSupportActionBar(toolbar);
|
setSupportActionBar(toolbar);
|
||||||
|
|
||||||
mLogView = findViewById(R.id.logview);
|
|
||||||
|
|
||||||
ToastUtils.show("onCreate");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
mLogView.start();
|
}
|
||||||
|
|
||||||
|
// 加载菜单
|
||||||
|
@Override
|
||||||
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
|
getMenuInflater().inflate(R.menu.main_menu, menu);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 菜单点击
|
||||||
|
@Override
|
||||||
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
|
int id = item.getItemId();
|
||||||
|
|
||||||
|
if (id == R.id.menu_start_service) {
|
||||||
|
startService(new Intent(this, AutoNFCService.class));
|
||||||
|
ToastUtils.show("NFC服务已启动");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (id == R.id.menu_stop_service) {
|
||||||
|
stopService(new Intent(this, AutoNFCService.class));
|
||||||
|
ToastUtils.show("NFC服务已停止");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (id == R.id.menu_log) {
|
||||||
|
LogActivity.startLogActivity(this);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return super.onOptionsItemSelected(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onNFCInterfaceActivity(View view) {
|
public void onNFCInterfaceActivity(View view) {
|
||||||
startActivity(new Intent(this, NFCInterfaceActivity.class));
|
startActivity(new Intent(this, NFCInterfaceActivity.class));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package cc.winboll.studio.autonfc.models;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
|
||||||
|
* @Date 2026/03/16 09:38
|
||||||
|
*/
|
||||||
|
public class NfcTermuxCmd {
|
||||||
|
public String script; // 要执行的预制脚本名(如 auth.sh)
|
||||||
|
public String[] args; // 脚本参数
|
||||||
|
public String workDir; // 工作目录
|
||||||
|
public boolean background; // 是否后台执行
|
||||||
|
public String resultDir; // 结果输出目录(可为 null)
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package cc.winboll.studio.autonfc.nfc;
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import cc.winboll.studio.libappbase.LogUtils;
|
||||||
|
/*
|
||||||
|
* 源码说明与描述:
|
||||||
|
* 开机自启动广播接收器,用于开机自动启动 NFC 监听服务
|
||||||
|
*
|
||||||
|
* 作者:豆包&ZhanGSKen<zhangsken@qq.com>
|
||||||
|
* 创建时间:2026-03-16 16:10:00
|
||||||
|
* 最后编辑时间:2026-03-16 18:20:00
|
||||||
|
*/
|
||||||
|
public class AutoNFCBootReceiver extends BroadcastReceiver {
|
||||||
|
|
||||||
|
private static final String TAG = "AutoNFCBootReceiver";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
|
||||||
|
LogUtils.d(TAG, "开机完成,启动 AutoNFC 监听服务");
|
||||||
|
Intent service = new Intent(context, AutoNFCService.class);
|
||||||
|
context.startService(service);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,162 @@
|
|||||||
|
package cc.winboll.studio.autonfc.nfc;
|
||||||
|
|
||||||
|
import android.app.Notification;
|
||||||
|
import android.app.NotificationChannel;
|
||||||
|
import android.app.NotificationManager;
|
||||||
|
import android.app.PendingIntent;
|
||||||
|
import android.app.Service;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.os.IBinder;
|
||||||
|
import android.nfc.NdefMessage;
|
||||||
|
import android.nfc.NdefRecord;
|
||||||
|
import android.nfc.NfcAdapter;
|
||||||
|
import android.nfc.Tag;
|
||||||
|
import android.nfc.tech.Ndef;
|
||||||
|
import android.widget.Toast;
|
||||||
|
import cc.winboll.studio.autonfc.R;
|
||||||
|
import cc.winboll.studio.libappbase.LogUtils;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 源码说明与描述:
|
||||||
|
* NFC 后台监听服务(正规保活版)
|
||||||
|
* 保活方式:前台服务 + START_STICKY + 任务销毁重启 + 开机自启
|
||||||
|
*
|
||||||
|
* 作者:豆包&ZhanGSKen<zhangsken@qq.com>
|
||||||
|
* 创建时间:2026-03-16 16:10:00
|
||||||
|
* 最后编辑时间:2026-03-16 18:20:00
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class AutoNFCService extends Service {
|
||||||
|
|
||||||
|
public static final String TAG = "AutoNFCService";
|
||||||
|
private static final int NOTIFICATION_ID = 0x1001;
|
||||||
|
private static final String CHANNEL_ID = "AUTO_NFC_SERVICE_CHANNEL";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate() {
|
||||||
|
super.onCreate();
|
||||||
|
LogUtils.d(TAG, "onCreate");
|
||||||
|
startForeground(NOTIFICATION_ID, buildNotification());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Notification buildNotification() {
|
||||||
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
|
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
NotificationChannel channel = new NotificationChannel(
|
||||||
|
CHANNEL_ID,
|
||||||
|
"AutoNFC 后台监听",
|
||||||
|
NotificationManager.IMPORTANCE_LOW
|
||||||
|
);
|
||||||
|
channel.setSound(null, null);
|
||||||
|
channel.setVibrationPattern(null);
|
||||||
|
nm.createNotificationChannel(channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
Intent intent = new Intent(this, NFCInterfaceActivity.class);
|
||||||
|
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
|
|
||||||
|
return new Notification.Builder(this)
|
||||||
|
.setChannelId(CHANNEL_ID)
|
||||||
|
.setSmallIcon(R.mipmap.ic_launcher)
|
||||||
|
.setContentTitle("AutoNFC 运行中")
|
||||||
|
.setContentText("NFC 卡片监听已启动")
|
||||||
|
.setContentIntent(pi)
|
||||||
|
.setSound(null)
|
||||||
|
.setVibrate(null)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
|
LogUtils.d(TAG, "onStartCommand");
|
||||||
|
if (intent != null) {
|
||||||
|
handleNfcIntent(intent);
|
||||||
|
}
|
||||||
|
return START_STICKY;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void handleNfcIntent(Intent intent) {
|
||||||
|
String action = intent.getAction();
|
||||||
|
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)
|
||||||
|
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
|
||||||
|
|| NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
|
||||||
|
|
||||||
|
LogUtils.d(TAG, "NFC 卡片已靠近");
|
||||||
|
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
|
||||||
|
parseNdefData(tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void parseNdefData(Tag tag) {
|
||||||
|
if (tag == null) {
|
||||||
|
showToast("标签为空");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ndef ndef = Ndef.get(tag);
|
||||||
|
if (ndef == null) {
|
||||||
|
showToast("不支持NDEF格式");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
ndef.connect();
|
||||||
|
NdefMessage msg = ndef.getNdefMessage();
|
||||||
|
if (msg == null) {
|
||||||
|
ndef.close();
|
||||||
|
showToast("未读取到NDEF数据");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
NdefRecord[] records = msg.getRecords();
|
||||||
|
if (records != null && records.length > 0) {
|
||||||
|
byte[] payload = records[0].getPayload();
|
||||||
|
if (payload.length > 0) {
|
||||||
|
int langLen = payload[0] & 0x3F;
|
||||||
|
int start = 1 + langLen;
|
||||||
|
if (start < payload.length) {
|
||||||
|
String data = new String(payload, start, payload.length - start, Charset.forName("UTF-8"));
|
||||||
|
LogUtils.d(TAG, "读取数据:" + data);
|
||||||
|
// 只弹出Toast显示内容
|
||||||
|
showToast("NFC数据:" + data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ndef.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
LogUtils.e(TAG, "解析失败", e);
|
||||||
|
showToast("读取失败:" + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 弹出Toast
|
||||||
|
*/
|
||||||
|
private void showToast(final String msg) {
|
||||||
|
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTaskRemoved(Intent rootIntent) {
|
||||||
|
super.onTaskRemoved(rootIntent);
|
||||||
|
LogUtils.d(TAG, "任务被移除,重启服务");
|
||||||
|
Intent intent = new Intent(getApplicationContext(), AutoNFCService.class);
|
||||||
|
startService(intent);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroy() {
|
||||||
|
super.onDestroy();
|
||||||
|
LogUtils.d(TAG, "onDestroy");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IBinder onBind(Intent intent) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,3 +1,10 @@
|
|||||||
|
/*
|
||||||
|
- 源码说明与描述:
|
||||||
|
- NFC 调试界面,负责前台分发、UI 交互、写入控制
|
||||||
|
- 作者:豆包&ZhanGSKenzhangsken@qq.com
|
||||||
|
- 创建时间:2026-03-16 10:00:00
|
||||||
|
- 最后编辑时间:2026-03-16 18:59:00
|
||||||
|
*/
|
||||||
package cc.winboll.studio.autonfc.nfc;
|
package cc.winboll.studio.autonfc.nfc;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
@@ -18,14 +25,15 @@ import android.widget.EditText;
|
|||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
import cc.winboll.studio.autonfc.R;
|
import cc.winboll.studio.autonfc.R;
|
||||||
|
import cc.winboll.studio.libappbase.LogUtils;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
/**
|
|
||||||
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
|
|
||||||
* @Date 2026/03/14 13:35
|
|
||||||
*/
|
|
||||||
public class NFCInterfaceActivity extends Activity {
|
public class NFCInterfaceActivity extends Activity {
|
||||||
|
|
||||||
|
// ========================= 常量定义 =========================
|
||||||
|
private static final String TAG = "NFCInterfaceActivity";
|
||||||
|
|
||||||
|
// ========================= 成员属性 =========================
|
||||||
private NfcAdapter mNfcAdapter;
|
private NfcAdapter mNfcAdapter;
|
||||||
private PendingIntent mPendingIntent;
|
private PendingIntent mPendingIntent;
|
||||||
private IntentFilter[] mIntentFilters;
|
private IntentFilter[] mIntentFilters;
|
||||||
@@ -38,25 +46,28 @@ public class NFCInterfaceActivity extends Activity {
|
|||||||
private Button btnWriteNfc;
|
private Button btnWriteNfc;
|
||||||
|
|
||||||
private Tag mCurrentTag;
|
private Tag mCurrentTag;
|
||||||
private boolean mIsWaitForRead = false;
|
|
||||||
|
|
||||||
|
// ========================= 生命周期 =========================
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
LogUtils.d(TAG, "onCreate() 调用,savedInstanceState: " + (savedInstanceState != null ? "非空" : "空"));
|
||||||
setContentView(R.layout.activity_nfc_interface);
|
setContentView(R.layout.activity_nfc_interface);
|
||||||
|
|
||||||
tvStatus = findViewById(R.id.tv_nfc_status);
|
tvStatus = (TextView) findViewById(R.id.tv_nfc_status);
|
||||||
tvData = findViewById(R.id.tv_nfc_data);
|
tvData = (TextView) findViewById(R.id.tv_nfc_data);
|
||||||
etWriteContent = findViewById(R.id.et_write_content);
|
etWriteContent = (EditText) findViewById(R.id.et_write_content);
|
||||||
btnReadNfc = findViewById(R.id.btn_read_nfc);
|
btnReadNfc = (Button) findViewById(R.id.btn_read_nfc);
|
||||||
btnWriteNfc = findViewById(R.id.btn_write_nfc);
|
btnWriteNfc = (Button) findViewById(R.id.btn_write_nfc);
|
||||||
|
|
||||||
initNFC();
|
initNFC();
|
||||||
|
startAutoNFCService();
|
||||||
|
|
||||||
btnReadNfc.setOnClickListener(new View.OnClickListener() {
|
btnReadNfc.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
startWaitForRead();
|
LogUtils.d(TAG, "btnReadNfc 点击,进入等待读卡模式");
|
||||||
|
updateStatus("等待卡片靠近...");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -64,16 +75,20 @@ public class NFCInterfaceActivity extends Activity {
|
|||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
String content = etWriteContent.getText().toString().trim();
|
String content = etWriteContent.getText().toString().trim();
|
||||||
|
LogUtils.d(TAG, "btnWriteNfc 点击,待写入内容: " + content);
|
||||||
writeNfc(content);
|
writeNfc(content);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ========================= 初始化 =========================
|
||||||
private void initNFC() {
|
private void initNFC() {
|
||||||
|
LogUtils.d(TAG, "initNFC() 开始初始化 NFC 配置");
|
||||||
NfcManager manager = (NfcManager) getSystemService(NFC_SERVICE);
|
NfcManager manager = (NfcManager) getSystemService(NFC_SERVICE);
|
||||||
mNfcAdapter = manager.getDefaultAdapter();
|
mNfcAdapter = manager.getDefaultAdapter();
|
||||||
|
|
||||||
if (mNfcAdapter == null) {
|
if (mNfcAdapter == null) {
|
||||||
|
LogUtils.e(TAG, "initNFC() 设备不支持 NFC");
|
||||||
updateStatus("设备不支持NFC");
|
updateStatus("设备不支持NFC");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -83,26 +98,31 @@ public class NFCInterfaceActivity extends Activity {
|
|||||||
|
|
||||||
IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
|
IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
|
||||||
try {
|
try {
|
||||||
filter.addDataType("*/*");
|
filter.addDataType("/");
|
||||||
} catch (IntentFilter.MalformedMimeTypeException e) {
|
} catch (IntentFilter.MalformedMimeTypeException e) {
|
||||||
e.printStackTrace();
|
LogUtils.e(TAG, "initNFC() 过滤器异常: " + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
mIntentFilters = new IntentFilter[]{filter};
|
mIntentFilters = new IntentFilter[] { filter };
|
||||||
mTechLists = new String[][]{{Ndef.class.getName()}, {NdefFormatable.class.getName()}};
|
mTechLists = new String[][] {
|
||||||
updateStatus("NFC初始化完成");
|
{ Ndef.class.getName() },
|
||||||
|
{ NdefFormatable.class.getName() }
|
||||||
|
};
|
||||||
|
LogUtils.d(TAG, "initNFC() 完成 NFC 前台分发配置");
|
||||||
|
updateStatus("NFC 初始化完成");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startWaitForRead() {
|
private void startAutoNFCService() {
|
||||||
mIsWaitForRead = true;
|
LogUtils.d(TAG, "startAutoNFCService() 启动后台监听服务");
|
||||||
updateStatus("请靠近NFC卡片进行读取");
|
Intent service = new Intent(this, AutoNFCService.class);
|
||||||
showToast("请靠近卡片");
|
startService(service);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ========================= 生命周期回调 =========================
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
NfcStateMonitor.startMonitor();
|
LogUtils.d(TAG, "onResume() 启用前台分发");
|
||||||
if (mNfcAdapter != null) {
|
if (mNfcAdapter != null) {
|
||||||
mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mIntentFilters, mTechLists);
|
mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mIntentFilters, mTechLists);
|
||||||
}
|
}
|
||||||
@@ -111,7 +131,7 @@ public class NFCInterfaceActivity extends Activity {
|
|||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
NfcStateMonitor.stopMonitor();
|
LogUtils.d(TAG, "onPause() 关闭前台分发");
|
||||||
if (mNfcAdapter != null) {
|
if (mNfcAdapter != null) {
|
||||||
mNfcAdapter.disableForegroundDispatch(this);
|
mNfcAdapter.disableForegroundDispatch(this);
|
||||||
}
|
}
|
||||||
@@ -120,98 +140,32 @@ public class NFCInterfaceActivity extends Activity {
|
|||||||
@Override
|
@Override
|
||||||
protected void onNewIntent(Intent intent) {
|
protected void onNewIntent(Intent intent) {
|
||||||
super.onNewIntent(intent);
|
super.onNewIntent(intent);
|
||||||
String action = intent.getAction();
|
LogUtils.d(TAG, "onNewIntent() 收到 NFC 卡片意图: " + intent.getAction());
|
||||||
|
|
||||||
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)
|
|
||||||
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
|
|
||||||
|| NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
|
|
||||||
|
|
||||||
NfcStateMonitor.notifyNfcConnected();
|
|
||||||
updateStatus("卡片已靠近");
|
|
||||||
mCurrentTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
|
mCurrentTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
|
||||||
|
|
||||||
if (mIsWaitForRead) {
|
Intent serviceIntent = new Intent(this, AutoNFCService.class);
|
||||||
mIsWaitForRead = false;
|
serviceIntent.putExtras(intent);
|
||||||
readNfcData(intent);
|
startService(serviceIntent);
|
||||||
}
|
|
||||||
}
|
updateStatus("卡片已靠近,数据解析中...");
|
||||||
}
|
|
||||||
|
|
||||||
public void readNfcData(Intent intent) {
|
|
||||||
if (intent == null) {
|
|
||||||
NfcStateMonitor.notifyReadFail("Intent为空");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
|
|
||||||
if (tag == null) {
|
|
||||||
NfcStateMonitor.notifyReadFail("标签为空");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ndef ndef = Ndef.get(tag);
|
|
||||||
if (ndef == null) {
|
|
||||||
NfcStateMonitor.notifyReadFail("不支持NDEF");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
ndef.connect();
|
|
||||||
NdefMessage msg = ndef.getNdefMessage();
|
|
||||||
if (msg == null) {
|
|
||||||
NfcStateMonitor.notifyReadFail("空标签");
|
|
||||||
ndef.close();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
NdefRecord[] records = msg.getRecords();
|
|
||||||
if (records != null && records.length > 0) {
|
|
||||||
// byte[] payload = records[0].getPayload();
|
|
||||||
// String data = new String(payload, 1, payload.length - 1, Charset.forName("UTF-8"));
|
|
||||||
// updateData(data);
|
|
||||||
// updateStatus("读取成功");
|
|
||||||
// NfcStateMonitor.notifyReadSuccess(data);
|
|
||||||
byte[] payload = records[0].getPayload();
|
|
||||||
if (payload.length > 0) {
|
|
||||||
int langCodeLength = payload[0] & 0x3F; // 标准NDEF Text格式,语言码长度存在第0字节的低6位
|
|
||||||
int textStartIndex = 1 + langCodeLength;
|
|
||||||
if (textStartIndex < payload.length) {
|
|
||||||
String data = new String(payload, textStartIndex, payload.length - textStartIndex, Charset.forName("UTF-8"));
|
|
||||||
updateData(data);
|
|
||||||
updateStatus("读取成功");
|
|
||||||
NfcStateMonitor.notifyReadSuccess(data);
|
|
||||||
} else {
|
|
||||||
NfcStateMonitor.notifyReadFail("标签数据为空");
|
|
||||||
updateStatus("标签数据为空");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
NfcStateMonitor.notifyReadFail("标签 payload 为空");
|
|
||||||
updateStatus("标签 payload 为空");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
ndef.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
NfcStateMonitor.notifyReadFail(e.getMessage());
|
|
||||||
updateStatus("读取失败:" + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ========================= 写入逻辑 =========================
|
||||||
public boolean writeNfc(String text) {
|
public boolean writeNfc(String text) {
|
||||||
|
LogUtils.d(TAG, "writeNfc() 写入文本: " + text);
|
||||||
if (mCurrentTag == null) {
|
if (mCurrentTag == null) {
|
||||||
NfcStateMonitor.notifyWriteFail("未检测到卡片");
|
LogUtils.w(TAG, "writeNfc() 未检测到卡片,无法写入");
|
||||||
updateStatus("请先靠近卡片");
|
updateStatus("请先靠近卡片");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (text.isEmpty()) {
|
if (text.isEmpty()) {
|
||||||
NfcStateMonitor.notifyWriteFail("输入内容不能为空");
|
LogUtils.w(TAG, "writeNfc() 写入内容为空");
|
||||||
updateStatus("请输入写入内容");
|
updateStatus("输入内容不能为空");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
NdefRecord record = createTextRecord(text);
|
NdefRecord record = createTextRecord(text);
|
||||||
NdefMessage msg = new NdefMessage(new NdefRecord[]{record});
|
NdefMessage msg = new NdefMessage(new NdefRecord[] { record });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Ndef ndef = Ndef.get(mCurrentTag);
|
Ndef ndef = Ndef.get(mCurrentTag);
|
||||||
@@ -220,30 +174,35 @@ public class NFCInterfaceActivity extends Activity {
|
|||||||
if (ndef.isWritable()) {
|
if (ndef.isWritable()) {
|
||||||
ndef.writeNdefMessage(msg);
|
ndef.writeNdefMessage(msg);
|
||||||
ndef.close();
|
ndef.close();
|
||||||
|
LogUtils.i(TAG, "writeNfc() 写入成功");
|
||||||
updateStatus("写入成功");
|
updateStatus("写入成功");
|
||||||
updateData(text);
|
updateData(text);
|
||||||
NfcStateMonitor.notifyWriteSuccess();
|
NfcStateMonitor.notifyWriteSuccess();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
NdefFormatable formatable = NdefFormatable.get(mCurrentTag);
|
NdefFormatable f = NdefFormatable.get(mCurrentTag);
|
||||||
if (formatable != null) {
|
if (f != null) {
|
||||||
formatable.connect();
|
f.connect();
|
||||||
formatable.format(msg);
|
f.format(msg);
|
||||||
formatable.close();
|
f.close();
|
||||||
|
LogUtils.i(TAG, "writeNfc() 格式化并写入成功");
|
||||||
updateStatus("格式化并写入成功");
|
updateStatus("格式化并写入成功");
|
||||||
NfcStateMonitor.notifyWriteSuccess();
|
NfcStateMonitor.notifyWriteSuccess();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
NfcStateMonitor.notifyWriteFail(e.getMessage());
|
LogUtils.e(TAG, "writeNfc() 写入异常: " + e.getMessage(), e);
|
||||||
updateStatus("写入失败:" + e.getMessage());
|
updateStatus("写入失败:" + e.getMessage());
|
||||||
|
NfcStateMonitor.notifyWriteFail(e.getMessage());
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ========================= NDEF 构造 =========================
|
||||||
private NdefRecord createTextRecord(String text) {
|
private NdefRecord createTextRecord(String text) {
|
||||||
|
LogUtils.d(TAG, "createTextRecord() 构造 NDEF 文本记录");
|
||||||
byte[] langBytes = "en".getBytes(Charset.forName("UTF-8"));
|
byte[] langBytes = "en".getBytes(Charset.forName("UTF-8"));
|
||||||
byte[] textBytes = text.getBytes(Charset.forName("UTF-8"));
|
byte[] textBytes = text.getBytes(Charset.forName("UTF-8"));
|
||||||
byte[] payload = new byte[1 + langBytes.length + textBytes.length];
|
byte[] payload = new byte[1 + langBytes.length + textBytes.length];
|
||||||
@@ -253,6 +212,7 @@ public class NFCInterfaceActivity extends Activity {
|
|||||||
return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payload);
|
return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ========================= UI 更新 =========================
|
||||||
private void updateStatus(final String s) {
|
private void updateStatus(final String s) {
|
||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
@@ -266,18 +226,8 @@ public class NFCInterfaceActivity extends Activity {
|
|||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
tvData.setText("读取/写入内容:\n" + s);
|
tvData.setText("内容:\n" + s);
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void showToast(final String s) {
|
|
||||||
runOnUiThread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
Toast.makeText(NFCInterfaceActivity.this, s, Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,18 +34,5 @@
|
|||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
<LinearLayout
|
|
||||||
android:orientation="vertical"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="0dp"
|
|
||||||
android:layout_weight="1.0">
|
|
||||||
|
|
||||||
<cc.winboll.studio.libappbase.LogView
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
android:id="@+id/logview"/>
|
|
||||||
|
|
||||||
</LinearLayout>
|
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
|||||||
12
autonfc/src/main/res/menu/main_menu.xml
Normal file
12
autonfc/src/main/res/menu/main_menu.xml
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<item
|
||||||
|
android:id="@+id/menu_start_service"
|
||||||
|
android:title="启动NFC服务"/>
|
||||||
|
<item
|
||||||
|
android:id="@+id/menu_stop_service"
|
||||||
|
android:title="停止NFC服务"/>
|
||||||
|
<item
|
||||||
|
android:id="@+id/menu_log"
|
||||||
|
android:title="启动日志"/>
|
||||||
|
</menu>
|
||||||
Reference in New Issue
Block a user