android.net.wifi.WifiNetworkSpecifier Java Examples

The following examples show how to use android.net.wifi.WifiNetworkSpecifier. 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: ConfigSecurities.java    From WifiUtils with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.Q)
static void setupWifiNetworkSpecifierSecurities(@NonNull WifiNetworkSpecifier.Builder wifiNetworkSpecifierBuilder, String security, @NonNull final String password) {
    wifiLog("Setting up WifiNetworkSpecifier.Builder " + security);
    switch (security) {
        case SECURITY_NONE:
            // nothing to do
            break;
        case SECURITY_WEP:
            // no longer possible
            break;
        case SECURITY_PSK:
        case SECURITY_EAP:
            wifiNetworkSpecifierBuilder.setWpa2Passphrase(password);
            break;

        default:
            wifiLog("Invalid security type: " + security);
            break;
    }
}
 
Example #2
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;
}
 
Example #3
Source File: ThetaDeviceManager.java    From DeviceConnect-Android with MIT License 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.Q)
public void requestNetwork(final String ssId) {
    WifiNetworkSpecifier specifier = new WifiNetworkSpecifier.Builder()
            .setSsid(ssId)
            .setWpa2Passphrase(parsePassword(ssId))
            .build();
    NetworkRequest request = new NetworkRequest.Builder()
            .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
            .setNetworkSpecifier(specifier)
            .build();
    requestNetwork(ssId, request);
}