scan.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /**
  2. * pda红外线扫码
  3. */
  4. var main = ''
  5. var filter = ''
  6. var receiver = ''
  7. var codeQueryTag = false
  8. let scanSuccess = function() {} // 成功后的操作函数
  9. //开启服务监听
  10. export function initScan(successFun) {
  11. scanSuccess = successFun
  12. console.log('开启了监听');
  13. main = plus.android.runtimeMainActivity(); //获取activity
  14. var IntentFilter = plus.android.importClass('android.content.IntentFilter');
  15. filter = new IntentFilter();
  16. filter.addAction("android.intent.ACTION_DECODE_DATA"); // 换你的广播动作 (设置-扫描设置-输出方式-输出方式改为Intent输出)
  17. receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
  18. onReceive: function(context, intent) {
  19. plus.android.importClass(intent);
  20. let code = intent.getStringExtra("barcode_string"); // 换你的广播标签
  21. queryCurrenCode(code);
  22. }
  23. });
  24. console.log('开启监听完成');
  25. }
  26. //这个得有,开启监听用
  27. export function startScan() {
  28. main.registerReceiver(receiver, filter);
  29. }
  30. //这个也得有,关闭监听用
  31. export function stopScan() {
  32. main.unregisterReceiver(receiver);
  33. }
  34. //这个是防止重复用的
  35. async function queryCurrenCode(code) {
  36. //防重复
  37. if (codeQueryTag) return false;
  38. codeQueryTag = true;
  39. setTimeout(() => {
  40. codeQueryTag = false;
  41. }, 150);
  42. console.log('调用接口啦------', code);
  43. scanSuccess(code)
  44. }