uniapp广播接收数据。
·
1.进入页面注册广播事件。
1.进入页面注册广播。
created: function(option) {
this.initScan();
this.startScan();
},
2.点击扫描接收广播特定消息。
openScan()
{
try {
let Intent = plus.android.importClass('android.content.Intent');
let context = plus.android.runtimeMainActivity();
// let intent = new Intent('android.intent.action.SCAN_KEY.down');
let intent = new Intent('com.example.decodeTest.start');
context.sendBroadcast(intent);
} catch (e) {
console.error('Error sending broadcast:', e);
}
},
2.点击扫描接收广播特定消息。
3.完整代码,关注 AI创造财富 公众号!DeepSkai - 智能未来
<template>
<view class="content"></view>
</template>
<script>
var main, receiver, filter;
var _codeQueryTag = false;
const ab2str = function (buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
export default {
name:"scan",
data() {
return {
};
},
created: function(option) {
this.initScan();
this.startScan();
},
onHide: function() {
this.stopScan();
},
destroyed: function() {
/*页面退出时一定要卸载监听,否则下次进来时会重复,造成扫一次出2个以上的结果*/
this.stopScan();
},
methods: {
openScanner() {
uni.scanCode({
success: (res) => {
console.log(res);
// 处理扫码成功后的逻辑
},
fail: (err) => {
console.error(err);
// 处理扫码失败后的逻辑
}
});
},
openScan()
{
try {
let Intent = plus.android.importClass('android.content.Intent');
let context = plus.android.runtimeMainActivity();
// let intent = new Intent('android.intent.action.SCAN_KEY.down');
let intent = new Intent('com.example.decodeTest.start');
context.sendBroadcast(intent);
} catch (e) {
console.error('Error sending broadcast:', e);
}
},
initScan() {
console.log("执行initScan");
let _this = this;
main = plus.android.runtimeMainActivity();
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
filter = new IntentFilter();
filter.addAction('com.kte.scan.result');
console.log("filter created: ", filter);
receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
onReceive: function(context, intent) {
console.log("执行receiver, Action: ", intent.getAction());
// 1. 获取字节数组 byte[]
let bdata = plus.android.invoke(intent, 'getByteArrayExtra', 'code_src');
if (!bdata || bdata.length === 0) {
console.log("字节数组为空或无效");
return;
}
// 2. 将 byte[] 转成 Hex 字符串(可选,用于调试)
let hexString = Array.from(bdata).map(b => {
return ('0' + (b & 0xFF).toString(16)).slice(-2).toUpperCase();
}).join(' ');
console.log("Hex 数据:", hexString);
// 3. 将 byte[] 转成字符串(需指定编码,如 UTF-8)
// 1. 将 Hex 字符串转为 byte[]
let hexArray = hexString.trim().split(' ');
let byteArray = hexArray.map(hex => parseInt(hex, 16));
// 2. 在 UniApp 中调用 Java 的 GB2312 解码
let strResult;
try {
let StringClass = plus.android.importClass('java.lang.String');
let javaString = plus.android.newObject(StringClass, byteArray, 'GB2312');
// 方法1:调用 toString() 并提取内容(兼容 UniApp)
let strResult = plus.android.invoke(javaString, 'toString');
console.log("解码结果:", strResult); // 应输出 "https://deepskai.cn/"
// 方法2(备用):如果方法1无效,直接转为 JS 字符串
if (typeof strResult === 'object') {
strResult = String.fromCharCode(...byteArray); // 直接通过 ASCII 码转换
console.log("备用解码结果:", strResult);
}
_this.queryCode(strResult);
} catch (e) {
console.error("解码失败:", e);
}
}
});
},
startScan() {
main.registerReceiver(receiver, filter);
},
stopScan() {
main.unregisterReceiver(receiver);
},
queryCode: function(code) {
//防重复
if (_codeQueryTag) return false;
_codeQueryTag = true;
setTimeout(function() {
_codeQueryTag = false;
}, 350);
const value = code;
uni.$emit('newbarcode', { code: value });
}
}
};
</script>
<style></style>
更多推荐



所有评论(0)