How to get the wifi name(SSID) of the currently connected wifi in Flutter

时光总嘲笑我的痴心妄想 提交于 2019-12-11 13:15:36

问题


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')),
    );
  }
}

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".


回答1:


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

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

EDIT: 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.

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

For instance, you have your project at /users/projects/myProject.

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

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

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

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

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.



来源:https://stackoverflow.com/questions/52498906/how-to-get-the-wifi-namessid-of-the-currently-connected-wifi-in-flutter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!