简体   繁体   English

Flutter 如何查看WIFI是否开启(连接与否无所谓)?

[英]Flutter how to check if WIFI is turned on (connected or not it doesn't matter)?

How do I check if the device has it's WIFI turned on in Flutter? Flutter如何查看设备是否开启了WIFI? All that I can find is how to check if a connection is present but how should i check if the WIFI itself is turned on but not if there is connection or not?我能找到的只是如何检查是否存在连接,但我应该如何检查 WIFI 本身是否已打开,而不是是否有连接?

You can copy paste run full code below您可以在下面复制粘贴运行完整代码
You can use package https://pub.dev/packages/wifi_configuration_2您可以使用 package https://pub.dev/packages/wifi_configuration_2
It has isWifiEnabled() and checkConnection()它有isWifiEnabled()checkConnection()
Add the following permission to AndroidManifest.xml将以下权限添加到AndroidManifest.xml

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-feature android:name="android.hardware.wifi" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

code snippet代码片段

void checkConnection() async {
    wifiConfiguration.isWifiEnabled().then((value) {
      print('Is wifi enabled: ${value.toString()}');
    });

    wifiConfiguration.checkConnection().then((value) {
      print('Value: ${value.toString()}');
    });

working demo工作演示

在此处输入图像描述

full code完整代码

import 'package:flutter/material.dart';
import 'package:wifi_configuration_2/wifi_configuration_2.dart';

WifiConfiguration wifiConfiguration;

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

//enum wifiStatus {
//  conected,
//alreadyConnected,.
//notConnected ,
//platformNotSupported,
//profileAlreadyInstalled,
//
//}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  List<WifiNetwork> wifiNetworkList = List();
  bool isLoaded = false;

  @override
  void initState() {
    super.initState();
    wifiConfiguration = WifiConfiguration();
    //getConnectionState();
    checkConnection();
  }

  void getConnectionState() async {
    WifiConnectionStatus connectionStatus =
    await wifiConfiguration.connectToWifi(
        "DarkBe@rs", "DarkBe@rs", "com.example.wifi_configuration_example");
    print("is Connected : ${connectionStatus}");
//
//
    switch (connectionStatus) {
      case WifiConnectionStatus.connected:
        print("connected");
        break;

      case WifiConnectionStatus.alreadyConnected:
        print("alreadyConnected");
        break;

      case WifiConnectionStatus.notConnected:
        print("notConnected");
        break;

      case WifiConnectionStatus.platformNotSupported:
        print("platformNotSupported");
        break;

      case WifiConnectionStatus.profileAlreadyInstalled:
        print("profileAlreadyInstalled");
        break;

      case WifiConnectionStatus.locationNotAllowed:
        print("locationNotAllowed");
        break;
    }
//
//    bool isConnected = await WifiConfiguration.isConnectedToWifi("DBWSN5");
    // String connectionState = await WifiConfiguration.connectedToWifi();
    //   print("coneection status ${connectionState}");
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: wifiNetworkList.isEmpty && isLoaded
            ? Center(
          child: FlatButton(
            color: Colors.red,
            child: Text("connect"),
            onPressed: () async {
              WifiConnectionObject connectionStatus =
              await wifiConfiguration.connectedToWifi();
              print("Ip address : ${connectionStatus.ip}");
              wifiConfiguration.getConnectionType().then((value) {
                print('Connection type: ${value.toString()}');
              });
              wifiConfiguration.isConnectionFast().then((value) {
                print('Is connection fast: ${value.toString()}');
              });
            },
          ),
        )
            : Column(
          children: <Widget>[
            FlatButton(
                onPressed: () {
                  WifiConfiguration wifiConfiguration = WifiConfiguration();
                  wifiConfiguration.enableWifi();
                },
                child: Text('Wifi enable')),
            FlatButton(
                onPressed: () {
                  WifiConfiguration wifiConfiguration = WifiConfiguration();
                  wifiConfiguration.disableWifi();
                },
                child: Text('Wifi disable')),
            FlatButton(
                onPressed: () {
                  wifiConfiguration.getConnectionType().then((value) {
                    print('Connection type: ${value.toString()}');
                  });

                  wifiConfiguration.isConnectionFast().then((value) {
                    print('Connection type: ${value.toString()}');
                  });
                },
                child: Text('Print information')),
            Divider(),
            Expanded(
              child: ListView.builder(
                itemBuilder: (context, index) {
                  WifiNetwork wifiNetwork = wifiNetworkList[index];
                  return ListTile(
                    leading: Text(wifiNetwork.signalLevel),
                    title: Text(wifiNetwork.ssid),
                    subtitle: Text(wifiNetwork.bssid),
                  );
                },
                itemCount: wifiNetworkList.length,
              ),
            ),
          ],
        ),
      ),
    );
  }

  void checkConnection() async {
    wifiConfiguration.isWifiEnabled().then((value) {
      print('Is wifi enabled: ${value.toString()}');
    });

    wifiConfiguration.checkConnection().then((value) {
      print('Value: ${value.toString()}');
    });

    WifiConnectionObject wifiConnectionObject =
    await wifiConfiguration.connectedToWifi();
    if (wifiConnectionObject != null) {
      getWifiList();
    }
  }

  Future<void> getWifiList() async {
    wifiNetworkList = await wifiConfiguration.getWifiList();
    print('Network list lenght: ${wifiNetworkList.length.toString()}');
    setState(() {});
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取Flutter中当前连接的wifi的wifi名称(SSID) - How to get the wifi name(SSID) of the currently connected wifi in Flutter 如何在 Flutter 最新(3.0.1)中获取所有连接的 wifi 设备列表? - How to get list of all connected devices of wifi in Flutter Latest(3.0.1)? 如何在 Flutter 中检查 tcp 套接字是否仍然连接? - How to check if the tcp socket is still connected in Flutter? Flutter 插件:Android Studio 检测不到连接的设备 - Flutter plugin: Android studio doesn't detect connected devices Flutter/Dart:如何找到连接到本地 Wifi 网络的所有设备? - Flutter/Dart: How to find All Devices Connected to my Local Wifi Network? flutter_reactive_ble - 如何在订阅前检查设备是否已连接 - flutter_reactive_ble - how to check if device is connected before subscribing 如何将android设备连接到flutter中的vs代码以检查代码?我也打开了调试 - How to connect android devices to vs code in flutter for check the code?i turned on debugging too Flutter 如何检查是否有设备通过蓝牙连接到我的设备 - Flutter how to check if there is a device connected to my device via bluetooth Flutter 状态栏文字颜色无论如何都不会变暗 - Flutter status bar text color doesn't turn dark no matter what 使用 flutter 获取我的手机连接到的 wifi 路由器的 mac 地址 - Get mac address of the wifi router my phone is connected to, using flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM