Skip to content
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
2 changes: 2 additions & 0 deletions docs/network_info_plus/usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ To successfully get WiFi Name or Wi-Fi BSSID starting with Android O, ensure all

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

* If you use device with Android 12 (API level 31) and newer be sure that your app has ACCESS_NETWORK_STATE permission.
Comment thread
miquelbeltran marked this conversation as resolved.

**This package does not provide the ACCESS_FINE_LOCATION nor the ACCESS_COARSE_LOCATION permission by default**

### iOS 12
Expand Down
12 changes: 8 additions & 4 deletions packages/network_info_plus/network_info_plus/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.3.0

- Android: Use new APIs to get network info on devices with with Android 12 (SDK 31) and newer. Due
to this change on such devices be sure to add `ACCESS_NETWORK_STATE` permission into your
`AndroidManifest.xml` file.

## 2.2.0

- Android: Migrate to Kotlin
Expand All @@ -14,7 +20,6 @@

- Android: Plugin no longer removes `"` from SSID name.


## 2.1.3

- Update nm dependency to be compatible with fresh versions of other Plus plugins
Expand Down Expand Up @@ -58,7 +63,6 @@

- migrate integration_test to flutter sdk


## 1.1.0

- Android, IOS: Adding IPv6 information
Expand Down Expand Up @@ -88,8 +92,8 @@

## 0.1.1

- Added a stub plugin for Web that throws unsupported exceptions, because
the functionality is not supported on Web.
- Added a stub plugin for Web that throws unsupported exceptions, because the functionality is not
supported on Web.

## 0.1.0

Expand Down
4 changes: 3 additions & 1 deletion packages/network_info_plus/network_info_plus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import 'package:network_info_plus/network_info_plus.dart';

final info = NetworkInfo();

var wifiName = await info.getWifiName(); // FooNetwork
var wifiName = await info.getWifiName(); // "FooNetwork"
var wifiBSSID = await info.getWifiBSSID(); // 11:22:33:44:55:66
var wifiIP = await info.getWifiIP(); // 192.168.1.43
var wifiIPv6 = await info.getWifiIPv6(); // 2001:0db8:85a3:0000:0000:8a2e:0370:7334
Expand All @@ -49,6 +49,8 @@ To successfully get WiFi Name or Wi-Fi BSSID starting with Android 1O, ensure al

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

- If you use device with Android 12 (API level 31) and newer be sure that your app has ACCESS_NETWORK_STATE permission.

**This package does not provide the ACCESS_FINE_LOCATION nor the ACCESS_COARSE_LOCATION permission by default**

### iOS 12
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,49 @@
package dev.fluttercommunity.plus.network_info

import android.net.ConnectivityManager
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import android.os.Build
import java.net.*

/** Reports network info such as wifi name and address. */
internal class NetworkInfo(private val wifiManager: WifiManager?) {
internal class NetworkInfo(private val wifiManager: WifiManager?,
private val connectivityManager: ConnectivityManager? = null
) {

private val wifiInfo: WifiInfo?
get() = wifiManager?.connectionInfo
get() =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val currentNetwork = connectivityManager?.activeNetwork
connectivityManager?.getNetworkCapabilities(currentNetwork)?.transportInfo as WifiInfo?
} else {
@Suppress("DEPRECATION")
wifiManager?.connectionInfo
}

// Android returns "SSID"
fun getWifiName(): String? {
var ssid: String? = null
if (wifiInfo != null) ssid = wifiInfo!!.ssid
return ssid
}
fun getWifiName(): String? = wifiInfo?.ssid

fun getWifiBSSID(): String? {
return if (wifiInfo != null) {
wifiInfo!!.bssid
} else {
null
}
}
fun getWifiBSSID(): String? = wifiInfo?.bssid

fun getWifiIPAddress(): String? {
var wifiInfo: WifiInfo? = null
if (wifiManager != null) wifiInfo = wifiManager.connectionInfo
var ip: String? = null
var interfaceIp = 0
if (wifiInfo != null) interfaceIp = wifiInfo.ipAddress
if (interfaceIp != 0) ip = formatIPAddress(interfaceIp)
return ip
var ipAddress: String? = null

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val linkAddresses = connectivityManager?.getLinkProperties(connectivityManager.activeNetwork)?.linkAddresses

val ipV4Address = linkAddresses?.firstOrNull { linkAddress ->

@vbuberen vbuberen Oct 3, 2022 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This filtering is added because API returns multiple addresses and only one of them is ipV4 that we need:
Screenshot 2022-10-02 at 22 50 50

linkAddress.address.hostAddress?.contains('.')
?: false
}?.address?.hostAddress

ipAddress = ipV4Address
} else {
@Suppress("DEPRECATION")
val interfaceIp = wifiInfo!!.ipAddress
if (interfaceIp != 0) ipAddress = formatIPAddress(interfaceIp)
}
return ipAddress
}

fun getWifiSubnetMask(): String {
Expand Down Expand Up @@ -84,10 +95,19 @@ internal class NetworkInfo(private val wifiManager: WifiManager?) {
return null
}

fun getGatewayIPAddress(): String {
val dhcpInfo = wifiManager!!.dhcpInfo
val gatewayIPInt = dhcpInfo.gateway
return formatIPAddress(gatewayIPInt)
fun getGatewayIPAddress(): String? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val linkAddresses = connectivityManager?.getLinkProperties(connectivityManager.activeNetwork)
val dhcpServer = linkAddresses?.dhcpServerAddress?.hostAddress

dhcpServer
} else {
@Suppress("DEPRECATION")
val dhcpInfo = wifiManager?.dhcpInfo
val gatewayIPInt = dhcpInfo?.gateway

gatewayIPInt?.let { formatIPAddress(it) }
}
}

private fun formatIPAddress(intIP: Int): String =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package dev.fluttercommunity.plus.network_info

import android.content.Context
import android.net.ConnectivityManager
import android.net.wifi.WifiManager
import android.os.Build
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding
import io.flutter.plugin.common.BinaryMessenger
Expand All @@ -24,7 +26,13 @@ class NetworkInfoPlusPlugin : FlutterPlugin {
methodChannel = MethodChannel(messenger, CHANNEL)
val wifiManager =
context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
val networkInfo = NetworkInfo(wifiManager)

var connectivityManager: ConnectivityManager? = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
connectivityManager = context.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
}

val networkInfo = NetworkInfo(wifiManager, connectivityManager)
val methodChannelHandler = NetworkInfoMethodChannelHandler(networkInfo)
methodChannel.setMethodCallHandler(methodChannelHandler)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
package="dev.fluttercommunity.plus.network_info_plus_example">

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Added it, so it is easy to open Settings of example app and grant permission without introducing any dependency to handle permission in this app.


<application
android:name="${applicationName}"
Expand Down
18 changes: 16 additions & 2 deletions packages/network_info_plus/network_info_plus/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,23 @@ class _MyHomePageState extends State<MyHomePage> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('NetworkInfo example app'),
title: const Text('NetworkInfoPlus example'),
),
body: Center(child: Text('Connection Status: $_connectionStatus')),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
'Network info',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 16),
Text(_connectionStatus),
],
)),
);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/network_info_plus/network_info_plus/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: network_info_plus
description: Flutter plugin for discovering information (e.g. WiFi details) of the network.
version: 2.2.0
version: 2.3.0
homepage: https://plus.fluttercommunity.dev/
repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/
repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/network_info_plus
issue_tracker: https://github.com/fluttercommunity/plus_plugins/labels/network_info_plus

environment:
Expand Down