Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
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
7 changes: 5 additions & 2 deletions packages/connectivity/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
## 0.4.5

* Support the v2 Android embedder.

## 0.4.4+2

* Change `ConnectionResult` type from enum to class with 2 fields. `type:ConnectionType` and `subtype:ConnectionSubtype`
* Add `getNetworkSubtype` to get mobile connection subtype.

## 0.4.4+1

* Update and migrate iOS example project.
Expand Down
31 changes: 26 additions & 5 deletions packages/connectivity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,33 @@ Sample usage to check current status:
```dart
import 'package:connectivity/connectivity.dart';

var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile) {
var connectivityInfo = await (Connectivity().checkConnectivityInfo());
if (connectivityInfo.result == ConnectivityResult.mobile) {
// I am connected to a mobile network.
} else if (connectivityResult == ConnectivityResult.wifi) {
if(connectivityInfo.subtype == ConnectionSubtype.HSDPA){
// I am on an HSDPA network
}
} else if (connectivityInfo.result == ConnectivityResult.wifi) {
// I am connected to a wifi network.
}
```

You can also check for a connection type when you are on mobile:

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

var connectivityResult = await (Connectivity().getNetworkSubtype());

if(connectivityResult.subtype == ConnectionSubtype.edge){
// I am on an edge network
} else if(connectivityResult.subtype == ConnectionSubtype.hsdpa){
// I am on a hsdpa network
} else if(connectivityResult.subtype == ConnectionSubtype.lte){
// I am on an lte network
}
```

> Note that you should not be using the current network status for deciding
whether you can reliably make a network connection. Always guard your app code
against timeouts and errors that might come from the network layer.
Expand All @@ -36,9 +55,11 @@ import 'package:connectivity/connectivity.dart';
initState() {
super.initState();

subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
subscription = Connectivity().onConnectivityInfoChanged.listen((ConnectionInfo info) {
// Got a new connectivity status!
})
info.result = // ConnectivityResult info
info.subtype = // Mobile Connectivity Subtype info
});
}

// Be sure to cancel subscription after you are done
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

Expand Down Expand Up @@ -107,4 +109,76 @@ private String getNetworkTypeLegacy() {
return "none";
}
}

@Nullable
String getNetworkSubType() {

NetworkInfo info = this.connectivityManager.getActiveNetworkInfo();

if (info == null || !info.isConnected()) {
return null;
}

/// Telephony Manager documentation https://developer.android.com/reference/android/telephony/TelephonyManager
/// Information about mobile broadband - https://en.wikipedia.org/wiki/Mobile_broadband#Generations

switch (info.getSubtype()) {
case TelephonyManager.NETWORK_TYPE_1xRTT: {
return "1xRTT"; // ~ 50-100 kbps
}
case TelephonyManager.NETWORK_TYPE_CDMA: {
return "cdma"; // ~ 14-64 kbps
}
case TelephonyManager.NETWORK_TYPE_EDGE: {
return "edge"; // ~ 50-100 kbps
}
case TelephonyManager.NETWORK_TYPE_EVDO_0: {
return "evdo_0"; // ~ 400-1000 kbps
}
case TelephonyManager.NETWORK_TYPE_EVDO_A: {
return "evdo_a"; // ~ 600-1400 kbps
}
case TelephonyManager.NETWORK_TYPE_GPRS: {
return "gprs"; // ~ 100 kbps
}
case TelephonyManager.NETWORK_TYPE_HSDPA: {
return "hsdpa"; // ~ 2-14 Mbps
}
case TelephonyManager.NETWORK_TYPE_HSPA: {
return "hspa"; // ~ 700-1700 kbps
}
case TelephonyManager.NETWORK_TYPE_HSUPA: {
return "hsupa"; // ~ 1-23 Mbps
}
case TelephonyManager.NETWORK_TYPE_UMTS: {
return "umts"; // ~ 400-7000 kbps
}
/*
* Above API level 7, make sure to set android:targetSdkVersion
* to appropriate level to use these
*/
case TelephonyManager.NETWORK_TYPE_EHRPD: { // API level 11
return "ehrpd"; // ~ 1-2 Mbps
}
case TelephonyManager.NETWORK_TYPE_EVDO_B: { // API level 9
return "evdo_b"; // ~ 5 Mbps
}
case TelephonyManager.NETWORK_TYPE_HSPAP: { // API level 13
return "hspap"; // ~ 10-20 Mbps
}
case TelephonyManager.NETWORK_TYPE_IDEN: { // API level 8
return "iden"; // ~25 kbps
}
case TelephonyManager.NETWORK_TYPE_LTE: { // API level 11
return "lte"; // ~ 10+ Mbps
}
// Unknown
case TelephonyManager.NETWORK_TYPE_UNKNOWN: {
return "unknown"; // is connected but cannot tell the speed
}
default: {
return null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
case "wifiIPAddress":
result.success(connectivity.getWifiIPAddress());
break;
case "subtype":
result.success(connectivity.getNetworkSubType());
break;
default:
result.notImplemented();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;
Expand Down Expand Up @@ -41,7 +42,7 @@ private void setupChannels(BinaryMessenger messenger, Context context) {
eventChannel = new EventChannel(messenger, "plugins.flutter.io/connectivity_status");
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

Connectivity connectivity = new Connectivity(connectivityManager, wifiManager);

Expand All @@ -60,4 +61,4 @@ private void teardownChannels() {
methodChannel = null;
eventChannel = null;
}
}
}
5 changes: 5 additions & 0 deletions packages/connectivity/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ class _MyHomePageState extends State<MyHomePage> {
});
break;
case ConnectivityResult.mobile:
setState(() {
_connectionStatus = '$result\n'
'Mobile Connection Type: $result\n';
});
break;
case ConnectivityResult.none:
setState(() => _connectionStatus = result.toString());
break;
Expand Down
53 changes: 50 additions & 3 deletions packages/connectivity/ios/Classes/ConnectivityPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@

#include <arpa/inet.h>

<<<<<<< HEAD
#import <CoreTelephony/CTTelephonyNetworkInfo.h>

@interface FLTConnectivityPlugin () <FlutterStreamHandler>
=======
@interface FLTConnectivityPlugin () <FlutterStreamHandler, CLLocationManagerDelegate>

@property(strong, nonatomic) FLTConnectivityLocationHandler* locationHandler;

>>>>>>> 0a7535d1cd7119767d8d2506b2c9e3742f585fa8
@end

@implementation FLTConnectivityPlugin {
Expand Down Expand Up @@ -90,15 +96,50 @@ - (NSString*)getWifiIP {
return address;
}

- (NSString*)getConnectionSubtype:(Reachability*)reachability {
if ([reachability currentReachabilityStatus] == NotReachable) {
return @"none";
}

CTTelephonyNetworkInfo* netinfo = [[CTTelephonyNetworkInfo alloc] init];
NSString* carrierType = netinfo.currentRadioAccessTechnology;

if ([carrierType isEqualToString:CTRadioAccessTechnologyGPRS]) {
return @"gprs";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyEdge]) {
return @"edge";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyWCDMA]) {
return @"cdma";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyHSDPA]) {
return @"hsdpa";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyHSUPA]) {
return @"hsupa";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyCDMA1x]) {
return @"cdma";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]) {
return @"evdo_0";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]) {
return @"evdo_a";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) {
return @"evdo_b";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyeHRPD]) {
return @"ehrpd";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyLTE]) {
return @"lte";
}
return @"unknown";
}

- (NSString*)statusFromReachability:(Reachability*)reachability {
NetworkStatus status = [reachability currentReachabilityStatus];
NSString* subtype = [self getConnectionSubtype:[Reachability reachabilityForInternetConnection]];
switch (status) {
case NotReachable:
return @"none";
return [NSString stringWithFormat:@"%@,%@", @"none", subtype];
case ReachableViaWiFi:
return @"wifi";
return [NSString stringWithFormat:@"%@,%@", @"wifi", subtype];
case ReachableViaWWAN:
return @"mobile";
return [NSString stringWithFormat:@"%@,%@", @"mobile", subtype];
}
}

Expand All @@ -116,6 +157,11 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
result([self getBSSID]);
} else if ([call.method isEqualToString:@"wifiIPAddress"]) {
result([self getWifiIP]);
<<<<<<< HEAD
} else if ([call.method isEqualToString:@"subtype"]) {
result([self getConnectionSubtype:[Reachability reachabilityForInternetConnection]]);

=======
} else if ([call.method isEqualToString:@"getLocationServiceAuthorization"]) {
result([self convertCLAuthorizationStatusToString:[FLTConnectivityLocationHandler
locationAuthorizationStatus]]);
Expand All @@ -128,6 +174,7 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
completion:^(CLAuthorizationStatus status) {
result([weakSelf convertCLAuthorizationStatusToString:status]);
}];
>>>>>>> 0a7535d1cd7119767d8d2506b2c9e3742f585fa8
} else {
result(FlutterMethodNotImplemented);
}
Expand Down
Loading