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
25 changes: 25 additions & 0 deletions packages/network_info_plus/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Auto detect text files and perform LF normalization
* text=auto

# Always perform LF normalization on these files
*.dart text
*.gradle text
*.html text
*.java text
*.json text
*.md text
*.py text
*.sh text
*.txt text
*.xml text
*.yaml text

# Make sure that these Windows files always have CRLF line endings in checkout
*.bat text eol=crlf
*.ps1 text eol=crlf

# Never perform LF normalization on these files
*.ico binary
*.jar binary
*.png binary
*.zip binary
47 changes: 47 additions & 0 deletions packages/network_info_plus/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
.DS_Store
.atom/
.idea/
.vscode/

.packages
.pub/
.dart_tool/
pubspec.lock
flutter_export_environment.sh

examples/all_plugins/pubspec.yaml

Podfile
Podfile.lock
Pods/
.symlinks/
**/Flutter/App.framework/
**/Flutter/ephemeral/
**/Flutter/Flutter.framework/
**/Flutter/Generated.xcconfig
**/Flutter/flutter_assets/

ServiceDefinitions.json
xcuserdata/
**/DerivedData/

local.properties
keystore.properties
.gradle/
gradlew
gradlew.bat
gradle-wrapper.jar
.flutter-plugins-dependencies
*.iml

generated_plugin_registrant.dart
GeneratedPluginRegistrant.h
GeneratedPluginRegistrant.m
GeneratedPluginRegistrant.java
GeneratedPluginRegistrant.swift
build/
.flutter-plugins

.project
.classpath
.settings
3 changes: 3 additions & 0 deletions packages/network_info_plus/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.1.0

- Initial release
27 changes: 27 additions & 0 deletions packages/network_info_plus/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 changes: 61 additions & 0 deletions packages/network_info_plus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# network_info_plus

[![Flutter Community: network_info_plus](https://fluttercommunity.dev/_github/header/network_info_plus)](https://github.com/fluttercommunity/community)

[![pub package](https://img.shields.io/pub/v/network_info_plus.svg)](https://pub.dev/packages/network_info_plus)

This plugin allows Flutter apps to discover network info and configure
themselves accordingly.

## Usage

You can get wi-fi related information using:

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

var wifiBSSID = await (NetworkInfo().getWifiBSSID());
var wifiIP = await (NetworkInfo().getWifiIP());network
var wifiName = await (NetworkInfo().getWifiName());wifi network
```

### 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

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/).

For help on editing plugin code, view the [documentation](https://flutter.dev/platform-plugins/#edit-code).
34 changes: 34 additions & 0 deletions packages/network_info_plus/android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
group 'dev.fluttercommunity.plus.network_info'
version '1.0-SNAPSHOT'

buildscript {
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
}
}

rootProject.allprojects {
repositories {
google()
jcenter()
}
}

apply plugin: 'com.android.library'

android {
compileSdkVersion 28

defaultConfig {
minSdkVersion 16
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
lintOptions {
disable 'InvalidPackage'
}
}
1 change: 1 addition & 0 deletions packages/network_info_plus/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.gradle.jvmargs=-Xmx1536M
1 change: 1 addition & 0 deletions packages/network_info_plus/android/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'network_info'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dev.fluttercommunity.plus.network_info">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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 2019 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 dev.fluttercommunity.plus.network_info;

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

/** Reports network info such as wifi name and address. */
class NetworkInfo {
private WifiManager wifiManager;

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

String getWifiName() {
WifiInfo wifiInfo = getWifiInfo();
String ssid = null;
if (wifiInfo != null) ssid = wifiInfo.getSSID();
if (ssid != null) ssid = ssid.replaceAll("\"", ""); // Android returns "SSID"
return ssid;
}

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

String getWifiIPAddress() {
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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2019 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 dev.fluttercommunity.plus.network_info;

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 NetworkInfo}, and then send the result back to the UIThread through the {@link
* MethodChannel.Result}.
*/
class NetworkInfoMethodChannelHandler implements MethodChannel.MethodCallHandler {

private NetworkInfo networkInfo;

/**
* Construct the NetworkInfoMethodChannelHandler with a {@code networkInfo}. The {@code
* networkInfo} must not be null.
*/
NetworkInfoMethodChannelHandler(NetworkInfo networkInfo) {
assert (networkInfo != null);
this.networkInfo = networkInfo;
}

@Override
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
switch (call.method) {
case "wifiName":
result.success(networkInfo.getWifiName());
break;
case "wifiBSSID":
result.success(networkInfo.getWifiBSSID());
break;
case "wifiIPAddress":
result.success(networkInfo.getWifiIPAddress());
break;
default:
result.notImplemented();
break;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2017 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 dev.fluttercommunity.plus.network_info;

import android.content.Context;
import android.net.wifi.WifiManager;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/** NetworkInfoPlusPlugin */
public class NetworkInfoPlusPlugin implements FlutterPlugin {

private MethodChannel methodChannel;

/** Plugin registration. */
public static void registerWith(Registrar registrar) {

NetworkInfoPlusPlugin plugin = new NetworkInfoPlusPlugin();
plugin.setupChannels(registrar.messenger(), registrar.context());
}

@Override
public void onAttachedToEngine(FlutterPluginBinding binding) {
setupChannels(binding.getBinaryMessenger(), binding.getApplicationContext());
}

@Override
public void onDetachedFromEngine(FlutterPluginBinding binding) {
teardownChannels();
}

private void setupChannels(BinaryMessenger messenger, Context context) {
methodChannel = new MethodChannel(messenger, "dev.fluttercommunity.plus/network_info");
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

NetworkInfo networkInfo = new NetworkInfo(wifiManager);

NetworkInfoMethodChannelHandler methodChannelHandler =
new NetworkInfoMethodChannelHandler(networkInfo);

methodChannel.setMethodCallHandler(methodChannelHandler);
}

private void teardownChannels() {
methodChannel.setMethodCallHandler(null);
methodChannel = null;
}
}
Loading