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

The following examples show how to use android.net.ConnectivityManager#setProcessDefaultNetwork() . 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: 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 2
Source File: IOTWifiModule.java    From react-native-iot-wifi with MIT License 5 votes vote down vote up
private void bindProcessToNetwork(final Network network) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        connectivityManager.bindProcessToNetwork(network);
    } else {
        ConnectivityManager.setProcessDefaultNetwork(network);
    }
}
 
Example 3
Source File: WifiUtil.java    From android-wificonnect with MIT License 5 votes vote down vote up
@TargetApi(LOLLIPOP)
void bindToRequiredNetwork(Network network) {
    if (SDK_INT >= M) {
        manager.bindProcessToNetwork(network);
    } else {
        ConnectivityManager.setProcessDefaultNetwork(network);
    }
}
 
Example 4
Source File: WifiUtils.java    From mosmetro-android with GNU General Public License v3.0 5 votes vote down vote up
@RequiresApi(21)
private void bindToNetwork(@Nullable Network network) {
    if (Build.VERSION.SDK_INT < 23) {
        try {
            ConnectivityManager.setProcessDefaultNetwork(network);
        } catch (IllegalStateException ignored) {}
    } else {
        cm.bindProcessToNetwork(network);
    }
}