sourceSets { main { jniLibs.srcDirs = [‘libs’] } }
]
将这些文件添加完之后不要急着去点击Make Project【小锤子】,要先在build.gradle文件中添加如下代码:
implementation files(‘libs/xUtils-2.5.5.jar’)
implementation files(‘libs/jxl.jar’)
implementation files(‘libs/cw-deviceapi20190815.jar’)
这个时候就可以Make Project【构建项目了】
private void initUHF() {
try {
mReader = RFIDWithUHF.getInstance();
} catch (Exception e) {
showToast(e.getMessage());
}
if (mReader != null) {
//这里用了一个异步线程去初始化,如果不开启线程的话就会直接崩溃😂
new InitTask().execute();
}
}
//初始化的异步任务
@Override
protected Boolean doInBackground(String… params) {
// TODO Auto-generated method stub
return mReader.init();
}
//自动识别标签
private void readTag() {
// 识别标签
if (btnStart.getText().toString().equals(“开始识别”)) {
// 单标签循环
if (mReader.startInventoryTag(0, 0)) {
btnStart.setText(“停止识别”);
loopFlag = true;
new TagThread().start();
} else {
mReader.stopInventory();
showToast(“开启失败----”);
}
} else {
// 停止识别
sto
pInventory();
}
}
//手动识别标签
private void readTag() {
// 单步
String strUII = mReader.inventorySingleTag();
if (!TextUtils.isEmpty(strUII)) {
String strEPC = mReader.convertUiiToEPC(strUII);
addEPCToList(strEPC, “N/A”);
tvCount.setText(“” + adapter.getItemCount());
} else {
showToast(“失败哦!”);
}
}
自动识别的时候开了一个异步任务实时数据更新,如下:
class TagThread extends Thread {
@Override
public void run() {
String strTid;
String strResult;
String[] res;
while (loopFlag) {
res = mReader.readTagFromBuffer();
if (res != null) {
strTid = res[0];
if (strTid.length() != 0 && !strTid.equals(“0000000000000000”) && !strTid.equals(“000000000000000000000000”)) {
strResult = “TID:” + strTid + “\n”;
} else {
strResult = “”;
}
Log.i(“data”, “EPC:” + res[1] + “|” + strResult);
Message msg = handler.obtainMessage();
msg.obj = strResult + “EPC:” + mReader.convertUiiToEPC(res[1]) + “@” + res[2];
handler.sendMessage(msg);
}
}
}
}
在handeler中将数据添加的List集合中并展示在列表中:
private void addEPCToList(String epc, String rssi) {
if (!TextUtils.isEmpty(epc)) {
int index = checkIsExist(epc);
TestBean testBean = new TestBean();
testBean.setTagCount(String.valueOf(1));
testBean.setTagRssi(rssi);
testBean.setTagUii(epc);
if (index == -1) {
tagList.add(testBean);
recyclerView.setAdapter(adapter);
tvCount.setText(“” + adapter.getItemCount());
} else {
int tagcount = Integer.parseInt(tagList.get(index).getTagCount(), 10) + 1;
testBean.setTagCount(String.valueOf(tagcount));