如何在uniapp小程序和app中支持pad扫码枪

🙂前言

扫条码通常直接调用uniapp的官方api接口拉起摄像头来实现,但是新出的平板型pda扫码枪中,操作员可能更习惯pda操作模式,而不是通过点击UI交互使用摄像头,这时应该如何在uniapp小程序和app中支持pad扫码枪?本文分享实现uniapp小程序和app中支持pad扫码枪的方法步骤。

基于vue2项目代码中添加,vue3语法糖的组合式写法差异点业务代码页面,其他都是一致的,晚点小波会同步更新在vue3+vite+typescript+pinia+uni-ui+pnpm项目中。


🙂第一步:utils文件夹创建个pdaScan.js文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
let main
let filter
let receiver
let tag = false
/**
* 开始广播监听扫码
*/
const regScan = () => {
main.registerReceiver(receiver, filter)
}
/**
* 停止广播监听扫码
*/
const unRegScan = () => {
main.unregisterReceiver(receiver)
}
/** 剩余下个变量已经做了全局变量
* // PDA扫描广播码
appScanAction: 'android.intent.action.SCANRESULT',
// PDA 扫码广播值字段
appScanExtra: 'value',
*/
const initScan = (onReceive) => {Q
console.log('initScan')
main = plus.android.runtimeMainActivity() //获取activity
var IntentFilter = plus.android.importClass('android.content.IntentFilter')
filter = new IntentFilter()
// filter.addAction('android.intent.action.SCANRESULT') // 换你的广播动作
// 东大集团 常见扫码枪广播 https://blog.csdn.net/echizao1839/article/details/115962599
filter.addAction('com.android.server.scannerservice.broadcast') // 换你的广播动作
receiver = plus.android.implements(
'io.dcloud.feature.internal.reflect.BroadcastReceiver',
{
onReceive: (context, intent) => {
plus.android.importClass(intent)
// let code = intent.getStringExtra('value') // 换你的广播标签
let code = intent.getStringExtra('scannerdata') // 换你的广播标签
console.log('pdaScan:1', code)

// 必须延迟一下,不然无法调用
setTimeout(() => {
uni.$emit('scancodedate', {
code,
result: code
})
}, 300)
}
}
)
}

export { initScan, regScan, unRegScan }

🧐第二步:入口main.js文件添加代码

1
2
3
4
5
6
7
8
// main.js
import { initScan, regScan, unRegScan } from "@/utils/pdaScan.js";

// APP PDA扫描
// #ifdef APP-PLUS
initScan();
regScan();
// #endif

🤔第三步:业务代码文件x.vue中调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//  #ifdef APP-PLUS
// 针对pda扫码监控获取值
onShow() {
uni.$off('scancodedate') // 每次进来先 移除全局自定义事件监听器
uni.$on('scancodedate', (data) => {
// data最好前后截取空格,扫码枪扫出来的code可能会有空格
this.scan(data)
})
},
onLoad() {},
onHide() {
uni.$off('scancodedate')
},
onUnload() {
uni.$off('scancodedate')
},
destroyed() {
uni.$off('scancodedate')
}
// #endif

😚常见pda扫码枪对应的广播名称code

引用: 常见扫码枪广播名称[1]

品牌品牌名称型号版本号广播名广播接收名
智联天地alpssimphonebase_v1.3.13android.intent.action.RECEIVE_SCANDATA_BROADCASTandroid.intent.extra.SCAN_BROADCAST_DATA
智联天地alpssimphonebase-m8_v1.6.13android.intent.action.RECEIVE_SCANDATA_BROADCASTandroid.intent.extra.SCAN_BROADCAST_DATA
智联天地alpsthimfoneandroid.intent.action.RECEIVE_SCANDATA_BROADCASTandroid.intent.extra.SCAN_BROADCAST_DATA
智联天地alpsN5android.intent.action.RECEIVE_SCANDATA_BROADCASTandroid.intent.extra.SCAN_BROADCAST_DATA
智联天地alpsn5android.intent.action.RECEIVE_SCANDATA_BROADCASTandroid.intent.extra.SCAN_BROADCAST_DATA
智联天地N5S
智联天地zltdN7enlscan.action.SCANNER_RESULTSCAN_BARCODE1
智联天地NEOLEXandroid.intent.action.SCANRESULTvalue
智联天地Android TDandroid.intent.action.SCANRESULTvalue
智联天地Alps Androidandroid.intent.action.SCANRESULTvalue
智联天地Androidandroid.intent.action.SCANRESULTvalue
智联天地X7android.intent.action.SCANRESULTvalue
新大陆NewlandNLS-MT65Wnlscan.action.SCANNER_RESULTSCAN_BARCODE1或SCAN_BARCODE2
新大陆NEWLANDNLS-MT60android.intent.action.RECEIVE_SCANDATA_BROADCASTandroid.intent.extra.SCAN_BROADCAST_DATA
新大陆NEWLANDn5android.intent.action.RECEIVE_SCANDATA_BROADCASTandroid.intent.extra.SCAN_BROADCAST_DATA
新大陆NEWLANDsimphoneandroid.intent.action.RECEIVE_SCANDATA_BROADCASTandroid.intent.extra.SCAN_BROADCAST_DATA
新大陆NEWLANDN5android.intent.action.RECEIVE_SCANDATA_BROADCASTandroid.intent.extra.SCAN_BROADCAST_DATA
新大陆NEWLANDN5Sandroid.intent.action.RECEIVE_SCANDATA_BROADCASTandroid.intent.extra.SCAN_BROADCAST_DATA
新大陆NEWLANDNLS-MT60EACTION_BAR_SCAN EXTRA_SCAN_DATA
新大陆NEWLANDthimfonenlscan.action.SCANNER_RESULTSCAN_BARCODE1,SCAN_BARCODE2
新大陆NEWLANDMT65nlscan.action.SCANNER_RESULTSCAN_BARCODE1,SCAN_BARCODE3
新大陆NEWLANDNLS-MT65nlscan.action.SCANNER_RESULTSCAN_BARCODE1,SCAN_BARCODE4
新大陆NEWLANDNLS-MT66nlscan.action.SCANNER_RESULTSCAN_BARCODE1,SCAN_BARCODE5
新大陆NEWLANDNLS-MT90nlscan.action.SCANNER_RESULTSCAN_BARCODE1,SCAN_BARCODE6
东大AUTOID Q7com.android.server.scannerservice.broadcast,com.android.server.aascannerdata
东大AUTOIDmsm8610com.android.server.scannerservice.broadcast,com.android.server.aascannerdata
东大AUTOIDPDT-900com.android.server.scannerservice.broadcast,com.android.server.aascannerdata
东大AUTOIDPDT-90Pcom.android.server.scannerservice.broadcast,com.android.server.aascannerdata
东大AUTOIDAUTOID9com.android.server.scannerservice.broadcast,com.android.server.aascannerdata
东大AUTOIDPDT-90Fcom.android.server.scannerservice.broadcast,com.android.server.aascannerdata
东大SEUICAUTOID Q9com.android.server.scannerservice.broadcast,com.android.server.aascannerdata
东大SEUICD310Ccom.android.server.scannerservice.broadcast,com.android.server.aascannerdata
东大SEUICCRUISE Gecom.android.server.scannerservice.broadcast,com.android.server.aascannerdata
优博讯qcomAndroid Handheld Terminalcom.barcode.sendBroadcastBARCODE
优博讯qcomCRUISEcom.android.server.scannerservice.broadcast,com.android.server.aascannerdata
优博讯qcomi6200Sandroid.intent.ACTION_DECODE_DATAbarcode_string,scannerdata
优博讯qcomi6200Aandroid.intent.ACTION_DECODE_DATAbarcode_string,scannerdata
优博讯qcomI6200Aandroid.intent.ACTION_DECODE_DATAbarcode_string,scannerdata
优博讯qcomi6200S安卓4.1版本urovo.rcv.messagebarcode
优博讯UrovoDT40android.intent.ACTION_DECODE_DATAbarcode_string
肖邦SUPOINPDAcom.android.server.scannerservice.broadcastscannerdata
肖邦SUPOINSHT30com.android.server.scannerservice.broadcastscannerdata
肖邦SUPOINSHT36com.android.server.scannerservice.broadcastscannerdata
霍尼韦尔HoneywellEDAcom.honeywell.scan.broadcastdata
捷宝JBcom.jb.action.GET_SCANDATAdata
商米SUNMIL2com.sunmi.scanner.ACTION_DATA_CODE_RECEIVEDdata
商米SUNMIV2_PROcom.sunmi.scanner.ACTION_DATA_CODE_RECEIVEDdata
凯立KAICOM,kaicomW660com.android.receive_scan_actiondata
凯立KAICOM,kaicomK211com.android.receive_scan_actiondata
凯立KAICOM,kaicomK7com.android.receive_scan_actiondata
盈达iDataK1android.intent.action.SCANRESULTvalue
盈达Androidandroid.intent.action.SCANRESULTvalue
思必拓KT40 se4500KT40 se4500条码action=com.se4500.onDecodeComplete,string数据key=se4500,开启扫描action=com.geomobile.se4500barcode,停止扫描action=com.geomobile.se4500barcode.poweroffse4500
得景条码action=techain.intent.action.DISPLAY_SCAN_RESULT,string数据key=decode_datadecode_data
新石器条码action=techain.intent.action.DISPLAY_SCAN_RESULT,string数据key=decode_datadecode_data
安能定制广播条码action=com.android.receive_scan_action,string数据key=datadata
圆通定制广播条码action=com.yto.action.GET_SCANDATA,string数据key=data,开启扫描action=com.yto.action.SCAN_SWITCH,开启扫描传值int,key=extra,值=1,关闭扫描action=com.yto.action.SCAN_SWITCH,关闭扫描传值int,key=extra,值=0data

😊总结

思路很简单,就是uniapp小程序启动时调用pda的广播函数,业务代码中直接获取对应pda返回的条码信息即可。


😊来自小波的 bilibili 视频教程


🙂相关参考资料链接

关注廿壴(GANXB2)微信公众号

『旅行者』,帮小波关注一波公众号吧。

小波需要100位关注者才能申请红包封面设计资格,万分感谢!

关注后可微信小波,前66的童鞋可以申请专属红包封面设计。

THE END
作者
chopin gump chopin gump
小尾巴
Stay Hungry, Stay Foolish「求知若饥, 虚心若愚」 — 廿壴(GANXB2)
许可协议
如何在uniapp小程序和app中支持pad扫码枪
https://blog.ganxb2.com/33231.html
post_default_index_img
微信

微信

支付宝

支付宝

- ( ゜- ゜)つロ 填写QQ邮箱自动获取头像亦更快获得回复通知
评论区显示“刷新”,多点几下或过会儿再来康康 _(≧∇≦」∠)_