增加 读取user

master
wanghao 2 weeks ago
parent a8a3b5da1a
commit 0f35f250b4

@ -19,7 +19,8 @@
tools:targetApi="31"> tools:targetApi="31">
<activity <activity
android:name=".WriteUserActivity" android:name=".WriteUserActivity"
android:exported="true" > android:exported="true"
android:label="读写user">
<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" />
@ -43,11 +44,11 @@
<activity <activity
android:name=".HomeActivity" android:name=".HomeActivity"
android:exported="true"> android:exported="true">
<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=".HomeActivity2" android:name=".HomeActivity2"

@ -142,5 +142,6 @@ public class MainActivity extends AppCompatActivity {
// uhfrManager.asyncStopReading(); // uhfrManager.asyncStopReading();
uhfrManager.close(); uhfrManager.close();
uhfrManager = null; uhfrManager = null;
} }
} }

@ -3,6 +3,7 @@ package com.example.writeepc;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.Toast;
import androidx.databinding.DataBindingUtil; import androidx.databinding.DataBindingUtil;
@ -17,9 +18,10 @@ public class WriteUserActivity extends BaseActivity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_write_user); binding = DataBindingUtil.setContentView(this, R.layout.activity_write_user);
infoData=new InfoData(); infoData = new InfoData();
binding.setData(infoData); binding.setData(infoData);
initUHFRManager(5); initUHFRManager(5);
initReceiver();
} }
@Override @Override
@ -29,7 +31,17 @@ public class WriteUserActivity extends BaseActivity {
} }
public void writeUser(View view) { public void writeUser(View view) {
String writeData = infoData.getWriteUser();
if (writeData == null || writeData.isEmpty()) {
Toast.makeText(this, "请输入要写入的数据", Toast.LENGTH_SHORT).show();
return;
}
if (writeData.length() % 4 != 0) {
Toast.makeText(this, "请输入4字数据", Toast.LENGTH_SHORT).show();
return;
}
Log.e("TAG", "writeUser:" + ASCIIUtil.str2Hex(writeData));
writeUserData(ASCIIUtil.str2Hex(writeData));
} }
public void readUser(View view) { public void readUser(View view) {
@ -43,12 +55,10 @@ public class WriteUserActivity extends BaseActivity {
} }
@Override
public void receiverSan() {
this.readUser(null);
}
@Override @Override
protected void onDestroy() { protected void onDestroy() {
@ -57,6 +67,9 @@ public class WriteUserActivity extends BaseActivity {
uhfrManager.close(); uhfrManager.close();
uhfrManager = null; uhfrManager = null;
} }
if (myReceiver != null) {
unregisterReceiver(myReceiver);
}
} }
} }

@ -1,6 +1,10 @@
package com.example.writeepc.base; package com.example.writeepc.base;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
@ -27,15 +31,22 @@ public class BaseActivity extends AppCompatActivity {
public TipsDialog tipsDialog; public TipsDialog tipsDialog;
public MediaType mediaType = MediaType.parse("text/xml; charset=utf-8"); public MediaType mediaType = MediaType.parse("text/xml; charset=utf-8");
public Gson gson; public Gson gson;
public MyReceiver myReceiver;
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
tipsDialog = new TipsDialog(this); // tipsDialog = new TipsDialog(this);
gson = new Gson(); // gson = new Gson();
} }
public void initReceiver() {
myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.rfid.FUN_KEY");
registerReceiver(myReceiver, intentFilter);
}
public void initDialog() { public void initDialog() {
dialog = new ProgressDialog(this); dialog = new ProgressDialog(this);
@ -70,7 +81,6 @@ public class BaseActivity extends AppCompatActivity {
} }
// 1- 读取TID // 1- 读取TID
public void readTid() { public void readTid() {
byte[] rdata = new byte[12]; byte[] rdata = new byte[12];
@ -118,11 +128,38 @@ public class BaseActivity extends AppCompatActivity {
} }
public void writeUserData(String writeData) {
byte[] bytes = Tools.HexString2Bytes(writeData);
Reader.READER_ERR readerErr = uhfrManager.writeTagData((char) 3, 0,
bytes, bytes.length, accesspwd, timeOut);
Log.e("TAG", "写入状态:" + readerErr.name());
if (readerErr == Reader.READER_ERR.MT_OK_ERR) {
music.start();
Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "写入失败", Toast.LENGTH_SHORT).show();
}
}
protected void readTidReault(boolean readState, String result, byte[] rdata) { protected void readTidReault(boolean readState, String result, byte[] rdata) {
} }
public void receiverSan() {
}
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
var keydown = intent.getBooleanExtra("keydown", false);
if (!keydown) {
receiverSan();
}
}
}
// @Override // @Override
// protected void onDestroy() { // protected void onDestroy() {
// super.onDestroy(); // super.onDestroy();

Loading…
Cancel
Save