L.Chan
L.Chan

Reputation: 11

How can my app change the network to another WiFi in android 6.0 mobilephone?

Process as below (Test on platform android api level =23 an android 6.0 mobilephone):

  1. In my app i do as :

    mWifiManager.addNetwork(wcg);
    boolean b = mWifiManager.enableNetwork(wcgID, true);
    

    but b always return false;

    log print:

    Not authorized to remove network 
    ......
    Not authorized to update network  
    
  2. Then I try the a Method in the WifiManager(Marked @Hide):

    connect(int networkId, ActionListener listener) 
    

    by reflectMethod , the result was also failed . Print as:

    java.lang.reflect.InvocationTargetException
    

Upvotes: 0

Views: 809

Answers (2)

Godfather
Godfather

Reputation: 848

In Lollipop you should try checking active ssid is same otherwise disable the network.

 int netId = mWifiManager.addNetwork(conf);
if (netId == -1) {
                    Log.d(this, "Failed to set the settings for  " + mNetworkSsidToConnect);
                    final List<WifiConfiguration> mWifiConfiguration = mWifiManager.getConfiguredNetworks();
                    for (int i = 0; i < mWifiConfiguration.size(); i++) {
                        String configSSID = mWifiConfiguration.get(i).SSID;
                        Log.d(this, "Config SSID" + configSSID + "Active SSID" + conf.SSID);
                        netId = mWifiConfiguration.get(i).networkId;
                        if (configSSID.equals(conf.SSID)) {
                            Log.d(this, "network id" + netId);
                            break;
                        } else {
            **/*If SSID is not same disable other network here */**
                            mWifiManager.disableNetwork(netId);
                        }
                    }
                }

Upvotes: 0

Tomasz W
Tomasz W

Reputation: 2076

That looks like a legitimate case.

Check WifiStateMachine code, particularly recordUidIfAuthorized (line 454) which is being used by all the remove or update calls.

If I'm reading this right, not everyone is authorized to modify or remove network configurations. You would have to be an owner of that configuration (= this configuration would have to be submitted by your process in the first place) to get the job done.

Upvotes: 2

Related Questions