Example usage for android.net NetworkCapabilities TRANSPORT_WIFI

List of usage examples for android.net NetworkCapabilities TRANSPORT_WIFI

Introduction

In this page you can find the example usage for android.net NetworkCapabilities TRANSPORT_WIFI.

Prototype

int TRANSPORT_WIFI

To view the source code for android.net NetworkCapabilities TRANSPORT_WIFI.

Click Source Link

Document

Indicates this network uses a Wi-Fi transport.

Usage

From source file:org.allseen.lsf.sampleapp.view.MainFragment.java

@SuppressLint("NewApi")
private void initWifiMonitoringApi21() {
    // Set the initial wifi state
    wifiConnectionStateUpdate(isWifiConnected());

    // Listen for wifi state changes
    NetworkRequest networkRequest = (new NetworkRequest.Builder())
            .addTransportType(NetworkCapabilities.TRANSPORT_WIFI).build();
    ConnectivityManager connectivityManager = (ConnectivityManager) getActivity()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    connectivityManager.registerNetworkCallback(networkRequest, new ConnectivityManager.NetworkCallback() {

        @Override// w  ww  .j  a  va 2 s  . co  m
        public void onAvailable(Network network) {
            wifiConnectionStateUpdate(true);
        }

        @Override
        public void onLost(Network network) {
            wifiConnectionStateUpdate(false);
        }
    });
}

From source file:com.mobilyzer.util.PhoneUtils.java

public void switchNetwork(boolean toWiFi, CountDownLatch latch) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkRequest.Builder request = new NetworkRequest.Builder();

    if (toWiFi) {
        request.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
    } else {//from  w ww  .  j a v  a  2 s  .c o m
        request.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
    }
    request.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
    ////      request.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
    //      ConnectivityManager.NetworkCallback connectivityNetworkCallback = new ConnectivityNetworkCallback(latch, cm);
    connectivityNetworkCallback = new ConnectivityNetworkCallback(latch, cm);
    cm.requestNetwork(request.build(), connectivityNetworkCallback);

}

From source file:org.universAAL.android.services.MiddlewareService.java

/**
 * Helper method to determine if there is a WiFi data connection.
 * /*from w w  w . ja  v  a 2  s .  co  m*/
 * @return True if there is one.
 */
//   private boolean isWifiOn(){
//      try {
//         Thread.sleep(3000);
//      } catch (InterruptedException e) {
//         e.printStackTrace();
//      }
//      ConnectivityManager connectivityManager = (ConnectivityManager) MiddlewareService.this
//            .getSystemService(Context.CONNECTIVITY_SERVICE);
//      NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
//      return (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI);
//   }

@SuppressLint("NewApi")
private void requestLollipopWifi() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        ConnectivityManager connectivityManager = (ConnectivityManager) MiddlewareService.this
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        connectivityManager.requestNetwork(
                new NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_WIFI).build(),
                new ConnectivityManager.NetworkCallback() {
                    public void onAvailable(Network network) {
                        Log.d(TAG, "Setting process default network " + network.toString());
                        ConnectivityManager.setProcessDefaultNetwork(network);
                    }
                });
    }
}