[HBuilder]HBuilder应用监听PDA扫描枪广播

之前在对PDA定制开发的时候,对扫描到的二维码有监听需求,代码如下

if (window.plus) {
    plusReady();
} else {
    document.addEventListener('plusready', plusReady, false);
}


function plusReady() {
    var main = plus.android.runtimeMainActivity(); //获取activity  
    var context = plus.android.importClass('android.content.Context'); //上下文  
    var receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
        onReceive: function (context, intent) {
            plus.android.importClass(context); //通过intent实例引入intent类,方便以后的‘.’操作  
						//byte类型数据,扫描枪返回数据
            var barocode = intent.getByteArrayExtra("barocode");
						//返回数据长度
            var barocodelen = intent.getIntExtra("length", 0);
            //var temp = intent.getByteExtra("barcodeType", (byte) 0);
            var aimid = intent.getByteArrayExtra("aimid");

            console.log(byteToString(barocode))
            alert(byteToString(barocode))
            //main.unregisterReceiver(receiver); //取消监听  
        }
    });
    var IntentFilter = plus.android.importClass('android.content.IntentFilter');
    var Intent = plus.android.importClass('android.content.Intent');
    var filter = new IntentFilter();
    filter.addAction("scan.rcv.message"); //监听扫描  
    main.registerReceiver(receiver, filter); //注册监听  
}

//基类方法 转换byte为string字符串
function byteToString(arr) {
    if (typeof arr === 'string') {
        return arr;
    }
    var str = '',
        _arr = arr;
    for (var i = 0; i < _arr.length; i++) {
        var one = _arr[i].toString(2),
            v = one.match(/^1+?(?=0)/);
        if (v && one.length == 8) {
            var bytesLength = v[0].length;
            var store = _arr[i].toString(2).slice(7 - bytesLength);
            for (var st = 1; st < bytesLength; st++) {
                store += _arr[st + i].toString(2).slice(2);
            }
            str += String.fromCharCode(parseInt(store, 2));
            i += bytesLength - 1;
        } else {
            str += String.fromCharCode(_arr[i]);
        }
    }
    return str;
}

 

0 条评论
发表一条评论