public void eventReadNotify(RfidReadEvents rfidReadEvents) { sendData(reader.Actions.getReadTags(100)); } void sendData(TagData[] b) { runOnUiThread(() -> { if (b != null) { for (TagData t : b) { if (isASCII) { tag = new Tag(t.getTagID(), RFDUtils.hex2ascii(t.getTagID()), ""); } else { tag = new Tag(t.getTagID(), "", ""); } } startBeepingTimer(); tagRecyclerViewAdapter.addItem(tag); binding.total.setText(String.valueOf(tempData.size())); } }); }什么意思
时间: 2024-02-10 13:32:37 浏览: 29
这是一段Java代码,它包含了一个事件处理函数`eventReadNotify`和一个数据发送函数`sendData`。在事件处理函数`eventReadNotify`中,当触发读取RFID标签事件时,会通过`reader.Actions.getReadTags(100)`方法获取最多100个标签数据,并将其传递给数据发送函数`sendData`进行处理。在数据发送函数中,如果读取到的标签数据不为空,则会将其转换为`Tag`对象,并将其添加到`tagRecyclerViewAdapter`中进行展示。最后,通过`binding.total.setText(String.valueOf(tempData.size()))`方法在UI界面上显示当前标签数量。代码中的`isASCII`变量可能表示是否需要将标签ID转换为ASCII格式。
相关问题
sendData(reader.Actions.getReadTags(100));什么意思
这段代码的作用是通过 `reader` 对象向某个设备发送指令,指令的内容是读取100个标签的信息。具体来说,`reader` 对象调用了 `Actions` 类中的 `getReadTags()` 方法,并将读取标签的数量作为参数传递给该方法,然后将该方法返回的指令数据通过 `sendData()` 方法发送给设备。这段代码的目的是让设备读取一定数量的标签信息,并将这些信息发送回来供后续处理。
public class InventoryRFID implements RfidEventsListener { @Override public void eventReadNotify(RfidReadEvents rfidReadEvents) { TagData[] tags = reader.Actions.getReadTags(500); if (tags == null) { return; } for (final TagData tag : tags) { startBeepingTimer(); if (epcList.add(tag.getTagID())) { runOnUiThread(new Runnable() { @Override public void run() { String epc = tag.getTagID(); if (epc.startsWith("40")) { String hexEpc = hexToAscii(epc); Log.e("tag", tag.getTagID()); reader(hexEpc); } } }); } } }怎么使用 Handler 避免并发修改 UI。
可以使用 Handler 来避免并发修改 UI,具体步骤如下:
1. 创建 Handler 对象,并在主线程中进行实例化。
2. 在 runOnUiThread 中,使用 Handler 发送一条消息,让其在主线程中更新 UI。
3. 在 Handler 中处理消息,更新 UI。由于 Handler 在主线程中运行,所以不会出现并发修改 UI 的问题。
修改后的代码如下:
```
public class InventoryRFID implements RfidEventsListener {
private Handler mHandler = new Handler(Looper.getMainLooper());
@Override
public void eventReadNotify(RfidReadEvents rfidReadEvents) {
TagData[] tags = reader.Actions.getReadTags(500);
if (tags == null) {
return;
}
for (final TagData tag : tags) {
startBeepingTimer();
if (epcList.add(tag.getTagID())) {
mHandler.post(new Runnable() {
@Override
public void run() {
String epc = tag.getTagID();
if (epc.startsWith("40")) {
String hexEpc = hexToAscii(epc);
Log.e("tag", tag.getTagID());
reader(hexEpc);
}
}
});
}
}
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)