You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.9 KiB
76 lines
1.9 KiB
|
3 weeks ago
|
let pdaMain;
|
||
|
|
let filter;
|
||
|
|
let receiver;
|
||
|
|
let receiving = false;
|
||
|
|
/**
|
||
|
|
* 开始广播监听扫码
|
||
|
|
*/
|
||
|
|
const start = () => {
|
||
|
|
/* #ifdef APP-PLUS */
|
||
|
|
if (!pdaMain)
|
||
|
|
return;
|
||
|
|
pdaMain.registerReceiver(receiver, filter);
|
||
|
|
// console.log('pda scan started.');
|
||
|
|
/* #endif */
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 停止广播监听扫码
|
||
|
|
*/
|
||
|
|
const stop = () => {
|
||
|
|
/* #ifdef APP-PLUS */
|
||
|
|
if (!pdaMain)
|
||
|
|
return;
|
||
|
|
pdaMain.unregisterReceiver(receiver);
|
||
|
|
// console.log('pda scan stopped.');
|
||
|
|
/* #endif */
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 定义广播
|
||
|
|
*/
|
||
|
|
const init = (receive) => {
|
||
|
|
//获取activity
|
||
|
|
/* #ifdef APP-PLUS */
|
||
|
|
pdaMain = plus.android.runtimeMainActivity();
|
||
|
|
const IntentFilter = plus.android.importClass('android.content.IntentFilter');
|
||
|
|
filter = new IntentFilter();
|
||
|
|
// 扫描设置的广播名称,霍尼韦尔:com.honeywell.scan,优博讯:android.intent.ACTION_DECODE_DATA
|
||
|
|
// 肯麦思公司:com.android.server.scannerservice.broadcast
|
||
|
|
filter.addAction("com.android.server.scannerservice.broadcast");
|
||
|
|
|
||
|
|
// filter.addCategory('sn');
|
||
|
|
receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
|
||
|
|
onReceive: function(context, intent) {
|
||
|
|
plus.android.importClass(intent);
|
||
|
|
// 扫描设置的开发者选项--键值名称
|
||
|
|
const code = intent.getStringExtra("scannerdata");
|
||
|
|
//防重复
|
||
|
|
if (receiving) return;
|
||
|
|
receiving = true;
|
||
|
|
setTimeout(function() {
|
||
|
|
receiving = false;
|
||
|
|
}, 300);
|
||
|
|
receive(code);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
// console.log('pda scan inited.');
|
||
|
|
/* #endif */
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
const destroy = () => {
|
||
|
|
/* #ifdef APP-PLUS */
|
||
|
|
receiver = undefined;
|
||
|
|
filter = undefined;
|
||
|
|
pdaMain = undefined;
|
||
|
|
// console.log('pda scan destroied.');
|
||
|
|
/* #endif */
|
||
|
|
}
|
||
|
|
|
||
|
|
export default {
|
||
|
|
init,
|
||
|
|
start,
|
||
|
|
stop,
|
||
|
|
destroy
|
||
|
|
};
|