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
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
58ea0ab
copy paste from connectivity
bparrishMines 3232bc2
copy wifi code to plugin
bparrishMines d45de16
add test dependency and temporary publish to none
bparrishMines c64daef
return null when ssid unknown
bparrishMines 758fea2
Merge branch 'master' of github.com:flutter/plugins into wifi_plugin
bparrishMines ac9d686
update pubspec
bparrishMines 64f8a35
update README
bparrishMines 0a220a7
Fix licenses
bparrishMines d46a3c0
fix documentation and comments
bparrishMines c42fe1e
remove final
bparrishMines b5ddd62
remove additional access wifi state
bparrishMines File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: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). |
2 changes: 2 additions & 0 deletions
2
packages/wifi_info_flutter/wifi_info_flutter/android/src/main/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="io.flutter.plugins.wifi_info_flutter"> | ||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> | ||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> | ||
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. Please change Thank you 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. I believe it's best to leave permissions to the developer. I added the permission that was in the |
||
</manifest> |
56 changes: 56 additions & 0 deletions
56
...o_flutter/android/src/main/java/io/flutter/plugins/wifi_info_flutter/WifiInfoFlutter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
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() { | ||
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(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...c/main/java/io/flutter/plugins/wifi_info_flutter/WifiInfoFlutterMethodChannelHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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; | ||
} | ||
} | ||
} |
53 changes: 31 additions & 22 deletions
53
...ter/android/src/main/java/io/flutter/plugins/wifi_info_flutter/WifiInfoFlutterPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/wifi_info_flutter/wifi_info_flutter/example/android/gradle.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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