uniapp one-click link to specify WiFi function

Here I wrote the loading component myself, which can be ignored.

Note: You must first delete the forgotten WiFi, open the WiFi module and click it to take effect, otherwise an error will be reported.

Basic process: first determine the mobile phone model. After passing it, initialize the WiFi module that.start_wifi (name it as you like), call start_wifi and use the uni.startWifi initialization module inside to see if it is successful. After success, use this.connected (name it as you like) and call connected. Use uni.connectWifi inside (connect to Wi-Fi. If the Wi-Fi information is known, you can directly use this interface to connect).

<template>
    <view class="content">
        <view @click="content_wf">一键联网</view>
        <Loading :loading='loading'></Loading>
    </view>
</template>

<script>
    import Loading from '../loading/index.nvue'
    export default {
        data() {
            return {

                loading: true,
            }
        },
        onLoad() {
            console.log(this.loading);
            this.loadingTimeout()

        },
        components: {
            Loading
        },
        methods: {
            loadingTimeout() {
                setTimeout(() => {
                    this.loading = false;
                    console.log(this.loading);
                }, 2000);
                console.log(this.loading);
            },
            content_wf() {
                this.loading = true
                console.log(this.loading);
                // 获取手机型号

                let that = this
                console.log(that);
                uni.getSystemInfo({
                    success: (res) => {
                        let system = ''
                        console.log("当前手机型号===>", res)
                        if (res.platform == 'android') {
                            system = parseInt(res.platform.substr(8))
                        }
                        if (res.platform == 'ios') {
                            system = parseInt(res.platform.substr(4))
                        }
                        if (res.platform == 'android' && system < 6) {
                            uni.showToast({
                                title: '手机版本不支持',
                                icon: 'none'
                            })
                            return
                        }
                        if (res.platform == 'ios' && system < 11.2) {
                            uni.showToast({
                                title: '手机版本不支持',
                                icon: 'none'
                            })
                            return
                        }
                        //初始wifi模块
                        that.start_wifi()
                    }
                })
            },
            start_wifi() {
                let that = this
                console.log(that);
                uni.startWifi({
                    success: (res) => {
                        console.log("startWifi==>", res)
                        that.connected()
                    },
                    fail: (error) => {
                        console.log(error)
                        uni.showToast({
                            title: '链接失败',
                            icon: 'error'
                        })
                        this.loading = false
                    }
                })
            },

            connected() {
                let that = this
                // console.log(that);
                uni.connectWifi({
                    SSID: '1901', //Wi-Fi 设备名称
                    password: 'fang12356', //Wi-Fi 密码
                    success: (res) => {
                        console.log(res);
                        uni.showToast({
                            title: "链接成功",
                        })
                        this.loading = false
                    },
                    fail: (error) => {
                        uni.showToast({
                            title: '链接失败',
                            icon: 'error',
                        })
                        this.loading = false
                    }
                })
            }
        }
    }
</script>

Guess you like

Origin blog.csdn.net/m0_45865109/article/details/129129015