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

[wifi_info_flutter] Check Permissions in Android O or higher #3234

Merged
merged 13 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/wifi_info_flutter/wifi_info_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.0.4

* Android: Add Log warning for unsatisfied requirement(s) in Android P or higher.
* Android: Update Example project.

## 1.0.3

* Fix README example.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,31 @@

package io.flutter.plugins.wifi_info_flutter;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import androidx.core.content.ContextCompat;
import io.flutter.Log;

/** Reports wifi information. */
class WifiInfoFlutter {
private WifiManager wifiManager;
private Context context;
private static final String TAG = "WifiInfoFlutter";

WifiInfoFlutter(WifiManager wifiManager) {
WifiInfoFlutter(WifiManager wifiManager, Context context) {
this.wifiManager = wifiManager;
this.context = context;
}

String getWifiName() {
if (!checkPermissions()) {
return null;
}
final WifiInfo wifiInfo = getWifiInfo();
String ssid = null;
if (wifiInfo != null) ssid = wifiInfo.getSSID();
Expand All @@ -25,6 +38,9 @@ String getWifiName() {
}

String getWifiBSSID() {
if (!checkPermissions()) {
return null;
}
final WifiInfo wifiInfo = getWifiInfo();
String bssid = null;
if (wifiInfo != null) {
Expand Down Expand Up @@ -53,4 +69,89 @@ String getWifiIPAddress() {
private WifiInfo getWifiInfo() {
return wifiManager == null ? null : wifiManager.getConnectionInfo();
}

private Boolean checkPermissions() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return true;
}

boolean grantedChangeWifiState =
ContextCompat.checkSelfPermission(context, Manifest.permission.CHANGE_WIFI_STATE)
== PackageManager.PERMISSION_GRANTED;

boolean grantedAccessFine =
ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED;

boolean grantedAccessCoarse =
ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)
== PackageManager.PERMISSION_GRANTED;

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P
&& !grantedChangeWifiState
&& !grantedAccessFine
&& !grantedAccessCoarse) {
Log.w(
TAG,
"Attempted to get Wi-Fi data that requires additional permission(s).\n"
+ "To successfully get WiFi Name or Wi-Fi BSSID starting with Android O, please ensure your app has one of the following permissions:\n"
+ "- CHANGE_WIFI_STATE\n"
+ "- ACCESS_FINE_LOCATION\n"
+ "- ACCESS_COARSE_LOCATION\n"
+ "For more information about Wi-Fi Restrictions in Android 8.0 and above, please consult the following link:\n"
+ "https://developer.android.com/guide/topics/connectivity/wifi-scan");
return false;
}

if (Build.VERSION.SDK_INT == Build.VERSION_CODES.P && !grantedChangeWifiState) {
Log.w(
TAG,
"Attempted to get Wi-Fi data that requires additional permission(s).\n"
+ "To successfully get WiFi Name or Wi-Fi BSSID starting with Android P, please ensure your app has the CHANGE_WIFI_STATE permission.\n"
+ "For more information about Wi-Fi Restrictions in Android 9.0 and above, please consult the following link:\n"
+ "https://developer.android.com/guide/topics/connectivity/wifi-scan");
return false;
}

if (Build.VERSION.SDK_INT == Build.VERSION_CODES.P
&& !grantedAccessFine
&& !grantedAccessCoarse) {
Log.w(
TAG,
"Attempted to get Wi-Fi data that requires additional permission(s).\n"
+ "To successfully get WiFi Name or Wi-Fi BSSID starting with Android P, additional to CHANGE_WIFI_STATE please ensure your app has one of the following permissions too:\n"
+ "- ACCESS_FINE_LOCATION\n"
+ "- ACCESS_COARSE_LOCATION\n"
+ "For more information about Wi-Fi Restrictions in Android 9.0 and above, please consult the following link:\n"
+ "https://developer.android.com/guide/topics/connectivity/wifi-scan");
return false;
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q
&& (!grantedAccessFine || !grantedChangeWifiState)) {
Log.w(
TAG,
"Attempted to get Wi-Fi data that requires additional permission(s).\n"
+ "To successfully get WiFi Name or Wi-Fi BSSID starting with Android Q, please ensure your app has the CHANGE_WIFI_STATE and ACCESS_FINE_LOCATION permission.\n"
+ "For more information about Wi-Fi Restrictions in Android 10.0 and above, please consult the following link:\n"
+ "https://developer.android.com/guide/topics/connectivity/wifi-scan");
return false;
}

LocationManager locationManager =
(LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && !gpsEnabled) {
Log.w(
TAG,
"Attempted to get Wi-Fi data that requires additional permission(s).\n"
+ "To successfully get WiFi Name or Wi-Fi BSSID starting with Android P, please ensure Location services are enabled on the device (under Settings > Location).\n"
+ "For more information about Wi-Fi Restrictions in Android 9.0 and above, please consult the following link:\n"
+ "https://developer.android.com/guide/topics/connectivity/wifi-scan");
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private void setupChannels(BinaryMessenger messenger, Context context) {
final WifiManager wifiManager =
(WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

final WifiInfoFlutter wifiInfoFlutter = new WifiInfoFlutter(wifiManager);
final WifiInfoFlutter wifiInfoFlutter = new WifiInfoFlutter(wifiManager, context);

final WifiInfoFlutterMethodChannelHandler methodChannelHandler =
new WifiInfoFlutterMethodChannelHandler(wifiInfoFlutter);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.flutter.plugins.wifi_info_flutter_example">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

<application
android:label="wifi_info_flutter_example"
android:icon="@mipmap/ic_launcher">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ class _MyHomePageState extends State<MyHomePage> {
}
if (status == LocationAuthorizationStatus.authorizedAlways ||
status == LocationAuthorizationStatus.authorizedWhenInUse) {
wifiName = await _connectivity.getWifiName();
wifiName = await _wifiInfo.getWifiName();
} else {
wifiName = await _connectivity.getWifiName();
wifiName = await _wifiInfo.getWifiName();
}
} else {
wifiName = await _connectivity.getWifiName();
wifiName = await _wifiInfo.getWifiName();
}
} on PlatformException catch (e) {
print(e.toString());
Expand All @@ -135,20 +135,20 @@ class _MyHomePageState extends State<MyHomePage> {
}
if (status == LocationAuthorizationStatus.authorizedAlways ||
status == LocationAuthorizationStatus.authorizedWhenInUse) {
wifiBSSID = await _connectivity.getWifiBSSID();
wifiBSSID = await _wifiInfo.getWifiBSSID();
} else {
wifiBSSID = await _connectivity.getWifiBSSID();
wifiBSSID = await _wifiInfo.getWifiBSSID();
}
} else {
wifiBSSID = await _connectivity.getWifiBSSID();
wifiBSSID = await _wifiInfo.getWifiBSSID();
}
} on PlatformException catch (e) {
print(e.toString());
wifiBSSID = "Failed to get Wifi BSSID";
}

try {
wifiIP = await _connectivity.getWifiIP();
wifiIP = await _wifiInfo.getWifiIP();
} on PlatformException catch (e) {
print(e.toString());
wifiIP = "Failed to get Wifi IP";
Expand Down
2 changes: 1 addition & 1 deletion packages/wifi_info_flutter/wifi_info_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: wifi_info_flutter
description: A new flutter plugin project.
version: 1.0.3
version: 1.0.4
homepage: https://github.com/flutter/plugins/tree/master/packages/wifi_info_flutter/wifi_info_flutter

environment:
Expand Down