如何在Flutter中获取当前连接的wifi的wifi名称(SSID)

时间:2018-09-25 13:04:13

标签: flutter wifi ssid

借助此https://webdev.dartlang.org/articles/performance/event-loop,我可以使用以下代码获取连接状态,即移动网络,WiFi或无连接状态:

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。

1 个答案:

答案 0 :(得分:1)

它只是在呼叫getWifiName()。在connectivity plugindiv::after { height: 100%; display: flex; align-items: center; justify-content: center; } 版本上可用。

在iOS中,使用此解决方案需要执行步骤described in this answer

编辑:版本0.3.2已发布。

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

现在,您可以执行以下操作:将flutter/plugins存储库克隆到您的计算机上,并使用路径引用来引用连接软件包。

例如,您的项目在0.3.2

然后,将flutter / plugins克隆到/users/projects/myProject

/users/projects/plugins

然后,在您的git clone git@github.com:flutter/plugins.git plugins中,将连接插件更改为:

pubspec.yaml

,它将在本地运行。您可以打开一个问题,要求他们发布该问题,当他们这样做时,您将可以使用版本 # connectivity: ^0.3.1 connectivity: path: ../plugins/packages/firebase_messaging