Created
July 25, 2019 10:13
-
-
Save IcedMango/232d22ec5ee26887821c2f9e4cc61eb2 to your computer and use it in GitHub Desktop.
How to get boardcast info from pda scanner with mui - 如何用MUI获取PDA扫码枪的广播信息
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | |
//通过intent实例引入intent类,方便以后的‘.’操作 | |
plus.android.importClass(context); | |
//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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment