k***@126.com
k***@126.com
  • 发布:2023-07-05 11:45
  • 更新:2023-09-05 15:35
  • 阅读:609

【报Bug】新版HBX(3.8.7.20230703)调用uni.getLocation时会触发onHide和onShow函数

分类:HBuilderX

产品分类: HbuilderX

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: Windows 10 专业版 22H2

HBuilderX版本号: 3.8.7

示例代码:
<template>  
    <view class="container">  
        <view class="one">  
            <view class="one_cont" :class="{'show': tipBlueTooth === 1}">  
                <text>蓝牙未开启</text>  
            </view>  
            <view class="one_cont" :class="{'show': tipLocation === 1}">  
                <text>位置服务未开启</text>  
            </view>  
        </view>  

        <view class="two">  
            <u-empty v-show="deviceList.length === 0" text="正在搜索..." mode="search"></u-empty>  
            <u-cell-group v-for="(item, index) in deviceList" :key="index">  
                <u-cell-item use-label-slot :index="index" @click="connect">  
                    <image slot="icon" style="width: 23px; height: 23px;" src="@/static/img/index/bluetooth.png"></image>  
                    <text slot="title">{{item.name == '' ? '未知设备' : item.name}}</text>  
                    <text slot="label">{{item.deviceId}}</text>  
                    <text slot="right-icon">{{item.RSSI}} dBm</text>  
                </u-cell-item>  
            </u-cell-group>  
        </view>  
    </view>  
</template>  

<script>  
export default {  
    data() {  
        return {  
            // 设备列表数据  
            deviceList: [],  
            // 正在连接的设备id  
            deviceId: '',  
            // 提示是否显示,0-不显示,1-显示  
            tipBlueTooth: 0,  
            tipLocation: 0,  
            // 检测是否开启蓝牙和位置服务的定时器  
            interval: null,  
            // 是否正在搜索  
            searching: false  
        };  
    },  

    // 监听页面下拉刷新  
    onPullDownRefresh() {  
        this.searching = false;  

        // 停止下拉刷新  
        setTimeout(() => {  
            uni.stopPullDownRefresh();  
        }, 500);  
    },  

    onShow() {  
        this.interval = setInterval(() => {  
            this.check();  
        }, 500);  

        let _this = this;  
        // 监听低功耗蓝牙连接状态的改变事件。包括开发者主动连接或断开连接,设备丢失,连接异常断开等等  
        uni.onBLEConnectionStateChange((res) => {  
            // 该方法回调中可以用于处理连接意外断开等异常情况  
            console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`);  
            // 更新蓝牙连接状态  
            uni.setStorageSync('blueState', res.connected);  
            if (!res.connected) {  
                // 记录设备断开连接信息  
                _this.writeLog(0);  
            }  
        });  
    },  

    onHide() {  
        // 清除定时器  
        clearInterval(this.interval);  
    },  

    methods: {  
        // 检查蓝牙和位置服务是否开启  
        check() {  
            let _this = this;  
            uni.openBluetoothAdapter({  
                success(res) {  
                    _this.tipBlueTooth = 0;  
                    uni.getLocation({  
                        type: 'wgs84',  
                        isHighAccuracy: true,  
                        success() {  
                            _this.tipLocation = 0;  

                            // 搜索蓝牙设备  
                            if (!_this.searching) {  
                                console.log('ok');  
                                _this.startBluetoothDeviceDiscovery();  
                            }  
                        },  
                        fail(err) {  
                            _this.tipLocation = 1;  
                            _this.searching = false;  
                        }  
                    });  
                },  
                fail(err) {  
                    _this.tipBlueTooth = 1;  
                    _this.searching = false;  
                    uni.getLocation({  
                        type: 'wgs84',  
                        isHighAccuracy: true,  
                        success() {  
                            _this.tipLocation = 0;  
                        },  
                        fail(err) {  
                            _this.tipLocation = 1;  
                            _this.searching = false;  
                        }  
                    });  
                }  
            });  
        },  

        // 连接蓝牙设备  
        connect(index) {  
            // 检测蓝牙与位置服务是否已开启  
            if (this.tipBlueTooth && this.tipLocation) {  
                return;  
            }  

            // 如果重新选择了蓝牙设备,则断开原连接并连接新设备  
            if (this.deviceId != null && this.deviceId != '') {  
                uni.showToast({  
                    title: '断开中...',  
                    icon: 'none',  
                    duration: 2000  
                });  
                this.closeBLEConnection();  
            }  

            // 连接蓝牙  
            let device = this.deviceList[index];  
            this.createBLEConnection(device);  
        },  

        // 根据deviceId连接蓝牙设备  
        createBLEConnection(device) {  
            console.log('连接设备信息', device);  

            // 将连接设备信息存储到Storage  
            uni.setStorageSync('device', JSON.stringify(device));  

            // data里面建立一个deviceId,存储起来  
            this.deviceId = device.deviceId;  

            uni.showToast({  
                title: '正在连接...',  
                icon: 'none',  
                duration: 2000  
            });  

            let _this = this;  
            // 连接蓝牙  
            uni.createBLEConnection({  
                // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接  
                deviceId: device.deviceId,  
                success(res) {  
                    uni.showToast({  
                        title: '蓝牙连接成功',  
                        icon: 'none',  
                        duration: 2000  
                    });  

                    // 连接设备后,停止搜索设备  
                    _this.stopBluetoothDevicesDiscovery();  

                    // 记录设备连接成功信息  
                    _this.writeLog(1);  

                    // 跳转到设备操作页,并传递连接成功参数  
                    uni.setStorageSync('blueState', true);  
                    uni.switchTab({  
                        url: '/pages/operate/operate'  
                    });  
                },  
                fail(err) {  
                    console.log('蓝牙连接失败', err);  
                    uni.showToast({  
                        title: `蓝牙连接失败`,  
                        icon: 'none',  
                        duration: 2000  
                    });  
                }  
            });  
        },  

        // 开始搜索蓝牙设备  
        startBluetoothDeviceDiscovery() {  
            // 标记正在搜索  
            this.searching = true;  
            let _this = this;  
            uni.startBluetoothDevicesDiscovery({  
                success(res) {  
                    // 监听发现的新设备  
                    _this.onBluetoothDeviceFound();  
                },  
                fail(err) {  
                    console.log('错误信息', err);  
                    uni.showToast({  
                        title: `搜索出错,${err}`,  
                        icon: 'none',  
                        duration: 2000  
                    });  
                }  
            });  
        },  

        // 监听寻找到新设备的事件  
        onBluetoothDeviceFound() {  
            let _this = this;  
            uni.onBluetoothDeviceFound((res) => {  
                console.log('发现一个设备', res.devices);  

                // 获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。  
                uni.getBluetoothDevices({  
                    success(res1) {  
                        _this.deviceList = res1.devices;  
                    }  
                });  

                console.log('所有搜索出的设备', _this.deviceList);  
            });  

        },  

        // 断开蓝牙连接  
        closeBLEConnection() {  
            uni.closeBLEConnection({  
                'deviceId': this.deviceId,  
                success(res) {  
                    console.log('断开蓝牙成功', res);  
                },  
                fail(err) {  
                    console.log('断开蓝牙失败', err);  
                    uni.showToast({  
                        title: '断开蓝牙失败',  
                        icon: 'none',  
                        duration: 2000  
                    });  
                }  
            });  
        },  

        // 停止搜寻蓝牙设备  
        stopBluetoothDevicesDiscovery() {  
            let _this = this;  
            uni.stopBluetoothDevicesDiscovery({  
                success(res) {  
                    console.log('停止搜索蓝牙设备:' + res);  
                    _this.loading = false;  
                },  
                fail(err) {  
                    console.log('停止搜索蓝牙设备失败,错误码:' + err);  
                }  
            });  
        },  

        // 记录设备连接信息  
        writeLog(msg) {  
            let logArr = uni.getStorageSync('logArr');  
            if (!logArr) {  
                logArr = [];  
            }  

            let info = {  
                'date': this.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:MM:ss'),  
                'msg': msg,  
                'show': false  
            }  
            logArr.unshift(info);  

            // 限制连接信息控制在30条之内  
            if (logArr.length > 30) {  
                logArr.pop();  
            }  
            uni.setStorageSync('logArr', logArr);  
        }  
    }  
};  
</script>  

<style lang="scss">  
.container {  

    .one {  
        display: flex;  
        flex-direction: column;  

        .one_cont {  
            width: 100%;  
            background-color: #d8453e;  
            color: #fff;  
            height: 40px;  
            display: none;  
            justify-content: center;  
            align-items: center;  
            margin-bottom: 1rpx;  
        }  

        .show {  
            display: flex;  
        }  
    }  
}  
</style>

操作步骤:

复现demo见附件test1.zip
使用新版HBuilderX版本,打开项目test1,点击运行 -> 运行到手机或模拟器 -> 运行到Android App基座 -> 运行
app授予全部权限

预期结果:

调用`uni.getLocation()不希望触发其它生命周期函数

实际结果:

调用`uni.getLocation()会一直触发onHide()和onShow()

bug描述:

使用新版HBuilderX版本,打开项目,点击运行 -> 运行到手机或模拟器 -> 运行到Android App基座 -> 运行
调用`uni.getLocation()会一直触发onHide()和onShow()

测试发现回退到HBuilderX版本3.8.4.20230531时不会出现此问题

2023-07-05 11:45 负责人:DCloud_Android_DQQ 分享
已邀请:
喜欢技术的前端

喜欢技术的前端 - QQ---445849201

这个跟HBX版本没有关系,你可以改一下逻辑,用onLoad和onUnload

    onLoad() {  
        this.interval = setInterval(() => {  
            this.check();  
        }, 500);  
        let _this = this;  
        // 监听低功耗蓝牙连接状态的改变事件。包括开发者主动连接或断开连接,设备丢失,连接异常断开等等  
        uni.onBLEConnectionStateChange((res) => {  
            // 该方法回调中可以用于处理连接意外断开等异常情况  
            console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`);  
            // 更新蓝牙连接状态  
            uni.setStorageSync('blueState', res.connected);  
            if (!res.connected) {  
                // 记录设备断开连接信息  
                _this.writeLog(0);  
            }  
        });  
    },  
    onUnload() {  
        // 清除定时器  
        clearInterval(this.interval);  
    },
  • k***@126.com (作者)

    谢谢回复!但是新版调用getLocation就是会触发onHide和onShow,老版本不会

    2023-07-05 18:41

  • 喜欢技术的前端

    回复 k***@126.com: 客气了,我试了3.8.4 也会触发

    2023-07-06 00:12

  • piaoyi_UI

    回复 k***@126.com: 什么版本不会触发呢

    2023-07-26 11:24

9***@qq.com

9***@qq.com

3.8.7 也会触发 官方咋修改的?晕死

  • 9***@qq.com

    退回上个版本 重点是重新打包 必须重新打包 可以 了

    2023-07-13 10:49

  • piaoyi_UI

    回复 9***@qq.com: 哪一个版本可以啊

    2023-07-26 10:40

1***@qq.com

1***@qq.com

3.8.7版本我也遇到了,再onShow中调用位置信息,一直触发

8***@qq.com

8***@qq.com

我现在也是这个问题,不晓得是设计如此还是BUG,旧版本不会这样,重新打包之后很多页面都不能用了,之前有在onshow中获取定位数据,就导致页面一直在onshow和onhide中死循环,郁闷死!!!!!!

7***@qq.com

7***@qq.com

3.8.7 我也遇见同样问题 只有回退到 3.8.4 和 3.8.3 重新打包测试没有这个问题

l***@goiot.net

l***@goiot.net

3.8.7我也遇到同样问题,进入app,调用getLocation会一直触发onHide和onShow,控制台猛刷打印的信息,这进入死循环了啊....,app后续直接被卡死

m***@163.com

m***@163.com

3.8.7 同问题,排查了好久 。调用摄像头扫码,触发 onshow 能理解,调用个定位 触发 onshow 不理解

DCloud_Android_DQQ

DCloud_Android_DQQ

bug 已确认

DCloud_Android_DQQ

DCloud_Android_DQQ

3.8.10 alpha bug已修复

  • piaoyi_UI

    3.8.11alpha没有解决吗,还是一直反复重启项目,app,vue里的Launch,show

    2023-07-26 10:40

piaoyi_UI

piaoyi_UI - 【插件开发】【专治疑难杂症】【多款插件已上架:https://ext.dcloud.net.cn/publisher?id=193663(微信搜索飘逸科技UI小程序直接体验)】【骗子请绕道】问题咨询请加QQ群:120594820,代表作灵感实用工具小程序

最终什么版本没有这个问题呢

1***@qq.com

1***@qq.com - 无良码

uni.getLocation({type: 'wgs84 ',success: (res) => {}})循环触发 onshow
后来改成uni.getLocation({type: 'gcj02',success: (res) => {}})3.8.7.20230703版本打包就没问题了

CyanLin

CyanLin

2023.08.21
版本3.8.12.20230817
还是有这个问题

2***@qq.com

2***@qq.com

官方什么时候能解决这个问题?

要回复问题请先登录注册