完成 写入

master
wangh 6 months ago
parent a8ee80ce6d
commit 9b641db9c0

@ -21,6 +21,7 @@ import com.example.jinyu_rfid.callback.DataReturnCall;
import com.example.jinyu_rfid.databinding.ActivityWriteUserBinding;
import com.example.jinyu_rfid.rfid.C5106Device;
import com.example.jinyu_rfid.rfid.RFIDModel;
import com.example.jinyu_rfid.uitls.ASCIIUtil;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
@ -62,9 +63,11 @@ public class WriteUserActivity extends BaseActivity implements DataReturnCall {
rfidModel.sanEpc(6);
}
private boolean writeUserState = false;
// 写入
public void writeUser(View view) {
if (list == null || list.isEmpty()) return;
/* if (list == null || list.isEmpty()) return;
var epcStr = binding.writeEpcCode.getText().toString();
if (epcStr.isEmpty()) return;
// EPC编码~胎号~品牌~规格~花纹~层级~轮胎名称~速度级别~负荷指数~轮辋直径~扁平比~销售区域
@ -74,13 +77,13 @@ public class WriteUserActivity extends BaseActivity implements DataReturnCall {
var configuration = tagList.get(i);
var state = configuration.isState();
tagUserStr.append("~");
/* if (state) {
*//* if (state) {
Optional<ReadTyreNoResult> optionalResult = Optional.ofNullable(list.get(i-1));
String propertyContent = optionalResult.map(ReadTyreNoResult::getPropertyContent).orElse("_");
tagUserStr.append(propertyContent);
} else {
tagUserStr.append("_");
}*/
}*//*
if (state) {
try {
var result = list.get(i - 1);
@ -93,7 +96,32 @@ public class WriteUserActivity extends BaseActivity implements DataReturnCall {
tagUserStr.append("_");
}
}
Log.e("TAG", "writeUser:" + tagUserStr);
String user = ASCIIUtil.str2Hex(tagUserStr.toString());
Log.e("TAG", "writeUser:" + user);
Log.e("TAG", "writeUser:" + user.length());*/
if (!writeUserState) {
Log.e("TAG", "writeUser:" + 1);
// writeUserState = rfidModel.writeUser(user, epcStr);
writeUserState = true;
Toast.makeText(context, getString(writeUserState ? R.string.write_success : R.string.write_failed), Toast.LENGTH_SHORT).show();
}
if (writeUserState) {
Map<String ,String> map=new HashMap<>();
map.put("EpcCode","1231");
map.put("TyreNo","1231");
map.put("token","123456");
Log.e("TAG", "writeUser:" + 2);
OkGo.<MyResult>post(url + "/write")
.upRequestBody(RequestBody.create(JSON, gson.toJson(map))).execute(new MyRecultCall(dialog, this) {
});
}
// writeUserState=false;
}
}

@ -18,17 +18,46 @@ public class C5106Device implements RFIDModel {
private UHFRManager uhfrManager;
private DataReturnCall call;
public byte[] accessBytes = Tools.HexString2Bytes("00000000");
public C5106Device(DataReturnCall call) {
uhfrManager = UHFRManager.getInstance();
this.call = call;
}
@Override
public void sanEpc(int len) {
byte[] rdata = new byte[len * 2];
Reader.READER_ERR readerErr = uhfrManager.getTagData(1, 2, len, rdata, accessBytes, (short) 200);
call.readEpcCodeInfo(Tools.Bytes2HexString(rdata, rdata.length), readerErr == Reader.READER_ERR.MT_OK_ERR, readerErr.toString());
}
public boolean writeUser() {
return false;
}
/**
* int mbank, 0:RESERVED1:EPC2:TID3:USER
* int startaddress, word
* byte[] data,
* int datalen, word
* byte[] password, 访4byte
* short timeout
* byte[] fdata,
* int fbank, 1 :EPC,2: TID ,3: USER
* int fstartaddr, word
* boolean matching true:,false:
*/
@Override
public boolean writeUser(String writeStr,String tagEpc) {
var bytes = Tools.HexString2Bytes(writeStr);
var bytesF = Tools.HexString2Bytes(tagEpc);
Reader.READER_ERR readerErr = uhfrManager.writeTagDataByFilter(
(char) 3, 0, bytes, bytes.length, accessBytes, (short) 200,
bytesF ,1,2,true);
Log.e("TAG", "writeUser:" + readerErr.toString());
return readerErr == Reader.READER_ERR.MT_OK_ERR;
}
}

@ -6,4 +6,6 @@ package com.example.jinyu_rfid.rfid;
*/
public interface RFIDModel {
void sanEpc(int len);
boolean writeUser(String writeStr, String tagEpc);
}

@ -0,0 +1,68 @@
package com.example.jinyu_rfid.uitls;
import android.util.Log;
/**
* @author: wangh
* @description: Ass
* @date: 2019/06/20-15:18
*/
public class ASCIIUtil {
// String明文转ASCII码hex字符串,一个明文字符生成两个字符表示的16进制ASCII码
public static String str2Hex(String str) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
// 这里的第二个参数16表示十六进制
sb.append(Integer.toString(c, 16));
// 或用toHexString方法直接转成16进制
// sb.append(Integer.toHexString(c));
}
return sb.toString();
}
// ASCII码hex字符串转String明文
public static String hex2Str(String hex) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hex.length() - 1; i += 2) {
String h = hex.substring(i, (i + 2));
int decimal = Integer.parseInt(h, 16);
sb.append((char) decimal);
}
return sb.toString();
}
public static String hexTo4ZN(String hex) throws Exception {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hex.length() - 1; i += 4) {
String h = hex.substring(i, (i + 4));
sb.append((char) Integer.parseInt(h, 16));
}
return sb.toString();
}
public static String str4Hex(String str) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
String s = Integer.toString(str.charAt(i), 16);
sb.append(s.length()==2?"00"+s:s);
}
return sb.toString();
}
public static String convertToAscii(String chineseText) {
StringBuilder asciiText = new StringBuilder();
for (int i = 0; i < chineseText.length(); i++) {
char c = chineseText.charAt(i);
int asciiValue = (int) c;
asciiText.append(asciiValue).append(" ");
}
return asciiText.toString().trim();
}
}

@ -27,4 +27,6 @@
<string name="project_list_rim">rim diameter</string>
<string name="project_list_flat">flat ratio</string>
<string name="project_list_area">sales area</string>
<string name="write_failed">write failed</string>
<string name="write_success">write success</string>
</resources>

@ -26,5 +26,7 @@
<string name="project_list_load">负荷指数</string>
<string name="project_list_rim">轮辋直径</string>
<string name="project_list_flat">扁平比</string>
<string name="project_list_area">销售区域</string>>
<string name="project_list_area">销售区域</string>
<string name="write_failed">写入失败</string>
<string name="write_success">写入成功</string>>
</resources>
Loading…
Cancel
Save