-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[wifi_info_flutter] Wifi plugin implementation #3143
Changes from 7 commits
58ea0ab
3232bc2
d45de16
c64daef
758fea2
ac9d686
64f8a35
0a220a7
d46a3c0
c42fe1e
b5ddd62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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:connectivity/connectivity.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). |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="io.flutter.plugins.wifi_info_flutter"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @bparrishMines |
||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> | ||
</manifest> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// 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.WifiManager; | ||
|
||
/** Reports connectivity related information such as wifi information. */ | ||
class WifiInfoFlutter { | ||
private WifiManager wifiManager; | ||
|
||
WifiInfoFlutter(WifiManager wifiManager) { | ||
this.wifiManager = wifiManager; | ||
} | ||
|
||
String getWifiName() { | ||
android.net.wifi.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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @amirh addresses #3134 (comment) |
||
return ssid; | ||
} | ||
|
||
String getWifiBSSID() { | ||
android.net.wifi.WifiInfo wifiInfo = getWifiInfo(); | ||
String bssid = null; | ||
if (wifiInfo != null) { | ||
bssid = wifiInfo.getBSSID(); | ||
} | ||
return bssid; | ||
} | ||
|
||
String getWifiIPAddress() { | ||
android.net.wifi.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 android.net.wifi.WifiInfo getWifiInfo() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bparrishMines Why not import android.net.wifi.WifiInfo ? |
||
return wifiManager == null ? null : wifiManager.getConnectionInfo(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ConnectivityMethodChannelHandler with a {@code connectivity}. The {@code | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bparrishMines It must be updated to |
||
* connectivity} 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; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
org.gradle.jvmargs=-Xmx1536M | ||
android.useAndroidX=true | ||
android.enableJetifier=true | ||
android.enableR8=true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean
var wifiBSSID = await WifiFlutter().getWifiBSSID()
without ()?