Java Code Examples for android.net.NetworkCapabilities#hasTransport()

The following examples show how to use android.net.NetworkCapabilities#hasTransport() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConnectivityHelper.java    From libcommon with Apache License 2.0 6 votes vote down vote up
@SuppressLint("NewApi")
	@TargetApi(Build.VERSION_CODES.LOLLIPOP)
	private static boolean isWifiNetworkReachable(
		@NonNull final NetworkCapabilities capabilities,
		@NonNull final NetworkInfo info) {

		final boolean isWiFi;
		if (BuildCheck.isAPI26()) {
			isWiFi = capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)		// API>=21
				|| capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET);	// API>=21
//				|| capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI_AWARE);	// API>=26 これはWi-Fi端末間での近接情報の発見機能
		} else {
			isWiFi = capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)		// API>=21
				|| capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET);	// API>=21
		}
		return isWiFi && isNetworkReachable(capabilities, info);
	}
 
Example 2
Source File: ConnectionHelper.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
static boolean vpnActive(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null)
        return false;

    try {
        for (Network network : cm.getAllNetworks()) {
            NetworkCapabilities caps = cm.getNetworkCapabilities(network);
            if (caps != null && caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN))
                return true;
        }
    } catch (Throwable ex) {
        Log.w(ex);
    }

    return false;
}
 
Example 3
Source File: Publisher.java    From PresencePublisher with MIT License 6 votes vote down vote up
private boolean sendMessageViaCurrentConnection() {
    if (connectivityManager == null) {
        HyperLog.e(TAG, "Connectivity Manager not found");
        return false;
    }
    //noinspection deprecation
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    //noinspection deprecation
    if (activeNetworkInfo == null || !activeNetworkInfo.isConnected()) {
        return false;
    }
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        for (Network network : connectivityManager.getAllNetworks()) {
            NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(network);
            if (networkCapabilities != null && networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                return true;
            }
        }
        return sendViaMobile();
    } else {
        //noinspection deprecation
        return activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI
                || activeNetworkInfo.getType() == ConnectivityManager.TYPE_VPN
                || activeNetworkInfo.getType() == ConnectivityManager.TYPE_ETHERNET
                || sendViaMobile();
    }
}
 
Example 4
Source File: NetWorkReceiver.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 获取连接的网络类型
 * @return 连接的网络类型
 */
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
public static int getConnectType() {
    // 获取手机所有连接管理对象 ( 包括对 wi-fi,net 等连接的管理 )
    try {
        // 获取网络连接状态
        ConnectivityManager cManager = AppUtils.getConnectivityManager();
        // 版本兼容处理
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
            // 判断连接的是否 Wifi
            NetworkInfo.State wifiState = cManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
            // 判断是否连接上
            if (wifiState == NetworkInfo.State.CONNECTED || wifiState == NetworkInfo.State.CONNECTING) {
                return NET_WIFI;
            } else {
                // 判断连接的是否移动网络
                NetworkInfo.State mobileState = cManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
                // 判断移动网络是否连接上
                if (mobileState == NetworkInfo.State.CONNECTED || mobileState == NetworkInfo.State.CONNECTING) {
                    return NET_MOBILE;
                }
            }
        } else {
            // 获取当前活跃的网络 ( 连接的网络信息 )
            Network network = cManager.getActiveNetwork();
            if (network != null) {
                NetworkCapabilities networkCapabilities = cManager.getNetworkCapabilities(network);
                // 判断连接的是否 Wifi
                if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                    return NET_WIFI;
                } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
                    return NET_MOBILE;
                }
            }
        }
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "getConnectType");
    }
    return NO_NETWORK;
}
 
Example 5
Source File: NetworkChangeNotifierAutoDetect.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Should changes to connected network {@code network} be ignored?
 * @param network Network to possibly consider ignoring changes to.
 * @param capabilities {@code NetworkCapabilities} for {@code network} if known, otherwise
 *         {@code null}.
 * @return {@code true} when either: {@code network} is an inaccessible VPN, or has already
 *         disconnected.
 */
private boolean ignoreConnectedInaccessibleVpn(
        Network network, NetworkCapabilities capabilities) {
    // Fetch capabilities if not provided.
    if (capabilities == null) {
        capabilities = mConnectivityManagerDelegate.getNetworkCapabilities(network);
    }
    // Ignore inaccessible VPNs as they don't apply to Chrome.
    return capabilities == null
            || capabilities.hasTransport(TRANSPORT_VPN)
            && !mConnectivityManagerDelegate.vpnAccessible(network);
}
 
Example 6
Source File: NetworkChangeNotifierAutoDetect.java    From cronet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns all connected networks that are useful and accessible to Chrome.
 * Only callable on Lollipop and newer releases.
 * @param ignoreNetwork ignore this network as if it is not connected.
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static Network[] getAllNetworksFiltered(
        ConnectivityManagerDelegate connectivityManagerDelegate, Network ignoreNetwork) {
    Network[] networks = connectivityManagerDelegate.getAllNetworksUnfiltered();
    // Whittle down |networks| into just the list of networks useful to us.
    int filteredIndex = 0;
    for (Network network : networks) {
        if (network.equals(ignoreNetwork)) {
            continue;
        }
        final NetworkCapabilities capabilities =
                connectivityManagerDelegate.getNetworkCapabilities(network);
        if (capabilities == null || !capabilities.hasCapability(NET_CAPABILITY_INTERNET)) {
            continue;
        }
        if (capabilities.hasTransport(TRANSPORT_VPN)) {
            // If we can access the VPN then...
            if (connectivityManagerDelegate.vpnAccessible(network)) {
                // ...we cannot access any other network, so return just the VPN.
                return new Network[] {network};
            } else {
                // ...otherwise ignore it as we cannot use it.
                continue;
            }
        }
        networks[filteredIndex++] = network;
    }
    return Arrays.copyOf(networks, filteredIndex);
}
 
Example 7
Source File: NetworkChangeNotifierAutoDetect.java    From cronet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onAvailable(Network network) {
    final NetworkCapabilities capabilities =
            mConnectivityManagerDelegate.getNetworkCapabilities(network);
    if (ignoreConnectedNetwork(network, capabilities)) {
        return;
    }
    final boolean makeVpnDefault = capabilities.hasTransport(TRANSPORT_VPN);
    if (makeVpnDefault) {
        mVpnInPlace = network;
    }
    final long netId = networkToNetId(network);
    @ConnectionType
    final int connectionType = mConnectivityManagerDelegate.getConnectionType(network);
    runOnThread(new Runnable() {
        @Override
        public void run() {
            mObserver.onNetworkConnect(netId, connectionType);
            if (makeVpnDefault) {
                // Make VPN the default network.
                mObserver.onConnectionTypeChanged(connectionType);
                // Purge all other networks as they're inaccessible to Chrome now.
                mObserver.purgeActiveNetworkList(new long[] {netId});
            }
        }
    });
}
 
Example 8
Source File: NetworkUtils.java    From sa-sdk-android with Apache License 2.0 5 votes vote down vote up
/**
 * 是否有可用网络
 *
 * @param context Context
 * @return true:网络可用,false:网络不可用
 */
public static boolean isNetworkAvailable(Context context) {
    // 检测权限
    if (!SensorsDataUtils.checkHasPermission(context, Manifest.permission.ACCESS_NETWORK_STATE)) {
        return false;
    }
    try {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                Network network = cm.getActiveNetwork();
                if (network != null) {
                    NetworkCapabilities capabilities = cm.getNetworkCapabilities(network);
                    if (capabilities != null) {
                        return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
                                || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
                                || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)
                                || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN);
                    }
                }
            } else {
                NetworkInfo networkInfo = cm.getActiveNetworkInfo();
                return networkInfo != null && networkInfo.isConnected();
            }
        }
        return false;
    } catch (Exception e) {
        SALog.printStackTrace(e);
        return false;
    }
}
 
Example 9
Source File: NetworkChangeNotifierAutoDetect.java    From cronet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
void initializeVpnInPlace() {
    final Network[] networks = getAllNetworksFiltered(mConnectivityManagerDelegate, null);
    mVpnInPlace = null;
    // If the filtered list of networks contains just a VPN, then that VPN is in place.
    if (networks.length == 1) {
        final NetworkCapabilities capabilities =
                mConnectivityManagerDelegate.getNetworkCapabilities(networks[0]);
        if (capabilities != null && capabilities.hasTransport(TRANSPORT_VPN)) {
            mVpnInPlace = networks[0];
        }
    }
}
 
Example 10
Source File: LocalIPUtil.java    From DanDanPlayForAndroid with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private LinkProperties getLinkProperties(ConnectivityManager connectivityManager, int cap) {
    Network nets[] = connectivityManager.getAllNetworks();
    for (Network n: nets) {
        LinkProperties linkProperties = connectivityManager.getLinkProperties(n);
        NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(n);
        String interfaceName =  linkProperties.getInterfaceName();
        if (interfaceName != null && networkCapabilities != null) {
            if (networkCapabilities.hasTransport(cap))
                return linkProperties;
        }
    }
    return null;
}
 
Example 11
Source File: DeviceInfoHelper.java    From appium-uiautomator2-server with Apache License 2.0 5 votes vote down vote up
public static String extractTransportTypes(NetworkCapabilities caps) {
    List<String> result = new ArrayList<>();
    for (Map.Entry<Integer, String> entry : TRANSPORTS.entrySet()) {
        if (caps.hasTransport(entry.getKey())) {
            result.add(entry.getValue());
        }
    }
    return TextUtils.join(",", result);
}
 
Example 12
Source File: ConnectionState.java    From onpc with GNU General Public License v3.0 5 votes vote down vote up
boolean isWifi()
{
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    {
        // getActiveNetwork Added in API level 23
        final Network net = connectivity.getActiveNetwork();
        if (net == null)
        {
            return false;
        }
        // getNetworkCapabilities Added in API level 21
        final NetworkCapabilities cap = connectivity.getNetworkCapabilities(net);
        if (cap == null)
        {
            return false;
        }
        // hasTransport Added in API level 21
        return cap.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
                || cap.hasTransport(NetworkCapabilities.TRANSPORT_VPN);
    }
    else
    {
        // If app targets Android 10 or higher, it must have the ACCESS_FINE_LOCATION permission
        // in order to use getConnectionInfo(), see
        // https://developer.android.com/about/versions/10/privacy/changes
        return wifi != null &&
                wifi.isWifiEnabled() &&
                wifi.getConnectionInfo() != null &&
                wifi.getConnectionInfo().getNetworkId() != -1;
    }
}
 
Example 13
Source File: ConnectivityHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static boolean isBluetoothNetworkReachable(
	@NonNull final NetworkCapabilities capabilities,
	@NonNull final NetworkInfo info) {

	return
		capabilities.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH)// API>=21
			&& isNetworkReachable(capabilities, info);
}
 
Example 14
Source File: ThetaM15.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
private static SocketFactory getWifiSocketFactory(final Context context) {
    SocketFactory socketFactory = SocketFactory.getDefault();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !isGalaxyDevice()) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        Network[] allNetwork = cm.getAllNetworks();
        for (Network network : allNetwork) {
            NetworkCapabilities networkCapabilities = cm.getNetworkCapabilities(network);
            if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                socketFactory = network.getSocketFactory();
            }
        }
    }
    return socketFactory;
}
 
Example 15
Source File: NetworkUtil.java    From NClientV2 with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static ConnectionType getConnectivityPostLollipop(ConnectivityManager cm,Network network){
    NetworkCapabilities capabilities=cm.getNetworkCapabilities(network);
    if(capabilities==null){return ConnectionType.WIFI;}
    if(capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR))
        return ConnectionType.CELLULAR;
    return ConnectionType.WIFI;
}
 
Example 16
Source File: NetworkCallbackImpl.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
@Override
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
    LogUtils.e("onCapabilitiesChanged: "+ networkCapabilities);
    if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
        if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
            mListener.onWifiConnected();
        } else {
            mListener.onMobileConnected();
        }
    }
}
 
Example 17
Source File: WiFiSocketFactory.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static Socket createWiFiSocket(Context context) {
    if(context == null){
        logInfo("Context supplied was null");
        return null;
    }
    PackageManager pm = context.getPackageManager();
    if (pm == null) {
        logInfo("PackageManager isn't available.");
        return null;
    }
    // getAllNetworks() and getNetworkCapabilities() require ACCESS_NETWORK_STATE
    if (pm.checkPermission(Manifest.permission.ACCESS_NETWORK_STATE, context.getPackageName()) != PackageManager.PERMISSION_GRANTED) {
        logInfo("Router service doesn't have ACCESS_NETWORK_STATE permission. It cannot bind a TCP transport to Wi-Fi network.");
        return null;
    }

    ConnectivityManager connMan = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connMan == null) {
        logInfo("ConnectivityManager isn't available.");
        return null;
    }

    Network[] allNetworks = connMan.getAllNetworks();
    if (allNetworks == null) {
        logInfo("Failed to acquire a list of networks.");
        return null;
    }

    // Samsung Galaxy S9 (with Android 8.0.0) provides two `Network` instances which have
    // TRANSPORT_WIFI capability. The first one throws an IOException upon creating a Socket,
    // and the second one actually works. To support such case, here we iterate over all
    // `Network` instances until we can create a Socket.
    for (Network network : allNetworks) {
        if (network == null) {
            continue;
        }

        NetworkCapabilities capabilities = connMan.getNetworkCapabilities(network);
        if (capabilities == null) {
            continue;
        }

        if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
            try {
                SocketFactory factory = network.getSocketFactory();
                if (factory != null) {
                    return factory.createSocket();
                }
            } catch (IOException e) {
                logInfo("IOException during socket creation (ignored): " + e.getMessage());
            }
        }
    }

    logInfo("Cannot find Wi-Fi network to bind a TCP transport.");
    return null;
}
 
Example 18
Source File: UpstreamNetworkMonitor.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private static boolean isCellular(NetworkCapabilities nc) {
    return (nc != null) && nc.hasTransport(TRANSPORT_CELLULAR) &&
           nc.hasCapability(NET_CAPABILITY_NOT_VPN);
}
 
Example 19
Source File: NetworkMonitorAutoDetect.java    From webrtc_android with MIT License 4 votes vote down vote up
/**
 * Returns connection type and status information about |network|.
 * Only callable on Lollipop and newer releases.
 */
@SuppressLint("NewApi")
NetworkState getNetworkState(  Network network) {
  if (network == null || connectivityManager == null) {
    return new NetworkState(false, -1, -1, -1, -1);
  }
  NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
  if (networkInfo == null) {
    Logging.w(TAG, "Couldn't retrieve information from network " + network.toString());
    return new NetworkState(false, -1, -1, -1, -1);
  }
  // The general logic of handling a VPN in this method is as follows. getNetworkInfo will
  // return the info of the network with the same id as in |network| when it is registered via
  // ConnectivityManager.registerNetworkAgent in Android. |networkInfo| may or may not indicate
  // the type TYPE_VPN if |network| is a VPN. To reliably detect the VPN interface, we need to
  // query the network capability as below in the case when networkInfo.getType() is not
  // TYPE_VPN. On the other hand when networkInfo.getType() is TYPE_VPN, the only solution so
  // far to obtain the underlying network information is to query the active network interface.
  // However, the active network interface may not be used for the VPN, for example, if the VPN
  // is restricted to WiFi by the implementation but the WiFi interface is currently turned
  // off and the active interface is the Cell. Using directly the result from
  // getActiveNetworkInfo may thus give the wrong interface information, and one should note
  // that getActiveNetworkInfo would return the default network interface if the VPN does not
  // specify its underlying networks in the implementation. Therefore, we need further compare
  // |network| to the active network. If they are not the same network, we will have to fall
  // back to report an unknown network.

  if (networkInfo.getType() != ConnectivityManager.TYPE_VPN) {
    // Note that getNetworkCapabilities returns null if the network is unknown.
    NetworkCapabilities networkCapabilities =
        connectivityManager.getNetworkCapabilities(network);
    if (networkCapabilities == null
        || !networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
      return getNetworkState(networkInfo);
    }
    // When |network| is in fact a VPN after querying its capability but |networkInfo| is not of
    // type TYPE_VPN, |networkInfo| contains the info for the underlying network, and we return
    // a NetworkState constructed from it.
    return new NetworkState(networkInfo.isConnected(), ConnectivityManager.TYPE_VPN, -1,
        networkInfo.getType(), networkInfo.getSubtype());
  }

  // When |networkInfo| is of type TYPE_VPN, which implies |network| is a VPN, we return the
  // NetworkState of the active network via getActiveNetworkInfo(), if |network| is the active
  // network that supports the VPN. Otherwise, NetworkState of an unknown network with type -1
  // will be returned.
  //
  // Note that getActiveNetwork and getActiveNetworkInfo return null if no default network is
  // currently active.
  if (networkInfo.getType() == ConnectivityManager.TYPE_VPN) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
        && network.equals(connectivityManager.getActiveNetwork())) {
      // If a VPN network is in place, we can find the underlying network type via querying the
      // active network info thanks to
      // https://android.googlesource.com/platform/frameworks/base/+/d6a7980d
      NetworkInfo underlyingActiveNetworkInfo = connectivityManager.getActiveNetworkInfo();
      // We use the NetworkInfo of the underlying network if it is not of TYPE_VPN itself.
      if (underlyingActiveNetworkInfo != null
          && underlyingActiveNetworkInfo.getType() != ConnectivityManager.TYPE_VPN) {
        return new NetworkState(networkInfo.isConnected(), ConnectivityManager.TYPE_VPN, -1,
            underlyingActiveNetworkInfo.getType(), underlyingActiveNetworkInfo.getSubtype());
      }
    }
    return new NetworkState(
        networkInfo.isConnected(), ConnectivityManager.TYPE_VPN, -1, -1, -1);
  }

  return getNetworkState(networkInfo);
}
 
Example 20
Source File: ConnectivityChecker.java    From sentry-android with MIT License 4 votes vote down vote up
@SuppressLint({"ObsoleteSdkInt", "MissingPermission", "NewApi"})
public static @Nullable String getConnectionType(
    final @NotNull Context context,
    final @NotNull ILogger logger,
    final @NotNull IBuildInfoProvider buildInfoProvider) {
  final ConnectivityManager connectivityManager = getConnectivityManager(context, logger);
  if (connectivityManager == null) {
    return null;
  }
  if (!Permissions.hasPermission(context, Manifest.permission.ACCESS_NETWORK_STATE)) {
    logger.log(SentryLevel.INFO, "No permission (ACCESS_NETWORK_STATE) to check network status.");
    return null;
  }

  boolean ethernet = false;
  boolean wifi = false;
  boolean cellular = false;

  if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.M) {
    final Network activeNetwork = connectivityManager.getActiveNetwork();
    if (activeNetwork == null) {
      logger.log(SentryLevel.INFO, "Network is null and cannot check network status");
      return null;
    }
    final NetworkCapabilities networkCapabilities =
        connectivityManager.getNetworkCapabilities(activeNetwork);
    if (networkCapabilities == null) {
      logger.log(SentryLevel.INFO, "NetworkCapabilities is null and cannot check network type");
      return null;
    }
    if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
      ethernet = true;
    }
    if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
      wifi = true;
    }
    if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
      cellular = true;
    }
  } else {
    // ideally using connectivityManager.getAllNetworks(), but its >= Android L only

    // for some reason linting didn't allow a single @SuppressWarnings("deprecation") on method
    // signature
    @SuppressWarnings("deprecation")
    final android.net.NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

    if (activeNetworkInfo == null) {
      logger.log(SentryLevel.INFO, "NetworkInfo is null, there's no active network.");
      return null;
    }

    @SuppressWarnings("deprecation")
    final int type = activeNetworkInfo.getType();

    @SuppressWarnings("deprecation")
    final int TYPE_ETHERNET = ConnectivityManager.TYPE_ETHERNET;

    @SuppressWarnings("deprecation")
    final int TYPE_WIFI = ConnectivityManager.TYPE_WIFI;

    @SuppressWarnings("deprecation")
    final int TYPE_MOBILE = ConnectivityManager.TYPE_MOBILE;

    switch (type) {
      case TYPE_ETHERNET:
        ethernet = true;
        break;
      case TYPE_WIFI:
        wifi = true;
        break;
      case TYPE_MOBILE:
        cellular = true;
        break;
    }
  }

  // TODO: change the protocol to be a list of transports as a device may have the capability of
  // multiple
  if (ethernet) {
    return "ethernet";
  }
  if (wifi) {
    return "wifi";
  }
  if (cellular) {
    return "cellular";
  }

  return null;
}