Android 6.0 WiFiの変更について

5461 ワード

https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-network http://m.blog.csdn.net/article/details?id=51042851 http://seewhy.leanote.com/post/Android-6.0-Changes最近,アンドロイド6.0のマシンでは,指定WiFiに切り替えることができず,アンドロイド6.0の問題というより,自分の以前のコードが仕様に合わないことが分かった.
質問:
指定したWiFiに接続し、私が以前使っていたコードは以下の通りです.
//          
    public void addNetwork(WifiConfiguration wcg) {
        int wcgID = mWifiManager.addNetwork(wcg);
        boolean b = mWifiManager.enableNetwork(wcgID , true);
     }

Android 6.0でWifiManager addNetwork(WifiConfiguration config)は、同じssidを追加すると-1に戻ります.このとき、この-1(NetWorkId)をenableNetwork(-1,true)に転送すると、WiFiに接続できないに違いありません.
原因を分析する
公式の文章(この文章は翻訳がいい)では、アンドロイド6.0におけるWiFiの変更について紹介しています.次のようになります.
Wi-Fi and Networking Changes
This release introduces the following behavior changes to the Wi-Fi and networking APIs.
  • Your apps can now change the state of WifiConfiguration objects only if you created these objects. You are not permitted to modify or delete WifiConfiguration objects created by the user or by other apps.
  • Previously, if an app forced the device to connect to a specific Wi-Fi network by using enableNetwork() with the disableAllOthers=true setting, the device disconnected from other networks such as cellular data. In This release, the device no longer disconnects from such other networks. If your app’s targetSdkVersion is “20” or lower, it is pinned to the selected Wi-Fi network. If your app’s targetSdkVersion is “21” or higher, use the multinetwork APIs (such as openConnection(), bindSocket(), and the new bindProcessToNetwork() method) to ensure that its network traffic is sent on the selected network.

  • 理由:WiFiリストをスキャンすると、接続に成功したWiFiのWifiConfigurationが自動的に作成され、addNetwork()を使用してこのWiFiを追加すると、システムが作成したWifiConfigurationオブジェクトが変更されるので、addNetwork()は-1を返します.
    addNetworkのソースコードで次のことがわかりました.
     public int addNetwork(WifiConfiguration config) {
            if (config == null) {
                return -1;
            }
            //     WifiConfiguration
            config.networkId = -1;
            return addOrUpdateNetwork(config);
        }

    Android 5.1システムでは、既にSSIDを指定したWifiConfigurationオブジェクトが存在していても、再びaddNetwork()を使用して同じネットワークを追加し、追加に成功して戻ってきたIDを追加するのは追加前と同じで、WifiConfigurationオブジェクトを返したのではないかと推測しますが、WifiConfigurationオブジェクトは変更されています
    ただし、手動でWLAN設定でネットワークAを削除し、再度ネットワークAに接続すると、ネットワークAのWifiConfigurationのIDが異なることに気づき、addNetwork()でWifiConfigurationオブジェクトを返さないとIDが異なるはずです.
    解決方法:
    次は大まかな考え方で、コードは完全ではありません.
    private void Wificonnect() {
            //      
            WifiConfiguration mWifiConfiguration;
            WifiManager mWifiManager = new WifiAdmin(this);
            String SSID = wifiName.replace("+", " ");
            String password = wifiPwd;
            //    SSID WifiConfiguration     
            WifiConfiguration tempConfig = IsExsits(SSID);
            if (tempConfig == null) {
                //      WifiConfiguration ,CreateWifiInfo()      
                mWifiConfiguration = CreateWifiInfo(SSID, password, 3);
                int wcgID = mWifiManager.addNetwork(mWifiConfiguration );
                boolean b = mWifiManager.enableNetwork(wcgID, true);
            } else {
            //    WiFi,    WiFi       
                mWifiConfiguration = tempConfig;
                boolean b = mWifiManager.enableNetwork(mWifiConfiguration .networkId, true);
            }
    
    }
    
    //        WiFi       SSID WifiConfiguration 
    public WifiConfiguration IsExsits(String SSID) {
            List<WifiConfiguration> existingConfigs = mWifiManager
                    .getConfiguredNetworks();
            for (WifiConfiguration existingConfig : existingConfigs) {
                if (existingConfig.SSID.equals("\"" + SSID + "\"")) {
                    return existingConfig;
                }
            }
            return null;
    }