pda我这里调用action:com.rfid.SCAN_CMD,获取action:com.rfid.SCAN, 根据自己手里的pda去设置,可以联系厂家售后
需在pda扫码软件中将模式改为广播
<template>
<view>
<view v-for="(item, index) in list" :key="index"> {{ item }} </view>
<button @click="scan">扫描</button>
</view>
</template>
<script>
import { scanCode } from "@/utils/scan";
import scan from "@/mixins/scan";
export default {
mixins: [scan],
data() {
return {
list: [],
};
},
methods: {
async scan() {
const code = await scanCode();
console.log("手动扫描", code);
this.list.unshift("手动扫描" + code);
},
onReceiveCode(code) {
console.log("页面监听", code);
this.list.unshift("页面监听" + code);
},
},
};
</script>
//app.vue
<script>
import { registerReceiver } from "@/utils/scan";
export default {
onLaunch() {
registerReceiver();
}
};
</script>
</style>
// @/utils/scan.js
const timeout = 5 * 1000; //扫码超时时间
const main = plus.android.runtimeMainActivity();
// 全局只允许一个地方获取,存储最新的回调函数,获取完立即销毁
let scanCallback = null;
//存储订阅,每个页面可以单独订阅,优先级低于scanCallback
const subscriptions = [];
export const byteArrayToString = (array) => {
return String.fromCharCode.apply(String, array);
};
const sleep = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
export const registerReceiver = () => {
//获取Android意图过滤类
const IntentFilter = plus.android.importClass("android.content.IntentFilter");
//实例化意图过滤
const filter = new IntentFilter();
//获取扫码成功的意图广播,
filter.addAction("com.rfid.SCAN");
const receiver = plus.android.implements(
"io.dcloud.feature.internal.reflect.BroadcastReceiver",
{
onReceive: function (context, intent) {
plus.android.importClass(intent);
// key 为 厂商提供,需要自己联系厂商获取 ,有的厂商使用getStringExtra就直接可以
const dataByte = intent.getByteArrayExtra("data");
const code = byteArrayToString(dataByte);
console.log("receiver:" + code);
if (scanCallback) {
scanCallback(code);
} else {
subscriptions.forEach((cb) => cb(code));
}
},
}
);
main.registerReceiver(receiver, filter);
};
export const openScan = () => {
//获取Android意图类
let Intent = plus.android.importClass("android.content.Intent");
//实例化意图
let intent = new Intent();
//定义意图,由厂商提供
intent.setAction("com.rfid.SCAN_CMD");
//广播这个意图
main.sendBroadcast(intent);
};
export const scanCode = async () => {
uni.showLoading({ title: "扫描中", mask: true });
const getCode = () => {
return new Promise((resolve) => {
scanCallback = (code) => {
resolve(code);
};
});
};
openScan();
const res = await Promise.race([getCode(), sleep(timeout)]);
scanCallback = null;
uni.hideLoading();
if (!res) {
uni.showToast({
icon: "none",
title: "扫描失败",
});
throw new Error("扫描失败");
}
return res;
};
export const addSubscription = (callback) => {
if (callback) {
subscriptions.push(callback);
}
};
export const removeSubscription = (callback) => {
const index = subscriptions.findIndex((v) => v === callback);
if (index > -1) {
subscriptions.splice(index, 1);
}
};
// @/mixins/scan.js
import { addSubscription, removeSubscription } from "@/utils/scan";
export default {
data() {},
onShow() {
if (this.onReceiveCode) {
addSubscription(this.onReceiveCode);
}
},
onHide() {
if (this.onReceiveCode) {
removeSubscription(this.onReceiveCode);
}
},
beforeDestroy() {
if (this.onReceiveCode) {
removeSubscription(this.onReceiveCode);
}
},
};
0 个评论
要回复文章请先登录或注册