【问题标题】:connect to open wireless by using wifi_configuration Flutter使用 wifi_configuration Flutter 连接到开放无线
【发布时间】:2020-10-06 16:49:34
【问题描述】:

请问如何使用 wifi_configuration 包连接到开放的热点 - 无线 - 因为我刚刚找到了一种允许连接到加密无线网络的方法。

WifiConfiguration.connectToWifi("wirelessname","wirelesspassword","packagename");

在 WifiConfiguration 类中,只有一种方法可用于连接。 是否有任何其他库可以连接到开放热点,或者有没有办法通过使用 wifi_configuration 库来做到这一点? 苹果提到我们只需要传递两个参数,当然是 ssid 和包名

init(ssid: String)
Creates a new hotspot configuration, identified by an SSID, for an open Wi-Fi network.

我重写了 connectToWifi 方法,只接收一个参数,但这不起作用。 提前致谢

【问题讨论】:

    标签: flutter networking wireless wifi


    【解决方案1】:

    您可以在下面复制粘贴运行完整代码
    你可以使用包https://pub.dev/packages/wifi_utils
    您可以拨打Wifi.connection并提供ssidpassword
    代码sn-p

    import 'package:wifi/wifi.dart';
    ...
    Future<Null> connection() async {
        Wifi.connection(ssid, password).then((v) {
          print(v);
        });
      }
    

    工作演示

    完整代码

    import 'dart:async';
    
    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    import 'package:wifi/wifi.dart';
    
    void main() => runApp(new MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          title: 'Wifi',
          theme: new ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: new MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => new _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      String _wifiName = 'click button to get wifi ssid.';
      int level = 0;
      String _ip = 'click button to get ip.';
      List<WifiResult> ssidList = [];
      String ssid = '', password = '';
    
      @override
      void initState() {
        super.initState();
        loadData();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Wifi'),
            centerTitle: true,
          ),
          body: SafeArea(
            child: ListView.builder(
              padding: EdgeInsets.all(8.0),
              itemCount: ssidList.length + 1,
              itemBuilder: (BuildContext context, int index) {
                return itemSSID(index);
              },
            ),
          ),
        );
      }
    
      Widget itemSSID(index) {
        if (index == 0) {
          return Column(
            children: [
              Row(
                children: <Widget>[
                  RaisedButton(
                    child: Text('ssid'),
                    onPressed: _getWifiName,
                  ),
                  Offstage(
                    offstage: level == 0,
                    child: Image.asset(
                        level == 0 ? 'images/wifi1.png' : 'images/wifi$level.png',
                        width: 28,
                        height: 21),
                  ),
                  Text(_wifiName),
                ],
              ),
              Row(
                children: <Widget>[
                  RaisedButton(
                    child: Text('ip'),
                    onPressed: _getIP,
                  ),
                  Text(_ip),
                ],
              ),
              TextField(
                decoration: InputDecoration(
                  border: UnderlineInputBorder(),
                  filled: true,
                  icon: Icon(Icons.wifi),
                  hintText: 'Your wifi ssid',
                  labelText: 'ssid',
                ),
                keyboardType: TextInputType.text,
                onChanged: (value) {
                  ssid = value;
                },
              ),
              TextField(
                decoration: InputDecoration(
                  border: UnderlineInputBorder(),
                  filled: true,
                  icon: Icon(Icons.lock_outline),
                  hintText: 'Your wifi password',
                  labelText: 'password',
                ),
                keyboardType: TextInputType.text,
                onChanged: (value) {
                  password = value;
                },
              ),
              RaisedButton(
                child: Text('connection'),
                onPressed: connection,
              ),
            ],
          );
        } else {
          return Column(children: <Widget>[
            ListTile(
              leading: Image.asset('images/wifi${ssidList[index - 1].level}.png',
                  width: 28, height: 21),
              title: Text(
                ssidList[index - 1].ssid,
                style: TextStyle(
                  color: Colors.black87,
                  fontSize: 16.0,
                ),
              ),
              dense: true,
            ),
            Divider(),
          ]);
        }
      }
    
      void loadData() async {
        Wifi.list('').then((list) {
          setState(() {
            ssidList = list;
          });
        });
      }
    
      Future<Null> _getWifiName() async {
        int l = await Wifi.level;
        String wifiName = await Wifi.ssid;
        setState(() {
          level = l;
          _wifiName = wifiName;
        });
      }
    
      Future<Null> _getIP() async {
        String ip = await Wifi.ip;
        setState(() {
          _ip = ip;
        });
      }
    
      Future<Null> connection() async {
        Wifi.connection(ssid, password).then((v) {
          print(v);
        });
      }
    }
    

    【讨论】:

    • 感谢您的帮助,因为我看到此代码用于连接到加密网络而不是打开的 Wifi.connection(ssid, PASSWORD)
    猜你喜欢
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    相关资源
    最近更新 更多