s***@ej-sh.com
s***@ej-sh.com
  • 发布:2020-03-31 15:16
  • 更新:2020-03-31 15:16
  • 阅读:2810

UNIAPP连结并调用蓝牙打印机问题

分类:uni-app

1、硬件环境:华为P20二台,华为荣耀青春版1台,华为NOVA一台

2、UNIAPP的代码如下

3、操作流程及思路,通过一个专门的程序,获取蓝牙的ID,特征编号,存起来

4、程序进入时,直接调用编号并连结

5、问题描述:
手机重启后,蓝牙无法自动连结(必须重新配对,重新获取设备ID等等,再用以下代码,反复很多次,才能连上),连上后关闭,打开程序,均无问题。
但其中一台P20,则无此问题,重启,关程序,只要重开后约30秒,均会 自动连结好,另一台P20则不行,二台P20的版本,型号,内存大小一模一样,要把人折磨得发疯啊。

    onLoad: function() {  
        let self = this;  
        self.deviceId = uni.getStorageSync('blue_deviceId');  
        if(self.deviceId != ''){  
            uni.openBluetoothAdapter({  
                success(res) {  
                    console.log('onLoad openBluetoothAdapter success......' + JSON.stringify(res));  
                    self.boolconnect = true;  
                    self.serviceId = uni.getStorageSync('blue_serviceId');  
                    self.characteristicId = uni.getStorageSync('blue_characteristicId');  
                    uni.createBLEConnection({  
                        timeout: 3000,  
                        deviceId: self.deviceId,  
                        success(res){  
                            console.log('onLoad createBLEConnection success......' + JSON.stringify(res));  
                        },  
                        fail(res){  
                            console.log('onLoad createBLEConnection fail......' + JSON.stringify(res));  
                        }  
                    });  
                    uni.onBLEConnectionStateChange(function (res) {  
                        self.booldevice = res.connected;  
                        if(!self.booldevice){  

                        }  
                        var dat = (res.connected == true ? '成功' : '失败');  
                        var msg = "蓝牙设备连结提示......" + dat;  
                        self.$tip(msg);  
                        console.log(msg);  
                    })  
                },  
                fail: function (res) {  
                    self.$tip("onLoad蓝牙设备打开失败......" + JSON.stringify(res));  
                }  
            })  
        }else{  
            self.$tip("onLoad蓝牙没有定义deviceId设备,请使用使用系统配置进行蓝牙设备确认")  
        }  
    },
2020-03-31 15:16 负责人:无 分享
已邀请:

该问题目前已经被锁定, 无法添加新回复