43 lines
1.4 KiB
JavaScript
43 lines
1.4 KiB
JavaScript
|
|
export const initRfidScan = (callback) => {
|
||
|
|
// #ifdef APP-PLUS
|
||
|
|
const main = plus.android.runtimeMainActivity();
|
||
|
|
const IntentFilter = plus.android.importClass('android.content.IntentFilter');
|
||
|
|
const filter = new IntentFilter();
|
||
|
|
filter.addAction('com.ubx.scan.rfid'); // 广播动作
|
||
|
|
|
||
|
|
const processedData = new Set(); // 用于记录已处理的数据
|
||
|
|
let debounceTimer = null; // 用于存储定时器
|
||
|
|
const debounceDelay = 500; // 防抖延迟时间(毫秒)
|
||
|
|
|
||
|
|
const receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
|
||
|
|
onReceive: (context, intent) => {
|
||
|
|
plus.android.importClass(intent);
|
||
|
|
const rfidEpc = intent.getStringExtra('rfid_epc'); // 广播标签数据
|
||
|
|
const rfidData = intent.getStringExtra('rfid_data'); // 广播标签数据
|
||
|
|
|
||
|
|
// 防抖处理
|
||
|
|
clearTimeout(debounceTimer);
|
||
|
|
debounceTimer = setTimeout(() => {
|
||
|
|
// 检查是否已经处理过该数据
|
||
|
|
if (!processedData.has(rfidData)) {
|
||
|
|
processedData.add(rfidData); // 记录已处理的数据
|
||
|
|
if (callback) {
|
||
|
|
callback({ rfidEpc, rfidData });
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
console.log('Duplicate RFID Data:', rfidData);
|
||
|
|
}
|
||
|
|
}, debounceDelay);
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
main.registerReceiver(receiver, filter);
|
||
|
|
|
||
|
|
// 保存引用以便后续注销
|
||
|
|
return () => {
|
||
|
|
clearTimeout(debounceTimer); // 清除定时器
|
||
|
|
main.unregisterReceiver(receiver);
|
||
|
|
processedData.clear(); // 清空已处理的数据记录
|
||
|
|
};
|
||
|
|
// #endif
|
||
|
|
};
|