Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

[wifi_info_flutter] Wifi plugin implementation #3143

Merged
merged 11 commits into from Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 65 additions & 9 deletions packages/wifi_info_flutter/wifi_info_flutter/README.md
@@ -1,15 +1,71 @@
# wifi_info_flutter

A new flutter plugin project.
This plugin retrieves information about a device's connection to wifi.

## Getting Started
> Note that on Android, this does not guarantee connection to Internet. For instance,
the app might have wifi access but it might be a VPN or a hotel WiFi with no access.

## Usage

### Android

Sample usage to check current status:

To successfully get WiFi Name or Wi-Fi BSSID starting with Android O, ensure all of the following conditions are met:

* If your app is targeting Android 10 (API level 29) SDK or higher, your app has the ACCESS_FINE_LOCATION permission.

* If your app is targeting SDK lower than Android 10 (API level 29), your app has the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.

* Location services are enabled on the device (under Settings > Location).

You can get wi-fi related information using:

```dart
import 'package:wifi_info_flutter/wifi_info_flutter.dart';

var wifiBSSID = await WifiFlutter().getWifiBSSID();
var wifiIP = await WifiFlutter().getWifiIP();
var wifiName = await WifiFlutter().getWifiName();
```

### iOS 12

To use `.getWifiBSSID()` and `.getWifiName()` on iOS >= 12, the `Access WiFi information capability` in XCode must be enabled. Otherwise, both methods will return null.

### iOS 13

This project is a starting point for a Flutter
[plug-in package](https://flutter.dev/developing-packages/),
a specialized package that includes platform-specific implementation code for
Android and/or iOS.
The methods `.getWifiBSSID()` and `.getWifiName()` utilize the [`CNCopyCurrentNetworkInfo`](https://developer.apple.com/documentation/systemconfiguration/1614126-cncopycurrentnetworkinfo) function on iOS.

As of iOS 13, Apple announced that these APIs will no longer return valid information.
An app linked against iOS 12 or earlier receives pseudo-values such as:

* SSID: "Wi-Fi" or "WLAN" ("WLAN" will be returned for the China SKU).

* BSSID: "00:00:00:00:00:00"

An app linked against iOS 13 or later receives `null`.

The `CNCopyCurrentNetworkInfo` will work for Apps that:

* The app uses Core Location, and has the user’s authorization to use location information.

* The app uses the NEHotspotConfiguration API to configure the current Wi-Fi network.

* The app has active VPN configurations installed.

If your app falls into the last two categories, it will work as it is. If your app doesn't fall into the last two categories,
and you still need to access the wifi information, you should request user's authorization to use location information.

There is a helper method provided in this plugin to request the location authorization: `requestLocationServiceAuthorization`.
To request location authorization, make sure to add the following keys to your _Info.plist_ file, located in `<project root>/ios/Runner/Info.plist`:

* `NSLocationAlwaysAndWhenInUseUsageDescription` - describe why the app needs access to the user’s location information all the time (foreground and background). This is called _Privacy - Location Always and When In Use Usage Description_ in the visual editor.
* `NSLocationWhenInUseUsageDescription` - describe why the app needs access to the user’s location information when the app is running in the foreground. This is called _Privacy - Location When In Use Usage Description_ in the visual editor.

## Getting Started

For help getting started with Flutter, view our
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
For help getting started with Flutter, view our online
[documentation](http://flutter.io/).

For help on editing plugin code, view the [documentation](https://flutter.io/platform-plugins/#edit-code).
@@ -1,3 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.flutter.plugins.wifi_info_flutter">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @bparrishMines
To make the plugin work with Android 8.0 and Android 8.1 without additional steps, you can add that too: android.permission.CHANGE_WIFI_STATE .
https://developer.android.com/guide/topics/connectivity/wifi-scan#wifi-scan-permissions
Thank you

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> to <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> .

Thank you

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's best to leave permissions to the developer. I added the permission that was in the connectivity plugin to avoid breaking changes.

</manifest>
@@ -0,0 +1,56 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.wifi_info_flutter;

import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

/** Reports wifi information. */
class WifiInfoFlutter {
private WifiManager wifiManager;

WifiInfoFlutter(WifiManager wifiManager) {
this.wifiManager = wifiManager;
}

String getWifiName() {
final WifiInfo wifiInfo = getWifiInfo();
String ssid = null;
if (wifiInfo != null) ssid = wifiInfo.getSSID();
if (ssid != null) ssid = ssid.replaceAll("\"", ""); // Android returns "SSID"
if (ssid != null && ssid.equals("<unknown ssid>")) ssid = null;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return ssid;
}

String getWifiBSSID() {
final WifiInfo wifiInfo = getWifiInfo();
String bssid = null;
if (wifiInfo != null) {
bssid = wifiInfo.getBSSID();
}
return bssid;
}

String getWifiIPAddress() {
final WifiInfo wifiInfo = null;
if (wifiManager != null) wifiInfo = wifiManager.getConnectionInfo();

String ip = null;
int i_ip = 0;
if (wifiInfo != null) i_ip = wifiInfo.getIpAddress();

if (i_ip != 0)
ip =
String.format(
"%d.%d.%d.%d",
(i_ip & 0xff), (i_ip >> 8 & 0xff), (i_ip >> 16 & 0xff), (i_ip >> 24 & 0xff));

return ip;
}

private WifiInfo getWifiInfo() {
return wifiManager == null ? null : wifiManager.getConnectionInfo();
}
}
@@ -0,0 +1,44 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.wifi_info_flutter;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

/**
* The handler receives {@link MethodCall}s from the UIThread, gets the related information from
* a @{@link WifiInfoFlutter}, and then send the result back to the UIThread through the {@link
* MethodChannel.Result}.
*/
class WifiInfoFlutterMethodChannelHandler implements MethodChannel.MethodCallHandler {
private WifiInfoFlutter wifiInfoFlutter;

/**
* Construct the WifiInfoFlutterMethodChannelHandler with a {@code wifiInfoFlutter}. The {@code
* wifiInfoFlutter} must not be null.
*/
WifiInfoFlutterMethodChannelHandler(WifiInfoFlutter wifiInfoFlutter) {
assert (wifiInfoFlutter != null);
this.wifiInfoFlutter = wifiInfoFlutter;
}

@Override
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
switch (call.method) {
case "wifiName":
result.success(wifiInfoFlutter.getWifiName());
break;
case "wifiBSSID":
result.success(wifiInfoFlutter.getWifiBSSID());
break;
case "wifiIPAddress":
result.success(wifiInfoFlutter.getWifiIPAddress());
break;
default:
result.notImplemented();
break;
}
}
}
@@ -1,37 +1,46 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.wifi_info_flutter;

import androidx.annotation.NonNull;
import android.content.Context;
import android.net.wifi.WifiManager;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/** WifiInfoFlutterPlugin */
public class WifiInfoFlutterPlugin implements FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private MethodChannel channel;
public class WifiInfoFlutterPlugin implements FlutterPlugin {
private MethodChannel methodChannel;

@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "wifi_info_flutter");
channel.setMethodCallHandler(this);
/** Plugin registration. */
@SuppressWarnings("deprecation")
public static void registerWith(io.flutter.plugin.common.PluginRegistry.Registrar registrar) {
WifiInfoFlutterPlugin plugin = new WifiInfoFlutterPlugin();
plugin.setupChannels(registrar.messenger(), registrar.context());
}

@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
} else {
result.notImplemented();
}
public void onAttachedToEngine(FlutterPluginBinding binding) {
setupChannels(binding.getBinaryMessenger(), binding.getApplicationContext());
}

@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
public void onDetachedFromEngine(FlutterPluginBinding binding) {
methodChannel.setMethodCallHandler(null);
methodChannel = null;
}

private void setupChannels(BinaryMessenger messenger, Context context) {
methodChannel = new MethodChannel(messenger, "plugins.flutter.io/wifi_flutter_info");
final WifiManager wifiManager =
(WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

final WifiInfoFlutter wifiInfoFlutter = new WifiInfoFlutter(wifiManager);

final WifiInfoFlutterMethodChannelHandler methodChannelHandler =
new WifiInfoFlutterMethodChannelHandler(wifiInfoFlutter);
methodChannel.setMethodCallHandler(methodChannelHandler);
}
}
@@ -1,3 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
android.enableR8=true
Expand Up @@ -41,5 +41,9 @@
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app requires accessing your location information all the time to get wi-fi information.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app requires accessing your location information when the app is in foreground to get wi-fi information.</string>
</dict>
</plist>