微信小程序一键链接WiFi

2023-02-20 15:19:16 浏览数 (1)

点击一键连接按钮,执行connectWifi 方法;

accountNumber: ”, //Wi-Fi 的SSID,即账号

bssid: ”, //Wi-Fi 的ISSID

password: ” //Wi-Fi 的密码

代码语言:javascript复制
connectWifi: function() {
            var that = this;
            //检测手机型号
            wx.getSystemInfo({
                success: function(res) {
                    var system = '';
                    if (res.platform == 'android') system = parseInt(res.system.substr(8));
                    if (res.platform == 'ios') system = parseInt(res.system.substr(4));
                    if (res.platform == 'android' && system < 6) {
                        wx.showToast({
                            title: '手机版本不支持'
                        });
                        return;
                    }
                    if (res.platform == 'ios' && system < 11.2) {
                        wx.showToast({
                            title: '手机版本不支持'
                        });
                        return;
                    }
                    //2.初始化 Wi-Fi 模块
                    that.startWifi();
                }
            });
        },
        //初始化 Wi-Fi 模块
        startWifi: function() {
            var that = this;
            wx.startWifi({
                success: function() {
                    //请求成功连接Wifi
                    that.Connected();
                },
                fail: function(res) {
                    wx.showToast({
                        title: '接口调用失败'
                    });
                }
            });
        },

        Connected: function() {
            var that = this;
            wx.connectWifi({
                SSID: that.data.accountNumber,
                BSSID: that.data.bssid,
                password: that.data.password,
                success: function(res) {
                    wx.showToast({
                        title: 'wifi连接成功'
                    });
                },
                fail: function(res) {
                    wx.showToast({
                        title: 'wifi连接失败'
                    });
                }
            });
        },

未经允许不得转载:肥猫博客 » 微信小程序一键链接WiFi

0 人点赞