1.1.1
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
/**
|
||||
* PDA 设备扫码实现(hbyapi 广播协议,基于 QS5502 开发 demo)
|
||||
*
|
||||
* 设备自身带有物理扫码按键,按下即触发扫描头;
|
||||
* 本模块负责「监听扫码结果广播」+「页面扫码按钮主动触发」。
|
||||
*
|
||||
* 协议(来自 QS5502 demo MainActivity.java):
|
||||
* 触发扫描:发送广播 hbyapi.intent.key_scan_down(模拟按键按下)
|
||||
* 接收结果:监听广播 com.qs.scancode,从 extra 键 data 获取扫码字符串
|
||||
*
|
||||
* 用法(与 scan-*.js 同构):
|
||||
* const impl = new ScanPdaImpl()
|
||||
* impl.init()
|
||||
* impl.register((code) => {...})
|
||||
* impl.startScan() // 主动触发(物理键按下同样会回传,无需手动调用)
|
||||
*/
|
||||
|
||||
class ScanPdaImpl {
|
||||
constructor() {
|
||||
this._main = null
|
||||
this._receiver = null
|
||||
this._filter = null
|
||||
this._callback = null
|
||||
this._isScanning = false
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化:获取主 Activity 引用
|
||||
*/
|
||||
init() {
|
||||
this._main = plus.android.runtimeMainActivity()
|
||||
if (!this._main) {
|
||||
throw new Error('[Scan-PDA] 获取主 Activity 失败')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册扫码结果回调(注册 com.qs.scancode 广播接收器)
|
||||
*/
|
||||
register(callback) {
|
||||
this._callback = callback
|
||||
if (this._receiver) return
|
||||
|
||||
var self = this
|
||||
var IntentFilter = plus.android.importClass('android.content.IntentFilter')
|
||||
this._filter = new IntentFilter()
|
||||
this._filter.addAction('com.qs.scancode')
|
||||
|
||||
this._receiver = plus.android.implements(
|
||||
'io.dcloud.feature.internal.reflect.BroadcastReceiver',
|
||||
{
|
||||
onReceive: function (context, intent) {
|
||||
plus.android.importClass(intent)
|
||||
var codeStr = intent.getStringExtra('data')
|
||||
if (!codeStr) return
|
||||
console.log('[Scan-PDA] 扫码结果:', codeStr)
|
||||
self._isScanning = false
|
||||
if (self._callback) {
|
||||
self._callback(codeStr)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
this._main.registerReceiver(this._receiver, this._filter)
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销监听(页面卸载时调用)
|
||||
*/
|
||||
unregister() {
|
||||
if (this._receiver && this._main) {
|
||||
this._main.unregisterReceiver(this._receiver)
|
||||
this._receiver = null
|
||||
}
|
||||
this._callback = null
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始扫描:发送 key_scan_down 广播主动触发扫描头
|
||||
* 设备物理扫码键按下时同样会回传结果,无需手动调用此方法
|
||||
*/
|
||||
startScan() {
|
||||
if (!this._main) return
|
||||
var Intent = plus.android.importClass('android.content.Intent')
|
||||
var intent = new Intent()
|
||||
intent.setAction('hbyapi.intent.key_scan_down')
|
||||
this._main.sendBroadcast(intent)
|
||||
this._isScanning = true
|
||||
console.log('[Scan-PDA] 已触发扫描广播')
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止扫描
|
||||
* demo 仅演示 key_scan_down;部分同协议机型支持 key_scan_up 停止,
|
||||
* 这里 try-catch 发送,无副作用
|
||||
*/
|
||||
stopScan() {
|
||||
this._isScanning = false
|
||||
if (!this._main) return
|
||||
try {
|
||||
var Intent = plus.android.importClass('android.content.Intent')
|
||||
var intent = new Intent()
|
||||
intent.setAction('hbyapi.intent.key_scan_up')
|
||||
this._main.sendBroadcast(intent)
|
||||
} catch (e) {
|
||||
console.log('[Scan-PDA] 发送停止广播失败(可忽略):', e.message)
|
||||
}
|
||||
}
|
||||
|
||||
isScanning() {
|
||||
return this._isScanning
|
||||
}
|
||||
|
||||
/**
|
||||
* 完全销毁(应用退出时调用)
|
||||
*/
|
||||
destroy() {
|
||||
this.unregister()
|
||||
this._main = null
|
||||
this._filter = null
|
||||
}
|
||||
}
|
||||
|
||||
export default ScanPdaImpl
|
||||
Reference in New Issue
Block a user