如何在Flutter中获取当前连接的wifi的wifi名称(SSID) [英] How to get the wifi name(SSID) of the currently connected wifi in Flutter

查看:2533
本文介绍了如何在Flutter中获取当前连接的wifi的wifi名称(SSID)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助此连接插件,我可以获取连接状态,即移动网络,wifi或不使用以下代码:

With the help of this Connectivity Plugin, I am able to get the connection status i.e. mobile network, wifi or none using the following code:

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:connectivity/connectivity.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _connectionStatus = 'Unknown';
  final Connectivity _connectivity = new Connectivity();
  StreamSubscription<ConnectivityResult> _connectivitySubscription;

  @override
  void initState() {
    super.initState();
    initConnectivity();
    _connectivitySubscription =
        _connectivity.onConnectivityChanged.listen((ConnectivityResult result) {
      setState(() => _connectionStatus = result.toString());
    });
  }

  @override
  void dispose() {
    _connectivitySubscription.cancel();
    super.dispose();
  }


  Future<Null> initConnectivity() async {
    String connectionStatus;

    try {
      connectionStatus = (await _connectivity.checkConnectivity()).toString();
    } on PlatformException catch (e) {
      print(e.toString());
      connectionStatus = 'Failed to get connectivity.';
    }


    if (!mounted) {
      return;
    }

    setState(() {
      _connectionStatus = connectionStatus;
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: const Text('Plugin example app'),
      ),
      body: new Center(
          child: new Text('Connection Status: $_connectionStatus\n')),
    );
  }
}

现在,我要在手机连接到wifi时获取Wifi的名称. 详细说明:假设用户已将其电话连接到名为"Home Wifi"的wifi,根据我写的代码,无论手机是否连接到wifi,我都只能得到,我也想获取名称如果手机已连接到wifi,即家用Wifi",则为wifi.

Now what I want is to get the name of the Wifi when the phone is connected to wifi. Detailed Description: Suppose the user has connected his/her phone with a wifi named "Home Wifi", from the code I have wriiten I am only able to get if the phone is connected to wifi or not, I also want to get the name of the wifi if the phone is connected to the wifi i.e. "Home Wifi".

推荐答案

它只是调用连接插件0.3.2版本上可用.

It's just calling getWifiName(). It's available on the 0.3.2 version of the connectivity plugin.

在iOS中,使用此解决方案需要此答案中所述的步骤.

In iOS, using this solution requires the steps described in this answer.

编辑:版本0.3.2已发布.

version 0.3.2 is published.

问题在于,尽管代码已经合并,但他们尚未发布新版本的插件.

The problem is that, although the code is already merged, they haven't published the new version of plugin yet.

现在,您可以执行以下操作:将 flutter/plugins 存储库克隆到您的计算机,然后使用路径引用来引用连接程序包.

For now, you can do the following: clone the flutter/plugins repository to your machine and reference the connectivity package using a path reference.

例如,您的项目位于/users/projects/myProject.

然后将flutter/plugins克隆到/users/projects/plugins.

Then you clone flutter/plugins to /users/projects/plugins.

git clone git@github.com:flutter/plugins.git plugins

然后,在您的pubspec.yaml中,将连接插件更改为:

Then, in your pubspec.yaml, you change the connectivity plugin to that:

  # connectivity: ^0.3.1
  connectivity:
    path: ../plugins/packages/firebase_messaging

它将在本地运行.您可以打开一个问题,要求他们发布该问题,当他们这样做时,您将可以使用版本0.3.2.

And it will run locally. You can open an issue requesting that they publish it, when they do it, you will be able to use version 0.3.2.

这篇关于如何在Flutter中获取当前连接的wifi的wifi名称(SSID)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆