完成 东集设备开发
parent
de8c0011d1
commit
c763aa21ae
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,89 @@
|
||||
package com.example.jinyu_rfid;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.databinding.DataBindingUtil;
|
||||
|
||||
import com.example.jinyu_rfid.adapter.ResultAdapter;
|
||||
import com.example.jinyu_rfid.base.BaseActivity;
|
||||
import com.example.jinyu_rfid.been.ReadTyreNoResult;
|
||||
import com.example.jinyu_rfid.callback.DataReturnCall;
|
||||
import com.example.jinyu_rfid.databinding.ActivityAutoidReadBinding;
|
||||
import com.example.jinyu_rfid.databinding.ActivityAutoidReadBindingImpl;
|
||||
import com.example.jinyu_rfid.databinding.ActivityReadBinding;
|
||||
import com.example.jinyu_rfid.rfid.AutoID9UDevice;
|
||||
import com.example.jinyu_rfid.rfid.RFIDModel;
|
||||
import com.example.jinyu_rfid.uitls.ASCIIUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class AutoIDReadActivity extends BaseActivity implements DataReturnCall {
|
||||
private ResultAdapter adapter;
|
||||
private RFIDModel rfidModel;
|
||||
private String[] stringArray;
|
||||
private List<ReadTyreNoResult> list;
|
||||
private ActivityAutoidReadBinding binding;
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
binding = DataBindingUtil.setContentView(this, R.layout.activity_autoid_read);
|
||||
binding.setTitle(getString(R.string.go_read));
|
||||
adapter = new ResultAdapter(this);
|
||||
list = new ArrayList<>(11);
|
||||
adapter.setList(list);
|
||||
binding.setAdapter(adapter);
|
||||
// 名称
|
||||
stringArray = getResources().getStringArray(R.array.project_list);
|
||||
// rfidModel = new C5106Device(this,this);
|
||||
rfidModel = new AutoID9UDevice(this, this);
|
||||
|
||||
}
|
||||
|
||||
|
||||
// 读取user区
|
||||
public void readInfo(View view) {
|
||||
rfidModel.sanUser(80);
|
||||
}
|
||||
|
||||
public void readEPCInfo(View view) {
|
||||
rfidModel.sanEpc(6);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void readEpcCodeInfo(String info, boolean state, String stateInfo) {
|
||||
if (state) {
|
||||
binding.readTextEpc.setText(info);
|
||||
music.start();
|
||||
return;
|
||||
}
|
||||
binding.readTextEpc.setText(null);
|
||||
}
|
||||
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
@Override
|
||||
public void readUserInfo(String user, boolean state, String stateInfo) {
|
||||
list.clear();
|
||||
if (state) {
|
||||
binding.readText.setText(user);
|
||||
music.start();
|
||||
String info = ASCIIUtil.hex2Str(user);
|
||||
Log.e("TAG", "readUserInfo:" + user);
|
||||
var infos = info.split("~");
|
||||
for (int i = 0; i < 12; i++) {
|
||||
list.add(new ReadTyreNoResult(i, stringArray[i], infos[i]));
|
||||
}
|
||||
} else {
|
||||
binding.readText.setText(null);
|
||||
}
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package com.example.jinyu_rfid.broadcast;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.util.Log;
|
||||
|
||||
import com.example.jinyu_rfid.callback.DataReturnCall;
|
||||
|
||||
public class ScanERCodeReceiver extends BroadcastReceiver {
|
||||
private DataReturnCall call;
|
||||
|
||||
public ScanERCodeReceiver() {
|
||||
}
|
||||
|
||||
public ScanERCodeReceiver(DataReturnCall call) {
|
||||
this.call = call;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
byte[] data = intent.getByteArrayExtra("data");
|
||||
if (data != null) {
|
||||
String barcode = new String(data);
|
||||
Log.e("", "onReceive, data:" + barcode);
|
||||
call.readerCodeInfo(barcode);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,128 @@
|
||||
package com.example.jinyu_rfid.rfid;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.util.Log;
|
||||
|
||||
import com.example.jinyu_rfid.callback.DataReturnCall;
|
||||
import com.example.jinyu_rfid.uitls.ASCIIUtil;
|
||||
import com.seuic.uhf.EPC;
|
||||
import com.seuic.uhf.UHFService;
|
||||
|
||||
import cn.pda.serialport.Tools;
|
||||
|
||||
/**
|
||||
* @author wanghao
|
||||
* @date 2024/6/7 13:36
|
||||
*/
|
||||
public class AutoID9UDevice implements RFIDModel {
|
||||
|
||||
private DataReturnCall call;
|
||||
private UHFService uhfService;
|
||||
private byte[] password = AutoID9UUtil.getHexByteArray("00000000");
|
||||
private AutoIDdeviceScanERCodeReceiver scanERCodeReceiver;
|
||||
private Context context;
|
||||
|
||||
|
||||
@SuppressLint("UnspecifiedRegisterReceiverFlag")
|
||||
public AutoID9UDevice(DataReturnCall call, Context context) {
|
||||
this.call = call;
|
||||
this.context = context;
|
||||
uhfService = UHFService.getInstance();
|
||||
uhfService.setParameters(UHFService.PARAMETER_INVENTORY_SESSION,0);
|
||||
if (!uhfService.isopen()) {
|
||||
uhfService.open();
|
||||
}
|
||||
|
||||
Log.e("TAG", "上电:" + uhfService.isopen());
|
||||
Log.e("TAG", "功率:" + uhfService.getPower());
|
||||
scanERCodeReceiver = new AutoIDdeviceScanERCodeReceiver();
|
||||
context.registerReceiver(scanERCodeReceiver, new IntentFilter("com.android.server.scannerservice.broadcast"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sanEpc(int len) {
|
||||
EPC epc = new EPC();
|
||||
var result = uhfService.inventoryOnce(epc, 100);
|
||||
Log.e("TAG", "sanEpc:" + result);
|
||||
String info = null;
|
||||
if (result) {
|
||||
var id = epc.getId();
|
||||
Log.e("TAG", "sanEpc:" + id);
|
||||
var length = id.length();
|
||||
var i = len * 4;
|
||||
if (i < length) {
|
||||
id = id.substring(0, i);
|
||||
} else if (i > length) {
|
||||
var tag = i - length;
|
||||
for (int j = 0; j < tag; j++) {
|
||||
id += "0";
|
||||
}
|
||||
|
||||
}
|
||||
info = id;
|
||||
}
|
||||
call.readEpcCodeInfo(info, result, null);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean writeUser(String writeStr, String tagEpc) {
|
||||
|
||||
var bytes = AutoID9UUtil.getHexByteArray(writeStr);
|
||||
Log.e("TAG", "writeUser:" + tagEpc);
|
||||
var b = uhfService.writeTagData(AutoID9UUtil.getHexByteArray(tagEpc), password,
|
||||
3, 0, bytes.length, bytes);
|
||||
return b;
|
||||
}
|
||||
|
||||
String id = null;
|
||||
|
||||
@Override
|
||||
public void sanUser(int len) {
|
||||
var power = uhfService.getPower();
|
||||
Log.e("TAG", "设备功率:" + power);
|
||||
|
||||
String info = null;
|
||||
EPC epc=new EPC();
|
||||
var result = uhfService.inventoryOnce(epc, 100);
|
||||
Log.e("TAG", "epcid:" + epc.getId());
|
||||
if (result) {
|
||||
var i = len * 2;
|
||||
byte[] data = new byte[i];
|
||||
result = uhfService.readTagData(AutoID9UUtil.getHexByteArray(epc.getId()), password, 3, 0, i, data);
|
||||
Log.e("TAG", "按长度读取:" + result);
|
||||
if (result) {
|
||||
info = Tools.Bytes2HexString(data, data.length);
|
||||
}
|
||||
}
|
||||
call.readUserInfo(info,result,null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
uhfService.close();
|
||||
context.unregisterReceiver(scanERCodeReceiver);
|
||||
}
|
||||
//
|
||||
// @Override
|
||||
// public void loading() {
|
||||
// uhfService.close();
|
||||
// uhfService.open();
|
||||
// Log.e("TAG", "loading:" + uhfService.getPower());
|
||||
// }
|
||||
|
||||
class AutoIDdeviceScanERCodeReceiver extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String data = intent.getStringExtra("scannerdata");
|
||||
if (data != null) {
|
||||
call.readerCodeInfo(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,121 @@
|
||||
package com.example.jinyu_rfid.rfid;
|
||||
|
||||
public class AutoID9UUtil {
|
||||
|
||||
/**
|
||||
* Copy the array at the specified location
|
||||
*/
|
||||
public static void memcpy(byte[] bytesTo, byte[] bytesFrom, int len) {
|
||||
memcpy(bytesTo, 0, bytesFrom, 0, len);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void memcpy(byte[] bytesTo, int startIndexTo, byte[] bytesFrom, int startIndexFrom, int len) {
|
||||
while (len-- > 0) {
|
||||
bytesTo[startIndexTo++] = bytesFrom[startIndexFrom++];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the array from the start
|
||||
*/
|
||||
public static boolean memcmp(byte[] bytes1, byte[] bytes2, int len) {
|
||||
|
||||
if (len < 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int startIndex = 0;
|
||||
while (len-- > 0) {
|
||||
if (bytes1[startIndex] != bytes2[startIndex]) {
|
||||
return false;
|
||||
}
|
||||
startIndex++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* byte array to hexadecimal string
|
||||
*/
|
||||
public static String getHexString(byte[] b, int length) {
|
||||
return getHexString(b, length, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the specified splitter
|
||||
*/
|
||||
public static String getHexString(byte[] b, int length, String split) {
|
||||
StringBuilder hex = new StringBuilder("");
|
||||
String temp = null;
|
||||
for (int i = 0; i < length; i++) {
|
||||
temp = Integer.toHexString(b[i] & 0xFF);
|
||||
if (temp.length() == 1) {
|
||||
temp = '0' + temp;
|
||||
}
|
||||
hex.append(temp + split);
|
||||
}
|
||||
return hex.toString().trim().toUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* String to hexadecimal array
|
||||
*/
|
||||
public static byte[] getHexByteArray(String hexString) {
|
||||
byte[] buffer = new byte[hexString.length() / 2];
|
||||
if (hexString == null || hexString.equals("")) {
|
||||
return null;
|
||||
}
|
||||
hexString = hexString.toUpperCase();
|
||||
int length = hexString.length() / 2;
|
||||
char[] hexChars = hexString.toCharArray();
|
||||
for (int i = 0; i < length; i++) {
|
||||
int pos = i * 2;
|
||||
buffer[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* String to hexadecimal array (Specified length)
|
||||
*/
|
||||
public static int getHexByteArray(String hexString, byte[] buffer, int nSize) {
|
||||
|
||||
hexString.replace(" ", "");
|
||||
if (nSize > hexString.length() / 2) {
|
||||
nSize = hexString.length() / 2;
|
||||
if (hexString.length() == 1) {
|
||||
nSize = 1;
|
||||
String str = "0";
|
||||
hexString = str + hexString;
|
||||
}
|
||||
}
|
||||
char[] hexChars = hexString.toCharArray();
|
||||
for (int i = 0; i < nSize; i++) {
|
||||
int pos = i * 2;
|
||||
buffer[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
|
||||
}
|
||||
return nSize;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static byte charToByte(char c) {
|
||||
return (byte) "0123456789ABCDEF".indexOf(c);
|
||||
}
|
||||
|
||||
/**
|
||||
* byte array to hexadecimal string
|
||||
*/
|
||||
public static String ByteArrayToString(byte[] bt_ary) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (bt_ary != null)
|
||||
for (byte b : bt_ary) {
|
||||
sb.append(String.format("%02X ", b));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<data>
|
||||
|
||||
<variable
|
||||
name="title"
|
||||
type="String" />
|
||||
|
||||
<variable
|
||||
name="adapter"
|
||||
type="com.example.jinyu_rfid.adapter.ResultAdapter" />
|
||||
|
||||
|
||||
<import type="android.view.View" />
|
||||
</data>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
tools:context=".ReadActivity">
|
||||
|
||||
<include
|
||||
layout="@layout/toolbar"
|
||||
app:title="@{title}" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="45dp"
|
||||
android:layout_marginTop="10dp"
|
||||
android:layout_marginEnd="8dp">
|
||||
|
||||
<TextView
|
||||
style="@style/text_name"
|
||||
android:layout_width="120dp"
|
||||
android:layout_height="match_parent"
|
||||
android:text="@string/epc_code" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/read_text_epc"
|
||||
style="@style/text_name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="@drawable/san_text" />
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="45dp"
|
||||
android:layout_marginTop="10dp"
|
||||
android:layout_marginEnd="8dp">
|
||||
|
||||
<TextView
|
||||
style="@style/text_name"
|
||||
android:layout_width="120dp"
|
||||
android:layout_height="match_parent"
|
||||
android:text="@string/scanning_area" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/read_text"
|
||||
style="@style/text_name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="@drawable/san_text" />
|
||||
</LinearLayout>
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="1"
|
||||
android:adapter="@{adapter}"
|
||||
android:padding="8dp"
|
||||
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
|
||||
|
||||
|
||||
<Button
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="55dp"
|
||||
android:layout_marginTop="10dp"
|
||||
android:layout_marginLeft="10dp"
|
||||
android:layout_marginRight="10dp"
|
||||
android:onClick="readID"
|
||||
android:text="@string/read_epc"
|
||||
android:textSize="18sp" />
|
||||
<Button
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="55dp"
|
||||
android:layout_margin="10dp"
|
||||
android:onClick="readInfo"
|
||||
android:text="@string/go_read"
|
||||
android:textSize="18sp" />
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
</layout>
|
Loading…
Reference in New Issue