Android Q 10 Connect to network WifiNetworkSpecifier

拈花ヽ惹草 提交于 2020-05-25 02:34:48

问题


Since Android Q doesn't allow the WifiManager to add Networks, they gave the advise to use WifiNetworkSpecifier instead. With the WifiNetworkSuggestionBuilder I was already able to display the notification on the statusbar, that user can join the network. But this API doesn't fulfill my requirements since that I don't the user to have to use the suggestion from the statusbar.

With WifiNetworkSpecifier I was also already able to display a popup about joining the network and the app also established a connection to the app. But that wifi connection seems only be available in the scope of the app. How is it possible to overcome this scope of the app, so other apps and for example also the browser is able to use this new established connection? Below is my code

    WifiNetworkSpecifier.Builder builder = new WifiNetworkSpecifier.Builder();
    builder.setSsid("abcdefgh");
    builder.setWpa2Passphrase("1234567890");

    WifiNetworkSpecifier wifiNetworkSpecifier = builder.build();
    NetworkRequest.Builder networkRequestBuilder = new NetworkRequest.Builder();
networkRequestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);     
networkRequestBuilder.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);            
networkRequestBuilder.addCapability(NetworkCapabilities.NET_CAPABILITY_TRUSTED);            
networkRequestBuilder.setNetworkSpecifier(wifiNetworkSpecifier);
NetworkRequest networkRequest = networkRequestBuilder.build();
    ConnectivityManager cm = (ConnectivityManager) App.getInstance().getBaseContext().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm != null) {
        cm.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(@NonNull Network network) {
                //Use this network object to Send request.
                //eg - Using OkHttp library to create a service request

                super.onAvailable(network);
            }
        });

回答1:


A bit late to the party, but maybe it will help someone else that encounters this problem.

It seems like you are right. In android Q once the app is killed, the system automatically disconnects the WiFi network we've connected to through WifiNetworkSpecifier and there is no way to prevent the system from doing so.

The best solution I've come up with, is using WifiNetworkSpecifier with WifiNetworkSuggestion. This allows us to use WifiNetworkSpecifier while we are using our app, and suggest wifi networks for the system to auto connect to once the system disconnects from our WiFi due to the app being terminated.

Here is some sample code:

    WifiNetworkSuggestion.Builder builder = new WifiNetworkSuggestion.Builder()
        .setSsid("YOUR_SSID")
        .setWpa2Passphrase("YOUR_PASSWORD")
    WifiNetworkSuggestion suggestion = builder.build();

    ArrayList<WifiNetworkSuggestion> list = new ArrayList<>();
    list.add(suggestion);

    WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    int status = manager.addNetworkSuggestions(list);

    if (status == STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
        //We have successfully added our wifi for the system to consider
    }

Cheers,



来源:https://stackoverflow.com/questions/57929863/android-q-10-connect-to-network-wifinetworkspecifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!