1***@qq.com
1***@qq.com
  • 发布:2024-10-06 12:37
  • 更新:2024-10-06 12:37
  • 阅读:13

【报Bug】 android13 plus.android.importClass("android.content.Context"); 报错

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: 11

HBuilderX类型: 正式

HBuilderX版本号: 4.08

手机系统: Android

手机系统版本号: Android 13

手机机型: RT800

页面类型: vue

vue版本: vue2

打包方式: 离线

项目创建方式: HBuilderX

示例代码:
<template> <view> <!-- <view style="text-align: center;margin-bottom: 20rpx;font-size: 12px;"> 已配对蓝牙设备 </view> --> <view style="text-align: center;padding: 10rpx 0;border: 1rpx solid #2B85E4;border-radius: 15rpx;" class="uni-flex uni-row" v-if="deviceList && deviceList.length>0" v-for="(device,index) in deviceList"

key="index"> <view style="width: 30rpx;">{{index+1}}</view>
<view style="width: 180rpx;">{{device.name}}</view>
<view style="width: 310rpx;">{{device.address}}</view>
<view class="flex-item"><button type="primary" plain size="mini" @click="print()">打印测试</button></view>

    <view class="flex-item"><button type="warn" plain size="mini"  
            @click="connectBlue(device)">{{curBlue==device.address?"已连接":"连接蓝牙"}}</button></view>  

</view>  

</view>

</template>

<script>
var tsc = require('../../components/gprint/tsc.js')

var androidBlue = require('../../components/gprint/androidBluetooth.js')  

export default {  
    data() {  
        return {  
            curBlue: "",  
            show: {  
                setting: false  
            },  
            deviceList: []  
        }  
    },  
    onShow() {  

    },  
    onLoad() {  
        var that = this;  
        // #ifdef APP-PLUS  
        that.initPrinter();  
        // #endif  

        uni.getStorage({  
            key: 'myAndroidLanya',  
            success: function(res) {  

                console.log(111111111, res.data)  
                that.curBlue = res.data;  
            },  
            fail(e) {  
                uni.showToast({  
                    title: '未连接蓝牙打印机',  
                    duration: 2000,  
                    icon: "none"  
                })  
            }  
        });  
    },  
    methods: {  
        connectBlue(device) {  
            let dev = device  
            let that = this;  
            uni.setStorage({  
                key: 'myAndroidLanya',  
                data: dev.address  
            });  
            uni.setStorage({  
                key: 'myAndroidLanyaName',  
                data: dev.name  
            });  
            this.curBlue = device.address  

            uni.showToast({  
                title: '连接成功',  
                icon: 'success',  
                duration: 2000  
            });  
            this.$forceUpdate()  
        },  

        initPrinter: function() {  
            var that = this;  
            that.deviceList = [];  
            var main = plus.android.runtimeMainActivity();  
            var Context = plus.android.importClass("android.content.Context");  
            var BManager = main.getSystemService(Context.BLUETOOTH_SERVICE);  
            plus.android.importClass(BManager);  
            var BAdapter = BManager.getAdapter();  
            plus.android.importClass(BAdapter);  
            var lists = BAdapter.getBondedDevices();  
            plus.android.importClass(lists);  
            var iterator = lists.iterator();  
            plus.android.importClass(iterator);  
            while (iterator.hasNext()) {  
                var d = iterator.next();  
                plus.android.importClass(d);  
                var temp = {  
                    name: d.getName(),  
                    address: d.getAddress(),  
                    status: d.getBondState(),  
                    uuids: d.getUuids(),  
                    op: d  
                };  
                that.deviceList.push(temp);  
            }  

            console.log(that.deviceList, 44444444444444)  
        },  

        print() {  
            let command = this.getCommand()  

            console.log(111111111, androidBlue, command)  
            androidBlue.androidBlue.printToLanya(command)  
        },  

        printSomething(dev, print) {  
            var that = this;  
            var main = plus.android.runtimeMainActivity();  

            var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");  
            var UUID = plus.android.importClass("java.util.UUID");  
            var uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");  
            var BAdapter = BluetoothAdapter.getDefaultAdapter();  
            var device = BAdapter.getRemoteDevice(dev.address);  
            plus.android.importClass(device);  
            var bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);  
            plus.android.importClass(bluetoothSocket);  
            console.log("开始连接打印机:" + dev.name);  
            if (!bluetoothSocket.isConnected()) {  
                bluetoothSocket.connect();  
                if (bluetoothSocket.isConnected()) {  
                    console.log("设备已连接,开始发送打印文件");  
                    var outputStream = bluetoothSocket.getOutputStream();  
                    plus.android.importClass(outputStream);  
                    print(outputStream);  
                    bluetoothSocket.close();  
                    if (!bluetoothSocket.isConnected()) {  
                        console.log("设备已关闭");  
                    }  
                } else {  
                    uni.showToast({  
                        title: '设备连接失败',  
                        icon: 'error',  
                        duration: 2000  
                    });  
                }  
            }  
        },  
        getCommand() {  
            var command = tsc.jpPrinter.createNew();  

            command.setCls() //清除缓冲区,防止下一个没生效  
            command.setSize(100, 50) //设置标签大小,单位mm.具体参数请用尺子量一下  
            command.setGap(2) //设置两个标签之间的间隙,单位mm.具体参数请用尺子量一下  
            command.setCls() //清除缓冲区  
            // command.setBox(10, 10, 100, 100, 5) //绘制一个边框  

            let x = 12;  
            let y = 12;  
            let lineHeight = 60;  
            let curLine = 0;  
            let font = "TSS24.BF2"  
            let fangdax = 2;  
            let fangday = 2  

            command.setText(x, y + lineHeight * curLine, font, 0, fangdax, fangday, "名称:") //绘制文字  

            curLine++;  
            command.setText(x, y + lineHeight * curLine, font, 0, fangdax, fangday, "批号:") //绘制文字  

            curLine++;  
            command.setText(x, y + lineHeight * curLine, font, 0, fangdax, fangday, "人员:") //绘制文字  

            curLine++;  
            command.setText(x, y + lineHeight * curLine, font, 0, fangdax, fangday, "日期") //绘制文字  

            // curLine++;  

            // command.setText(x, y + lineHeight * curLine, "TSS24.BF2", 0, 1, 1, "数量:") //绘制文字  
            // command.setText(x + 80, y + lineHeight * curLine, "TSS24.BF2", 0, 1, 1, item.createFqty) //绘制文字  

            // command.setText(x + 120, y + lineHeight * curLine, "TSS24.BF2", 0, 1, 1, "单位:") //绘制文字  
            // command.setText(x + 200, y + lineHeight * curLine, "TSS24.BF2", 0, 1, 1, item.unitName) //绘制文字  

            // command.setText(1, 275, "TSS24.BF2", 0, 1, 1, "条码号:") //绘制文字  
            // command.setText(110, 275, "TSS24.BF2", 0, 1, 1, item.barCode) //绘制文字  
            // command.setQrcode(x + 200, y + 40, "L", 5, 'M', barcodeInfo.barCode) //绘制二维码  
            command.setPagePrint() //执行打印指令  
            return command;  
        },  

        mySleep: async function(time) {  
            await this.mypromise(time);  
        },  
        mypromise: function(time) {  
            return new Promise((resolve) => setTimeout(resolve, time));  
        }  
    }  
}  

</script>

<style >
/* page {
padding: 0;
margin: 0;
}

.picked {  
    background-color: lavender;  
} */  

</style>

操作步骤:

打开当前页面即报错。执行到
plus.android.importClass("android.content.Context");

预期结果:

正常获取已保存的蓝牙列表

实际结果:

报错

bug描述:

调用原生蓝牙 进行蓝牙打印,,在android 8 上正常运行,在android13上 报错

TypeError: Cannot read property 'TYPE' of null

报错行 : plus.android.importClass("android.content.Context");
代码如下

var main = plus.android.runtimeMainActivity();  
                var Context = plus.android.importClass("android.content.Context");  
                var BManager = main.getSystemService(Context.BLUETOOTH_SERVICE);  
                plus.android.importClass(BManager);  
                var BAdapter = BManager.getAdapter();  
                plus.android.importClass(BAdapter);  
                var lists = BAdapter.getBondedDevices();  
                plus.android.importClass(lists);  
                var iterator = lists.iterator();  
                plus.android.importClass(iterator);  
                while (iterator.hasNext()) {  
                    var d = iterator.next();  
                    plus.android.importClass(d);  
                    var temp = {  
                        name: d.getName(),  
                        address: d.getAddress(),  
                        status: d.getBondState(),  
                        uuids: d.getUuids(),  
                        op: d  
                    };  
                    that.deviceList.push(temp);  
                }
2024-10-06 12:37 负责人:无 分享
已邀请:

要回复问题请先登录注册