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

3

9

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

Reams answered 25/9, 2018 at 13:4 Comment(0)
R
10

It's just calling getWifiName(), available in the network_info_plus plugin. This method used to be available in the connectivity plugin, but it has been moved to this new plugin later.

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

Rawdan answered 25/9, 2018 at 20:38 Comment(15)
You can also use the GIT url directly in a pubspec.yaml.Triviality
Thanks a lot. It's working fine. I have only checked for android device for now. I hope this works fine on ios too.Reams
I would also love to know how can I add the git url directly as Randal mentioned.Reams
Ah, here's how to add directly from git as @RandalSchwartz mentioned. I saw it once but totally forgot. You would have to use git: url: [email protected]:flutter/plugins.git and path: packages/connectivity.Rawdan
So I've gotten this to work in android, but in ios i'm getting an exception PlatformException(UNAVAILABLE, wifi name unavailable, null). Permission problem?Minorca
@Rob C, Yes, I think it deserves a new question! :)Rawdan
Done, thanks for the suggestion. For reference: #55717251Minorca
I always receive <unknown ssid> when I called getWifiName.Nay
@Dhaval Kansara, is it iOS? Did you check https://mcmap.net/q/1176955/-flutter-ios-reading-wifi-name-using-the-connectivity-or-wifi-plugin ?Rawdan
@Rawdan Yes, I have already added Access Wifi Information for my project.Nay
The BSSID may be null, if there is no network currently connected. "02:00:00:00:00:00", if the caller has insufficient permissions to access the BSSID. found this from android developer site but don't know how to add permissonsFaggoting
@Rawdan I am also using connectivity plugin to get wifi ssid for IOS with the method connectivity.getWifiName(), but it returns null, but I can get IP with getWifiIP() method. What could be the problem?Earth
@Rawdan this is my attempt #63858289Earth
This is no longer null safeKwh
Anything for web?!Calices
U
5

In 2022, I have switched to network_info_plus library, in my case this new version is better than the old one. And it still uses the same method names as the old version to get the name of wifi: getWifiName().

Unconditioned answered 20/4, 2022 at 9:7 Comment(0)
N
1

Use network_info_plus package.

Add this code to android manifest:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Future<void> setNetwork() async {
final info = NetworkInfo();
var locationStatus = await Permission.location.status;
if (locationStatus.isDenied) {
  await Permission.locationWhenInUse.request();
}
if (await Permission.location.isRestricted) {
  openAppSettings();
}

if (await Permission.location.isGranted) {
  var wifiName = await info.getWifiName();
  log('wifiName $wifiName');
  }
}
Neolamarckism answered 4/10, 2022 at 15:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.