添加数据模型NfcTermuxCmd类的NFC卡写入和读取。
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#Created by .winboll/winboll_app_build.gradle
|
||||||
#Mon Mar 16 03:39:59 GMT 2026
|
#Mon Mar 16 06:46:34 GMT 2026
|
||||||
stageCount=0
|
stageCount=0
|
||||||
libraryProject=
|
libraryProject=
|
||||||
baseVersion=15.11
|
baseVersion=15.11
|
||||||
publishVersion=15.0.0
|
publishVersion=15.0.0
|
||||||
buildCount=27
|
buildCount=45
|
||||||
baseBetaVersion=15.0.1
|
baseBetaVersion=15.0.1
|
||||||
|
|||||||
@@ -3,16 +3,8 @@
|
|||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="cc.winboll.studio.autonfc">
|
package="cc.winboll.studio.autonfc">
|
||||||
|
|
||||||
<!-- 控制近距离通信 -->
|
|
||||||
<uses-permission android:name="android.permission.NFC"/>
|
<uses-permission android:name="android.permission.NFC"/>
|
||||||
|
|
||||||
<!-- 拥有完全的网络访问权限 -->
|
|
||||||
<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-permission android:name="android.permission.FOREGROUND_SERVICE"/>
|
||||||
|
|
||||||
<uses-feature
|
<uses-feature
|
||||||
@@ -31,60 +23,31 @@
|
|||||||
<activity
|
<activity
|
||||||
android:name=".MainActivity"
|
android:name=".MainActivity"
|
||||||
android:label="@string/app_name">
|
android:label="@string/app_name">
|
||||||
|
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
|
|
||||||
<action android:name="android.intent.action.MAIN"/>
|
<action android:name="android.intent.action.MAIN"/>
|
||||||
|
|
||||||
<category android:name="android.intent.category.LAUNCHER"/>
|
<category android:name="android.intent.category.LAUNCHER"/>
|
||||||
|
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
|
|
||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
<activity
|
<activity
|
||||||
android:name=".nfc.NFCInterfaceActivity"
|
android:name=".nfc.NFCInterfaceActivity"
|
||||||
android:launchMode="singleTop">
|
android:launchMode="singleTop">
|
||||||
|
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
|
|
||||||
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
|
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
|
||||||
|
|
||||||
<category android:name="android.intent.category.DEFAULT"/>
|
<category android:name="android.intent.category.DEFAULT"/>
|
||||||
|
|
||||||
<data android:mimeType="*/*"/>
|
<data android:mimeType="*/*"/>
|
||||||
|
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
|
|
||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
<!-- 新增:NFC 透明代理 Activity -->
|
<!-- NFC 绑定服务 -->
|
||||||
<activity
|
|
||||||
android:name=".nfc.NfcProxyActivity"
|
|
||||||
android:theme="@android:style/Theme.Translucent.NoTitleBar"
|
|
||||||
android:excludeFromRecents="true"
|
|
||||||
android:exported="false" />
|
|
||||||
|
|
||||||
<service
|
<service
|
||||||
android:name=".nfc.AutoNFCService"
|
android:name=".nfc.AutoNFCService"
|
||||||
android:exported="false"/>
|
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,10 +1,14 @@
|
|||||||
package cc.winboll.studio.autonfc;
|
package cc.winboll.studio.autonfc;
|
||||||
|
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
|
import android.content.ComponentName;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.ServiceConnection;
|
||||||
import android.nfc.NfcAdapter;
|
import android.nfc.NfcAdapter;
|
||||||
import android.nfc.Tag;
|
import android.nfc.Tag;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.IBinder;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
@@ -18,10 +22,29 @@ import cc.winboll.studio.libappbase.ToastUtils;
|
|||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
private static final String TAG = "MainActivity";
|
public static final String TAG = "MainActivity";
|
||||||
|
|
||||||
private NfcAdapter mNfcAdapter;
|
private NfcAdapter mNfcAdapter;
|
||||||
private PendingIntent mPendingIntent;
|
private PendingIntent mPendingIntent;
|
||||||
|
private AutoNFCService mService;
|
||||||
|
private boolean mBound = false;
|
||||||
|
|
||||||
|
// 服务连接
|
||||||
|
private ServiceConnection mConnection = new ServiceConnection() {
|
||||||
|
@Override
|
||||||
|
public void onServiceConnected(ComponentName name, IBinder service) {
|
||||||
|
AutoNFCService.LocalBinder binder = (AutoNFCService.LocalBinder) service;
|
||||||
|
mService = binder.getService();
|
||||||
|
mBound = true;
|
||||||
|
LogUtils.d(TAG, "onServiceConnected: 服务已绑定");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onServiceDisconnected(ComponentName name) {
|
||||||
|
mBound = false;
|
||||||
|
LogUtils.d(TAG, "onServiceDisconnected: 服务已断开");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@@ -39,6 +62,26 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
LogUtils.d(TAG, "onCreate() -> NFC 监听已绑定到 MainActivity");
|
LogUtils.d(TAG, "onCreate() -> NFC 监听已绑定到 MainActivity");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onStart() {
|
||||||
|
super.onStart();
|
||||||
|
// 绑定服务
|
||||||
|
Intent intent = new Intent(this, AutoNFCService.class);
|
||||||
|
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
|
||||||
|
LogUtils.d(TAG, "onStart: 绑定服务");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onStop() {
|
||||||
|
super.onStop();
|
||||||
|
// 解绑服务,系统会自动停止服务
|
||||||
|
if (mBound) {
|
||||||
|
unbindService(mConnection);
|
||||||
|
mBound = false;
|
||||||
|
LogUtils.d(TAG, "onStop: 解绑服务");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
@@ -61,18 +104,14 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
@Override
|
@Override
|
||||||
protected void onNewIntent(Intent intent) {
|
protected void onNewIntent(Intent intent) {
|
||||||
super.onNewIntent(intent);
|
super.onNewIntent(intent);
|
||||||
|
//ToastUtils.show("onNewIntent");
|
||||||
LogUtils.d(TAG, "onNewIntent() -> 检测到 NFC 卡片");
|
LogUtils.d(TAG, "onNewIntent() -> 检测到 NFC 卡片");
|
||||||
|
|
||||||
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
|
if (mBound && mService != null) {
|
||||||
if (tag == null) {
|
mService.handleNfcIntent(intent);
|
||||||
LogUtils.e(TAG, "onNewIntent() -> Tag 为空");
|
} else {
|
||||||
return;
|
LogUtils.e(TAG, "服务未绑定,无法处理 NFC");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 转发给服务处理
|
|
||||||
Intent serviceIntent = new Intent(this, AutoNFCService.class);
|
|
||||||
serviceIntent.putExtras(intent);
|
|
||||||
startService(serviceIntent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -85,18 +124,6 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
int id = item.getItemId();
|
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) {
|
if (id == R.id.menu_log) {
|
||||||
LogActivity.startLogActivity(this);
|
LogActivity.startLogActivity(this);
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -5,10 +5,62 @@ package cc.winboll.studio.autonfc.models;
|
|||||||
* @Date 2026/03/16 09:38
|
* @Date 2026/03/16 09:38
|
||||||
*/
|
*/
|
||||||
public class NfcTermuxCmd {
|
public class NfcTermuxCmd {
|
||||||
public String script; // 要执行的预制脚本名(如 auth.sh)
|
|
||||||
public String[] args; // 脚本参数
|
private String script; // 要执行的预制脚本名(如 auth.sh)
|
||||||
public String workDir; // 工作目录
|
private String[] args; // 脚本参数
|
||||||
public boolean background; // 是否后台执行
|
private String workDir; // 工作目录
|
||||||
public String resultDir; // 结果输出目录(可为 null)
|
private boolean background; // 是否后台执行
|
||||||
|
private String resultDir; // 结果输出目录(可为 null)
|
||||||
|
|
||||||
|
public NfcTermuxCmd() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public NfcTermuxCmd(String script, String[] args, String workDir, boolean background, String resultDir) {
|
||||||
|
this.script = script;
|
||||||
|
this.args = args;
|
||||||
|
this.workDir = workDir;
|
||||||
|
this.background = background;
|
||||||
|
this.resultDir = resultDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getScript() {
|
||||||
|
return script;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScript(String script) {
|
||||||
|
this.script = script;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] getArgs() {
|
||||||
|
return args;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setArgs(String[] args) {
|
||||||
|
this.args = args;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getWorkDir() {
|
||||||
|
return workDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWorkDir(String workDir) {
|
||||||
|
this.workDir = workDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isBackground() {
|
||||||
|
return background;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBackground(boolean background) {
|
||||||
|
this.background = background;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getResultDir() {
|
||||||
|
return resultDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setResultDir(String resultDir) {
|
||||||
|
this.resultDir = resultDir;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -12,12 +12,15 @@ import android.nfc.NdefRecord;
|
|||||||
import android.nfc.NfcAdapter;
|
import android.nfc.NfcAdapter;
|
||||||
import android.nfc.Tag;
|
import android.nfc.Tag;
|
||||||
import android.nfc.tech.Ndef;
|
import android.nfc.tech.Ndef;
|
||||||
|
import android.os.Binder;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import cc.winboll.studio.autonfc.MainActivity;
|
import cc.winboll.studio.autonfc.MainActivity;
|
||||||
import cc.winboll.studio.autonfc.R;
|
import cc.winboll.studio.autonfc.R;
|
||||||
import cc.winboll.studio.libappbase.LogUtils;
|
import cc.winboll.studio.libappbase.LogUtils;
|
||||||
|
import cc.winboll.studio.libappbase.ToastUtils;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 源码说明与描述:
|
* 源码说明与描述:
|
||||||
@@ -33,6 +36,15 @@ public class AutoNFCService extends Service {
|
|||||||
private static final int NOTIFICATION_ID = 0x1001;
|
private static final int NOTIFICATION_ID = 0x1001;
|
||||||
private static final String CHANNEL_ID = "NFC_SERVICE_CHANNEL";
|
private static final String CHANNEL_ID = "NFC_SERVICE_CHANNEL";
|
||||||
|
|
||||||
|
// Binder
|
||||||
|
private final IBinder mBinder = new LocalBinder();
|
||||||
|
|
||||||
|
public class LocalBinder extends Binder {
|
||||||
|
public AutoNFCService getService() {
|
||||||
|
return AutoNFCService.this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
@@ -41,26 +53,64 @@ public class AutoNFCService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public IBinder onBind(Intent intent) {
|
||||||
LogUtils.d(TAG, "onStartCommand() -> 服务运行");
|
LogUtils.d(TAG, "onBind: 服务被绑定");
|
||||||
|
return mBinder;
|
||||||
if (intent != null) {
|
|
||||||
handleNfcIntent(intent);
|
|
||||||
}
|
|
||||||
return START_STICKY;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理 MainActivity 转发的 NFC 数据
|
@Override
|
||||||
private void handleNfcIntent(Intent intent) {
|
public boolean onUnbind(Intent intent) {
|
||||||
String action = intent.getAction();
|
LogUtils.d(TAG, "onUnbind: 服务解绑,停止前台服务");
|
||||||
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)
|
stopForeground(true);
|
||||||
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
|
stopSelf();
|
||||||
|| NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
|
return super.onUnbind(intent);
|
||||||
|
}
|
||||||
|
|
||||||
LogUtils.d(TAG, "handleNfcIntent() -> 收到 NFC 卡片");
|
// 对外暴露给 Activity 调用
|
||||||
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
|
public void handleNfcIntent(Intent intent) {
|
||||||
parseNdefData(tag);
|
ToastUtils.show(TAG + " : handleNfcIntent");
|
||||||
|
LogUtils.d(TAG, "handleNfcIntent() start");
|
||||||
|
|
||||||
|
if (intent == null) {
|
||||||
|
LogUtils.e(TAG, "handleNfcIntent() intent is null");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String action = intent.getAction();
|
||||||
|
LogUtils.d(TAG, "handleNfcIntent() action = " + action);
|
||||||
|
|
||||||
|
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)
|
||||||
|
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
|
||||||
|
|| NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
|
||||||
|
|
||||||
|
LogUtils.d(TAG, "handleNfcIntent() -> 匹配 NFC 动作,开始处理");
|
||||||
|
|
||||||
|
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
|
||||||
|
if (tag == null) {
|
||||||
|
LogUtils.e(TAG, "handleNfcIntent() tag is null");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
LogUtils.d(TAG, "handleNfcIntent() tag id = " + bytesToHexString(tag.getId()));
|
||||||
|
LogUtils.d(TAG, "handleNfcIntent() tag tech list = " + Arrays.toString(tag.getTechList()));
|
||||||
|
|
||||||
|
parseNdefData(tag);
|
||||||
|
LogUtils.d(TAG, "handleNfcIntent() parseNdefData() called");
|
||||||
|
} else {
|
||||||
|
LogUtils.w(TAG, "handleNfcIntent() action not match: " + action);
|
||||||
|
}
|
||||||
|
|
||||||
|
LogUtils.d(TAG, "handleNfcIntent() end");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 辅助工具:byte[] 转十六进制字符串,用于打印 Tag ID
|
||||||
|
private String bytesToHexString(byte[] bytes) {
|
||||||
|
if (bytes == null || bytes.length == 0) return "";
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for (byte b : bytes) {
|
||||||
|
sb.append(String.format("%02X", b));
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void parseNdefData(Tag tag) {
|
private void parseNdefData(Tag tag) {
|
||||||
@@ -106,9 +156,9 @@ public class AutoNFCService extends Service {
|
|||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
NotificationChannel channel = new NotificationChannel(
|
NotificationChannel channel = new NotificationChannel(
|
||||||
CHANNEL_ID,
|
CHANNEL_ID,
|
||||||
"NFC 后台服务",
|
"NFC 后台服务",
|
||||||
NotificationManager.IMPORTANCE_LOW
|
NotificationManager.IMPORTANCE_LOW
|
||||||
);
|
);
|
||||||
channel.setSound(null, null);
|
channel.setSound(null, null);
|
||||||
channel.setVibrationPattern(null);
|
channel.setVibrationPattern(null);
|
||||||
@@ -119,12 +169,12 @@ public class AutoNFCService extends Service {
|
|||||||
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
|
|
||||||
return new Notification.Builder(this)
|
return new Notification.Builder(this)
|
||||||
.setChannelId(CHANNEL_ID)
|
.setChannelId(CHANNEL_ID)
|
||||||
.setSmallIcon(R.mipmap.ic_launcher)
|
.setSmallIcon(R.mipmap.ic_launcher)
|
||||||
.setContentTitle("NFC 服务运行中")
|
.setContentTitle("NFC 服务运行中")
|
||||||
.setContentText("正在监听卡片")
|
.setContentText("正在监听卡片")
|
||||||
.setContentIntent(pi)
|
.setContentIntent(pi)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -132,10 +182,5 @@ public class AutoNFCService extends Service {
|
|||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
LogUtils.d(TAG, "onDestroy() -> 服务已停止");
|
LogUtils.d(TAG, "onDestroy() -> 服务已停止");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public IBinder onBind(Intent intent) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,38 +3,30 @@ package cc.winboll.studio.autonfc.nfc;
|
|||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.nfc.NdefMessage;
|
|
||||||
import android.nfc.NdefRecord;
|
|
||||||
import android.nfc.NfcAdapter;
|
import android.nfc.NfcAdapter;
|
||||||
import android.nfc.Tag;
|
import android.nfc.Tag;
|
||||||
import android.nfc.tech.Ndef;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.view.View;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
import android.view.View;
|
|
||||||
import cc.winboll.studio.autonfc.R;
|
import cc.winboll.studio.autonfc.R;
|
||||||
|
import cc.winboll.studio.autonfc.models.NfcTermuxCmd;
|
||||||
import cc.winboll.studio.libappbase.LogUtils;
|
import cc.winboll.studio.libappbase.LogUtils;
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
/*
|
|
||||||
* 源码说明与描述:
|
|
||||||
* NFC 操作界面(自主读卡 + 显示内容与时间 + 写入数据)
|
|
||||||
* 不再接收 AutoNFCService 消息
|
|
||||||
*
|
|
||||||
* 作者:豆包&ZhanGSKen<zhangsken@qq.com>
|
|
||||||
* 创建时间:2026-03-16 16:10:00
|
|
||||||
* 最后编辑时间:2026-03-17 10:45:00
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class NFCInterfaceActivity extends Activity {
|
public class NFCInterfaceActivity extends Activity {
|
||||||
|
|
||||||
private static final String TAG = "NFCInterfaceActivity";
|
public static final String TAG = "NFCInterfaceActivity";
|
||||||
|
|
||||||
|
private EditText et_script;
|
||||||
|
private EditText et_args;
|
||||||
|
private EditText et_workDir;
|
||||||
|
private EditText et_background;
|
||||||
|
private EditText et_resultDir;
|
||||||
|
|
||||||
private EditText etInput;
|
|
||||||
private TextView tvResult;
|
private TextView tvResult;
|
||||||
private TextView tvStatus;
|
private TextView tvStatus;
|
||||||
|
|
||||||
@@ -45,22 +37,23 @@ public class NFCInterfaceActivity extends Activity {
|
|||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
LogUtils.d(TAG, "onCreate()");
|
|
||||||
setContentView(R.layout.activity_nfc_interface);
|
setContentView(R.layout.activity_nfc_interface);
|
||||||
|
|
||||||
initView();
|
initView();
|
||||||
initNfc();
|
initNfc();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initView() {
|
private void initView() {
|
||||||
LogUtils.d(TAG, "initView()");
|
et_script = findViewById(R.id.et_script);
|
||||||
etInput = findViewById(R.id.et_input);
|
et_args = findViewById(R.id.et_args);
|
||||||
|
et_workDir = findViewById(R.id.et_workDir);
|
||||||
|
et_background = findViewById(R.id.et_background);
|
||||||
|
et_resultDir = findViewById(R.id.et_resultDir);
|
||||||
|
|
||||||
tvResult = findViewById(R.id.tv_result);
|
tvResult = findViewById(R.id.tv_result);
|
||||||
tvStatus = findViewById(R.id.tv_status);
|
tvStatus = findViewById(R.id.tv_status);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initNfc() {
|
private void initNfc() {
|
||||||
LogUtils.d(TAG, "initNfc()");
|
|
||||||
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
|
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
|
||||||
|
|
||||||
if (mNfcAdapter == null) {
|
if (mNfcAdapter == null) {
|
||||||
@@ -68,15 +61,12 @@ public class NFCInterfaceActivity extends Activity {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!mNfcAdapter.isEnabled()) {
|
if (!mNfcAdapter.isEnabled()) {
|
||||||
tvStatus.setText("请在设置中开启NFC");
|
tvStatus.setText("请开启NFC");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
mNfcPendingIntent = PendingIntent.getActivity(
|
Intent nfcIntent = new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
||||||
this, 0,
|
mNfcPendingIntent = PendingIntent.getActivity(this, 0, nfcIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
|
|
||||||
PendingIntent.FLAG_UPDATE_CURRENT
|
|
||||||
);
|
|
||||||
|
|
||||||
tvStatus.setText("NFC已启动,等待卡片靠近");
|
tvStatus.setText("NFC已启动,等待卡片靠近");
|
||||||
}
|
}
|
||||||
@@ -84,167 +74,157 @@ public class NFCInterfaceActivity extends Activity {
|
|||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
LogUtils.d(TAG, "onResume() -> 启用前台读卡");
|
|
||||||
enableForegroundDispatch();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPause() {
|
|
||||||
super.onPause();
|
|
||||||
LogUtils.d(TAG, "onPause() -> 停止前台读卡");
|
|
||||||
disableForegroundDispatch();
|
|
||||||
mCurrentTag = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void enableForegroundDispatch() {
|
|
||||||
if (mNfcAdapter != null && mNfcAdapter.isEnabled()) {
|
if (mNfcAdapter != null && mNfcAdapter.isEnabled()) {
|
||||||
mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent, null, null);
|
mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent, null, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void disableForegroundDispatch() {
|
@Override
|
||||||
|
protected void onPause() {
|
||||||
|
super.onPause();
|
||||||
if (mNfcAdapter != null) {
|
if (mNfcAdapter != null) {
|
||||||
mNfcAdapter.disableForegroundDispatch(this);
|
mNfcAdapter.disableForegroundDispatch(this);
|
||||||
}
|
}
|
||||||
|
mCurrentTag = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onNewIntent(Intent intent) {
|
protected void onNewIntent(Intent intent) {
|
||||||
super.onNewIntent(intent);
|
super.onNewIntent(intent);
|
||||||
LogUtils.d(TAG, "onNewIntent() -> 卡片已靠近");
|
|
||||||
|
|
||||||
mCurrentTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
|
mCurrentTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
|
||||||
tvStatus.setText("卡片已连接");
|
if (mCurrentTag == null) return;
|
||||||
|
|
||||||
// 自动读取旧数据
|
tvStatus.setText("卡片已连接,解析中...");
|
||||||
readOldNfcData(mCurrentTag);
|
readNfc();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// -------------------------------------------------------------------------
|
||||||
* 读取卡片原有数据并显示(带时间)
|
// 读取 NFC(完全委托给工具类)
|
||||||
*/
|
// -------------------------------------------------------------------------
|
||||||
private void readOldNfcData(Tag tag) {
|
private void readNfc() {
|
||||||
LogUtils.d(TAG, "readOldNfcData(tag=" + tag + ")");
|
|
||||||
|
|
||||||
if (tag == null) {
|
|
||||||
showToast("标签为空");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ndef ndef = Ndef.get(tag);
|
|
||||||
if (ndef == null) {
|
|
||||||
showToast("不支持NDEF");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
ndef.connect();
|
NfcTermuxCmd cmd = NfcUtils.readTag(mCurrentTag);
|
||||||
NdefMessage msg = ndef.getNdefMessage();
|
if (cmd == null) {
|
||||||
if (msg == null) {
|
tvStatus.setText("读取成功:标签为空");
|
||||||
tvResult.setText("无旧数据");
|
tvResult.setText("");
|
||||||
ndef.close();
|
// 清空窗体
|
||||||
|
clearUiFields();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
NdefRecord[] records = msg.getRecords();
|
// 核心改动:读取成功后,同时更新详情显示 和 窗体输入框
|
||||||
if (records != null && records.length > 0) {
|
updateUiWithCmd(cmd);
|
||||||
byte[] payload = records[0].getPayload();
|
|
||||||
int langLen = payload[0] & 0x3F;
|
|
||||||
int start = 1 + langLen;
|
|
||||||
|
|
||||||
String content = new String(
|
|
||||||
payload, start, payload.length - start,
|
|
||||||
Charset.forName("UTF-8")
|
|
||||||
);
|
|
||||||
|
|
||||||
// 附加时间
|
|
||||||
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(new Date());
|
|
||||||
String show = "读取时间:" + time + "\n\n内容:\n" + content;
|
|
||||||
tvResult.setText(show);
|
|
||||||
|
|
||||||
showToast("读取成功");
|
|
||||||
}
|
|
||||||
ndef.close();
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LogUtils.e(TAG, "读取失败", e);
|
LogUtils.e(TAG, "readNfc 失败", e);
|
||||||
tvResult.setText("读取失败:" + e.getMessage());
|
tvStatus.setText("读取失败:" + e.getMessage());
|
||||||
showToast("读取失败");
|
// 出错时清空窗体
|
||||||
|
clearUiFields();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// -------------------------------------------------------------------------
|
||||||
* 写入按钮点击
|
// 新增:根据读取到的 Cmd 填充 UI(详情 + 窗体)
|
||||||
*/
|
// -------------------------------------------------------------------------
|
||||||
public void onWriteClick(View view) {
|
private void updateUiWithCmd(NfcTermuxCmd cmd) {
|
||||||
LogUtils.d(TAG, "onWriteClick()");
|
if (cmd == null) return;
|
||||||
String text = etInput.getText().toString().trim();
|
|
||||||
|
|
||||||
if (text.isEmpty()) {
|
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(new Date());
|
||||||
showToast("请输入内容");
|
String show = "【读取时间】 " + time + "\n\n"
|
||||||
return;
|
+ "【解析结果】\n"
|
||||||
}
|
+ "script: " + cmd.getScript() + "\n"
|
||||||
|
+ "args: " + (cmd.getArgs() != null ? String.join(", ", cmd.getArgs()) : "[]") + "\n"
|
||||||
|
+ "workDir: " + cmd.getWorkDir() + "\n"
|
||||||
|
+ "background: " + cmd.isBackground() + "\n"
|
||||||
|
+ "resultDir: " + cmd.getResultDir();
|
||||||
|
|
||||||
|
tvResult.setText(show);
|
||||||
|
tvStatus.setText("读取成功!");
|
||||||
|
|
||||||
|
// 👇 关键逻辑:自动填入窗体(每次读取后都会覆盖输入框)
|
||||||
|
et_script.setText(cmd.getScript() != null ? cmd.getScript() : "");
|
||||||
|
et_args.setText(cmd.getArgs() != null ? String.join(",", cmd.getArgs()) : "");
|
||||||
|
et_workDir.setText(cmd.getWorkDir() != null ? cmd.getWorkDir() : "");
|
||||||
|
et_background.setText(String.valueOf(cmd.isBackground()));
|
||||||
|
et_resultDir.setText(cmd.getResultDir() != null ? cmd.getResultDir() : "");
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 辅助:清空所有输入框
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
private void clearUiFields() {
|
||||||
|
et_script.setText("");
|
||||||
|
et_args.setText("");
|
||||||
|
et_workDir.setText("");
|
||||||
|
et_background.setText("");
|
||||||
|
et_resultDir.setText("");
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 写入按钮(委托给工具类)
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
public void onWriteClick(View view) {
|
||||||
if (mCurrentTag == null) {
|
if (mCurrentTag == null) {
|
||||||
showToast("请先靠近卡片");
|
showToast("请先靠近卡片");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
writeNfc(mCurrentTag, text);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 写入NDEF
|
|
||||||
*/
|
|
||||||
private void writeNfc(Tag tag, String text) {
|
|
||||||
LogUtils.d(TAG, "writeNfc(text=" + text + ")");
|
|
||||||
tvStatus.setText("正在写入...");
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Ndef ndef = Ndef.get(tag);
|
NfcTermuxCmd cmd = buildCmdFromUI();
|
||||||
if (ndef == null) {
|
NfcUtils.writeTag(mCurrentTag, cmd);
|
||||||
showToast("不支持NDEF");
|
|
||||||
tvStatus.setText("不支持NDEF");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ndef.connect();
|
|
||||||
NdefRecord record = createTextRecord(text, true);
|
|
||||||
NdefMessage msg = new NdefMessage(new NdefRecord[]{record});
|
|
||||||
ndef.writeNdefMessage(msg);
|
|
||||||
ndef.close();
|
|
||||||
|
|
||||||
tvStatus.setText("写入成功!");
|
tvStatus.setText("写入成功!");
|
||||||
showToast("写入成功");
|
showToast("写入成功");
|
||||||
|
readNfc(); // 写入后重读,此时会自动填入窗体
|
||||||
|
|
||||||
// 写入后重新读取
|
|
||||||
readOldNfcData(tag);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LogUtils.e(TAG, "写入失败", e);
|
LogUtils.e(TAG, "写入失败", e);
|
||||||
tvStatus.setText("写入失败");
|
tvStatus.setText("写入失败:" + e.getMessage());
|
||||||
showToast("写入失败");
|
showToast("写入失败");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// -------------------------------------------------------------------------
|
||||||
* 创建文本记录
|
// 填充调试数据
|
||||||
*/
|
// -------------------------------------------------------------------------
|
||||||
private NdefRecord createTextRecord(String text, boolean isUtf8) {
|
public void onFillTestDataClick(View view) {
|
||||||
byte[] langBytes = "en".getBytes(Charset.forName("US-ASCII"));
|
String testJson = "{\"script\":\"BuildWinBoLLProject.sh\",\"args\":[\"DebugTemp\"],\"workDir\":null,\"background\":true,\"resultDir\":null}";
|
||||||
byte[] textBytes = text.getBytes(Charset.forName(isUtf8 ? "UTF-8" : "UTF-16"));
|
try {
|
||||||
int status = isUtf8 ? 0 : 0x80;
|
NfcTermuxCmd cmd = NfcUtils.jsonToCmd(testJson);
|
||||||
status |= langBytes.length & 0x3F;
|
et_script.setText(cmd.getScript());
|
||||||
|
et_args.setText(cmd.getArgs() != null ? String.join(",", cmd.getArgs()) : "");
|
||||||
|
et_workDir.setText(cmd.getWorkDir() != null ? cmd.getWorkDir() : "");
|
||||||
|
et_background.setText(String.valueOf(cmd.isBackground()));
|
||||||
|
et_resultDir.setText(cmd.getResultDir() != null ? cmd.getResultDir() : "");
|
||||||
|
|
||||||
byte[] data = new byte[1 + langBytes.length + textBytes.length];
|
showToast("调试数据已填入");
|
||||||
data[0] = (byte) status;
|
} catch (Exception e) {
|
||||||
System.arraycopy(langBytes, 0, data, 1, langBytes.length);
|
showToast("解析失败");
|
||||||
System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
|
// -------------------------------------------------------------------------
|
||||||
NdefRecord.RTD_TEXT, new byte[0], data);
|
// 从 UI 构建 NfcTermuxCmd
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
private NfcTermuxCmd buildCmdFromUI() {
|
||||||
|
String script = et_script.getText().toString().trim();
|
||||||
|
String argsStr = et_args.getText().toString().trim();
|
||||||
|
String workDir = et_workDir.getText().toString().trim();
|
||||||
|
String bgStr = et_background.getText().toString().trim();
|
||||||
|
String resultDir = et_resultDir.getText().toString().trim();
|
||||||
|
|
||||||
|
NfcTermuxCmd cmd = new NfcTermuxCmd();
|
||||||
|
cmd.setScript(script);
|
||||||
|
cmd.setArgs(argsStr.isEmpty() ? new String[0] : argsStr.split(","));
|
||||||
|
cmd.setWorkDir(workDir.isEmpty() ? null : workDir);
|
||||||
|
cmd.setBackground("true".equalsIgnoreCase(bgStr));
|
||||||
|
cmd.setResultDir(resultDir.isEmpty() ? null : resultDir);
|
||||||
|
|
||||||
|
return cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showToast(String msg) {
|
private void showToast(String msg) {
|
||||||
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,97 +0,0 @@
|
|||||||
package cc.winboll.studio.autonfc.nfc;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* 源码说明与描述:
|
|
||||||
* NFC 透明代理 Activity,用于在后台捕获 NFC 卡片事件并转发给 AutoNFCService
|
|
||||||
* 界面为 1 像素透明,不影响用户操作
|
|
||||||
*
|
|
||||||
* 作者:豆包&ZhanGSKen<zhangsken@qq.com>
|
|
||||||
* 创建时间:2026-03-17 14:00:00
|
|
||||||
* 最后编辑时间:2026-03-17 18:30:00
|
|
||||||
*/
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.app.PendingIntent;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.nfc.NfcAdapter;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.view.Window;
|
|
||||||
import android.view.WindowManager;
|
|
||||||
import cc.winboll.studio.libappbase.LogUtils;
|
|
||||||
|
|
||||||
public class NfcProxyActivity extends Activity {
|
|
||||||
|
|
||||||
private static final String TAG = "NfcProxyActivity";
|
|
||||||
|
|
||||||
private NfcAdapter mNfcAdapter;
|
|
||||||
private PendingIntent mPendingIntent;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
LogUtils.d(TAG, "onCreate() -> 启动 NFC 透明代理");
|
|
||||||
|
|
||||||
// 设置为 1 像素透明窗口,用户不可见
|
|
||||||
Window window = getWindow();
|
|
||||||
window.setLayout(1, 1);
|
|
||||||
window.setBackgroundDrawableResource(android.R.color.transparent);
|
|
||||||
window.setFlags(
|
|
||||||
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
|
|
||||||
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
|
|
||||||
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
|
|
||||||
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|
|
||||||
);
|
|
||||||
|
|
||||||
// 初始化 NFC
|
|
||||||
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
|
|
||||||
Intent intent = new Intent(this, getClass())
|
|
||||||
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
|
||||||
mPendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onResume() {
|
|
||||||
super.onResume();
|
|
||||||
LogUtils.d(TAG, "onResume() -> 启用前台分发");
|
|
||||||
|
|
||||||
// 强制获取焦点,确保 enableForegroundDispatch 生效
|
|
||||||
getWindow().getDecorView().requestFocus();
|
|
||||||
getWindow().setFlags(
|
|
||||||
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
|
|
||||||
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
|
|
||||||
0 // 临时允许获取焦点
|
|
||||||
);
|
|
||||||
|
|
||||||
if (mNfcAdapter != null) {
|
|
||||||
mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 恢复为不可聚焦,避免拦截用户操作
|
|
||||||
getWindow().setFlags(
|
|
||||||
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
|
|
||||||
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
|
|
||||||
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
|
|
||||||
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPause() {
|
|
||||||
super.onPause();
|
|
||||||
LogUtils.d(TAG, "onPause() -> 关闭前台分发");
|
|
||||||
if (mNfcAdapter != null) {
|
|
||||||
mNfcAdapter.disableForegroundDispatch(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onNewIntent(Intent intent) {
|
|
||||||
super.onNewIntent(intent);
|
|
||||||
LogUtils.d(TAG, "onNewIntent() -> 捕获到 NFC 卡片事件");
|
|
||||||
|
|
||||||
// 转发给 AutoNFCService 处理
|
|
||||||
Intent serviceIntent = new Intent(this, AutoNFCService.class);
|
|
||||||
serviceIntent.putExtras(intent);
|
|
||||||
startService(serviceIntent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
package cc.winboll.studio.autonfc.nfc;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
|
||||||
|
* @Date 2026/03/16 14:26
|
||||||
|
*/
|
||||||
|
import android.nfc.NdefMessage;
|
||||||
|
import android.nfc.NdefRecord;
|
||||||
|
import android.nfc.Tag;
|
||||||
|
import android.nfc.tech.Ndef;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.JsonSyntaxException;
|
||||||
|
import cc.winboll.studio.autonfc.models.NfcTermuxCmd;
|
||||||
|
import cc.winboll.studio.libappbase.LogUtils;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
public class NfcUtils {
|
||||||
|
|
||||||
|
public static final String TAG = "NfcUtils";
|
||||||
|
private static Gson sGson = new Gson();
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 读取 NFC 标签并解析为 NfcTermuxCmd
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
public static NfcTermuxCmd readTag(Tag tag) throws Exception {
|
||||||
|
if (tag == null) {
|
||||||
|
LogUtils.e(TAG, "readTag: tag is null");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ndef ndef = Ndef.get(tag);
|
||||||
|
if (ndef == null) {
|
||||||
|
LogUtils.e(TAG, "readTag: 不支持 NDEF");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
ndef.connect();
|
||||||
|
NdefMessage msg = ndef.getNdefMessage();
|
||||||
|
if (msg == null) return null;
|
||||||
|
|
||||||
|
NdefRecord[] records = msg.getRecords();
|
||||||
|
if (records == null || records.length == 0) return null;
|
||||||
|
|
||||||
|
byte[] payload = records[0].getPayload();
|
||||||
|
int status = payload[0] & 0xFF;
|
||||||
|
int langLen = status & 0x3F;
|
||||||
|
int start = 1 + langLen;
|
||||||
|
|
||||||
|
if (start >= payload.length) return null;
|
||||||
|
|
||||||
|
String json = new String(payload, start, payload.length - start, Charset.forName("UTF-8"));
|
||||||
|
LogUtils.d(TAG, "readTag: 提取 JSON -> " + json);
|
||||||
|
|
||||||
|
return sGson.fromJson(json, NfcTermuxCmd.class);
|
||||||
|
} finally {
|
||||||
|
if (ndef != null && ndef.isConnected()) {
|
||||||
|
ndef.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 写入 NfcTermuxCmd 到 NFC 标签
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
public static void writeTag(Tag tag, NfcTermuxCmd cmd) throws Exception {
|
||||||
|
if (tag == null) throw new Exception("tag is null");
|
||||||
|
|
||||||
|
String json = sGson.toJson(cmd);
|
||||||
|
writeJson(tag, json);
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 写入原始 JSON 字符串到 NFC
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
public static void writeJson(Tag tag, String json) throws Exception {
|
||||||
|
if (tag == null) throw new Exception("tag is null");
|
||||||
|
|
||||||
|
Ndef ndef = Ndef.get(tag);
|
||||||
|
if (ndef == null) throw new Exception("标签不支持 NDEF");
|
||||||
|
|
||||||
|
try {
|
||||||
|
ndef.connect();
|
||||||
|
int maxSize = ndef.getMaxSize();
|
||||||
|
int realSize = json.getBytes(Charset.forName("UTF-8")).length;
|
||||||
|
|
||||||
|
if (realSize > maxSize) {
|
||||||
|
throw new Exception("数据过大 (" + realSize + ") > 容量 (" + maxSize + ")");
|
||||||
|
}
|
||||||
|
|
||||||
|
NdefRecord record = createTextRecord(json, true);
|
||||||
|
NdefMessage msg = new NdefMessage(new NdefRecord[]{record});
|
||||||
|
|
||||||
|
ndef.writeNdefMessage(msg);
|
||||||
|
LogUtils.d(TAG, "writeJson: 写入成功");
|
||||||
|
} finally {
|
||||||
|
if (ndef != null && ndef.isConnected()) {
|
||||||
|
ndef.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 创建 NFC 文本记录
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
public static NdefRecord createTextRecord(String text, boolean isUtf8) {
|
||||||
|
byte[] langBytes = "en".getBytes(Charset.forName("US-ASCII"));
|
||||||
|
byte[] textBytes = text.getBytes(Charset.forName(isUtf8 ? "UTF-8" : "UTF-16"));
|
||||||
|
|
||||||
|
int status = isUtf8 ? 0 : 0x80;
|
||||||
|
status |= langBytes.length & 0x3F;
|
||||||
|
|
||||||
|
byte[] data = new byte[1 + langBytes.length + textBytes.length];
|
||||||
|
data[0] = (byte) status;
|
||||||
|
System.arraycopy(langBytes, 0, data, 1, langBytes.length);
|
||||||
|
System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length);
|
||||||
|
|
||||||
|
return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 辅助:JSON -> NfcTermuxCmd
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
public static NfcTermuxCmd jsonToCmd(String json) throws JsonSyntaxException {
|
||||||
|
return sGson.fromJson(json, NfcTermuxCmd.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 辅助:NfcTermuxCmd -> JSON
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
public static String cmdToJson(NfcTermuxCmd cmd) {
|
||||||
|
return sGson.toJson(cmd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,46 +1,104 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<LinearLayout
|
<ScrollView
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent">
|
||||||
android:orientation="vertical"
|
|
||||||
android:padding="20dp">
|
|
||||||
|
|
||||||
<!-- 状态显示 -->
|
<LinearLayout
|
||||||
<TextView
|
android:layout_width="match_parent"
|
||||||
android:id="@+id/tv_status"
|
android:layout_height="wrap_content"
|
||||||
android:layout_width="match_parent"
|
android:orientation="vertical"
|
||||||
android:layout_height="wrap_content"
|
android:padding="16dp">
|
||||||
android:text="等待NFC卡片..."
|
|
||||||
android:textSize="16sp"
|
|
||||||
android:textColor="#FF666666"/>
|
|
||||||
|
|
||||||
<EditText
|
<TextView
|
||||||
android:id="@+id/et_input"
|
android:layout_width="wrap_content"
|
||||||
android:layout_width="match_parent"
|
android:layout_height="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:text="脚本名称 script:"/>
|
||||||
android:hint="输入要写入的NFC文本"
|
|
||||||
android:minHeight="50dp"
|
|
||||||
android:layout_marginTop="10dp"/>
|
|
||||||
|
|
||||||
<Button
|
<EditText
|
||||||
android:layout_width="match_parent"
|
android:id="@+id/et_script"
|
||||||
android:layout_height="wrap_content"
|
android:layout_width="match_parent"
|
||||||
android:text="写入NFC"
|
android:layout_height="wrap_content"
|
||||||
android:onClick="onWriteClick"/>
|
android:hint="如 auth.sh"/>
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:layout_width="match_parent"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginTop="20dp"
|
android:layout_marginTop="8dp"
|
||||||
android:text="读取结果"/>
|
android:text="参数 args (逗号分隔):"/>
|
||||||
|
|
||||||
<TextView
|
<EditText
|
||||||
android:id="@+id/tv_result"
|
android:id="@+id/et_args"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="120dp"
|
android:layout_height="wrap_content"
|
||||||
android:background="#f5f5f5"
|
android:hint="user1,pass123"/>
|
||||||
android:gravity="center"/>
|
|
||||||
|
|
||||||
</LinearLayout>
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="8dp"
|
||||||
|
android:text="工作目录 workDir:"/>
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/et_workDir"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:hint="/data/data/com.termux/files/home"/>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="8dp"
|
||||||
|
android:text="后台执行 background (true/false):"/>
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/et_background"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="true"/>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="8dp"
|
||||||
|
android:text="结果目录 resultDir:"/>
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/et_resultDir"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:hint="/data/data/com.termux/files/log"/>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="16dp"
|
||||||
|
android:onClick="onFillTestDataClick"
|
||||||
|
android:text="填入调试数据 (BuildWinBoLLProject.sh)"/>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="8dp"
|
||||||
|
android:onClick="onWriteClick"
|
||||||
|
android:text="写入 NFC (NfcTermuxCmd JSON)"/>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tv_status"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="16dp"
|
||||||
|
android:text="状态"/>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tv_result"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="16dp"
|
||||||
|
android:scrollbars="vertical"
|
||||||
|
android:textSize="14sp"/>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
</ScrollView>
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
<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
|
<item
|
||||||
android:id="@+id/menu_log"
|
android:id="@+id/menu_log"
|
||||||
android:title="启动日志"/>
|
android:title="启动日志"/>
|
||||||
|
|||||||
Reference in New Issue
Block a user