Java Code Examples for android.net.ConnectivityManager#bindProcessToNetwork()

The following examples show how to use android.net.ConnectivityManager#bindProcessToNetwork() . 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: MainActivity.java    From Chorus-RF-Laptimer with MIT License 6 votes vote down vote up
private void preferWifiAndConnectUDP() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        for (Network net : cm.getAllNetworks()) {
            NetworkInfo networkInfo = cm.getNetworkInfo(net);
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                cm.bindProcessToNetwork(net);
                break;
            }
        }
    }

    udp.connect(getGatewayIP(), 0);
    useUDP();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        cm.bindProcessToNetwork(null);
    }
}
 
Example 2
Source File: StateMachineConnection.java    From timelapse-sony with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void process(final StateMachineConnection sm) {

    // Workaround when there is a data connection more than the wifi one
    // http://stackoverflow.com/questions/33237074/request-over-wifi-on-android-m
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        ConnectivityManager connectivityManager = (ConnectivityManager)
                sm.mApplication.getSystemService(Context.CONNECTIVITY_SERVICE);
        for (Network net : connectivityManager.getAllNetworks()) {
            NetworkInfo netInfo = connectivityManager.getNetworkInfo(net);
            if (netInfo != null
                    && netInfo.getType() == ConnectivityManager.TYPE_WIFI
                    && netInfo.getExtraInfo() != null
                    && netInfo.getExtraInfo()
                    .equals(sm.mStateRegistry.wifiInfo.getSSID())) {
                connectivityManager.bindProcessToNetwork(net);
                break;
            }
        }
    }

    sm.mStateRegistry.apiAttempts = 1;
    sm.setCurrentState(State.CHECK_API);
}
 
Example 3
Source File: RNWifiModule.java    From react-native-wifi-reborn with ISC License 5 votes vote down vote up
/**
 * Use this to execute api calls to a wifi network that does not have internet access.
 *
 * Useful for commissioning IoT devices.
 *
 * This will route all app network requests to the network (instead of the mobile connection).
 * It is important to disable it again after using as even when the app disconnects from the wifi
 * network it will keep on routing everything to wifi.
 *
 * @param useWifi boolean to force wifi off or on
 */
@ReactMethod
public void forceWifiUsage(final boolean useWifi, final Promise promise) {
    final ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connectivityManager == null) {
        promise.reject(ForceWifiUsageErrorCodes.couldNotGetConnectivityManager.toString(), "Failed to get the ConnectivityManager.");
        return;
    }

    if (useWifi) {
        NetworkRequest networkRequest = new NetworkRequest.Builder()
                .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                .build();
        connectivityManager.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(@NonNull final Network network) {
                super.onAvailable(network);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    connectivityManager.bindProcessToNetwork(network);
                } else {
                    ConnectivityManager.setProcessDefaultNetwork(network);
                }

                connectivityManager.unregisterNetworkCallback(this);

                promise.resolve(null);
            }
        });
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            connectivityManager.bindProcessToNetwork(null);
        } else {
            ConnectivityManager.setProcessDefaultNetwork(null);
        }

        promise.resolve(null);
    }
}
 
Example 4
Source File: PhoneUtils.java    From Mobilyzer with Apache License 2.0 5 votes vote down vote up
public void unregisterNetworkCallback(){
	if (connectivityNetworkCallback!=null){
		ConnectivityManager cm=((ConnectivityNetworkCallback)connectivityNetworkCallback).getConnectivityManager();
		cm.bindProcessToNetwork(null);
		cm.unregisterNetworkCallback(connectivityNetworkCallback);
		connectivityNetworkCallback = null;
	}
}
 
Example 5
Source File: ConnectorUtils.java    From WifiUtils with Apache License 2.0 4 votes vote down vote up
@RequiresApi(Build.VERSION_CODES.Q)
private static boolean connectAndroidQ(@Nullable ConnectivityManager connectivityManager, @NonNull ScanResult scanResult, @NonNull String password) {
    if (connectivityManager == null) {
        return false;
    }

    WifiNetworkSpecifier.Builder wifiNetworkSpecifierBuilder = new WifiNetworkSpecifier.Builder()
            .setSsid(scanResult.SSID)
            .setBssid(MacAddress.fromString(scanResult.BSSID));

    final String security = ConfigSecurities.getSecurity(scanResult);

    ConfigSecurities.setupWifiNetworkSpecifierSecurities(wifiNetworkSpecifierBuilder, security, password);


    NetworkRequest networkRequest = new NetworkRequest.Builder()
            .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
            .setNetworkSpecifier(wifiNetworkSpecifierBuilder.build())
            .build();

    // not sure, if this is needed
    if (networkCallback != null) {
        connectivityManager.unregisterNetworkCallback(networkCallback);
    }

    networkCallback = new ConnectivityManager.NetworkCallback() {
        @Override
        public void onAvailable(@NonNull Network network) {
            super.onAvailable(network);

            wifiLog("AndroidQ+ connected to wifi ");

            // bind so all api calls are performed over this new network
            connectivityManager.bindProcessToNetwork(network);
        }

        @Override
        public void onUnavailable() {
            super.onUnavailable();

            wifiLog("AndroidQ+ could not connect to wifi");
        }
    };

    connectivityManager.requestNetwork(networkRequest, networkCallback);

    return true;
}