Android 5および6のWiFiネットワークに接続するのに苦労していますが、他の同様の質問は私にとってはうまくいかないようです。 Androidで同じコードを使用することができます。4.4.2Android 5および6でWiFiネットワークに自動的に接続
下記のコードスニペットを追加してください。
String networkSSID = getSsid();
String networkPass = getNetworkPass();
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
conf.status = WifiConfiguration.Status.ENABLED;
conf.priority = 40;
conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
conf.preSharedKey = "\""+ networkPass +"\"";
int value = mWifiManager.addNetwork(conf);
Log.i(TAG_CHECK, "Connecting to : " + value);
boolean enabled = mWifiManager.enableNetwork(value, true);
Log.i(TAG_CHECK, "enabled to : " + enabled);
mWifiManager.reconnect();
これは私が気づいたものです。
mWifiManager.addNetwork(CONF)
戻りアンドロイド5(電話)とAndroid 6(タブ)といくつかの(+)整数。
しかし、Wi-Fi設定を開く(何もする必要はなく、そこに接続するだけです)、または手動で電源を切ってからWi-Fiをオンにして、自動的に接続する必要があります。
ネットワークへの接続を検出するリスナーは、どちらのバージョンでも接続が確立されたことを正確に知るためのものです。以下に与えられた権限の写真を添付してください。
私が紛失しているものは何ですか?
EDIT
:WifiManagerクラスを参照すると、アクセスポイントはWIFI_AP_STATE_DISABLED状態のままです。私はまた、別のAndroid Mの携帯電話を試している間、すべてが期待通りに機能することを強調する必要があります。
EDIT2
I have the following observations.
1. The issue so far is specific to 1 android 6.0.1 Samsung tablet and 1 android 5.0.2 Micromax phone. It works just fine on 3 other android 6.0.1 phones, 1 android N phone and Android 4.4.2 phone.
2. The access point ends up in wifi_ap_disabled state in the problematic cases consistently. Both addNetwork and enableNetwork calls are affirmative.
3. These access points are not that of a router wifi but that of other phones that broadcast. The problematic phones can programatically connect to wifi hotspots (setup manually and not in the programatic way as I would like to) without any issue.
4. Mobile data enabled/disabled state or wifi state with a different connected network doesn't change the dynamics for both working and problematic phones.
This makes me think that it is a combination of phones/tabs (and not OS) and the access point broadcast configuration. Do you think I should be playing around with some config parameters?
Edit 3 - Important Update
So the wifimanager is obtained like below
WifiManager mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
I instantiate mWifiManager in a service (inside onCreate method) and use that later to scan and connect with inputs from front-end activity. While it doesn't work in the way as described earlier, using the same snippet seems to work when the enableNetwork call is done right in the onCreate method - working just as expected. It also works fine when called from front-end activity but only when I don't instantiate mWifiManager in the service.
Though I would expect only one reference to a system service - WifiManager i.e, I think the subsequent usage of that reference (or a different reference to WifiManager) gets a lower priority (or gets deprioritized by the previous reference) and hence doesn't get completely executed leaving the access point in disabled state and requiring manual intervention to help complete the execution by WifiManager and other wifi system services.
Also, I notice the same in Android 5.0.2 phone, the asynchronous enableNetwork does get executed, but it takes some time to execute it.
My questions
1. It is no more a question about the correctness of the code. Services in general have lesser priority compared to front-end threads So, is there is way I could prioritise the enableNetwork call so that it gets immediately executed?
2. What happens in case of multiple references to WifiManager?
特定のSSIDに接続しようとしているときに、既にモバイルデータやその他のWiFI SSIDに接続していますか? '' 'ConnectivityManager'''を使って' '' 'NetworkCallback''を追加し、あなたが接続しようとしているSSIDについて' '' onNetworkAvailable() '' 'を得られるかどうか確認できますか? '' '' NETWORK_STATE_CHANGED_ACTION'''と '' WIFI_STATE_CHANGED_ACTION''のブロードキャストを登録して、接続しようとしている間にネットワークと無線LANのさまざまな状態を見ることをお勧めします。 – Nishkarsh
状態の変化を検出するためにリスナーを追加しましたが、コールバックはまったくありません。私の観察でいくつかの編集を加えました。 – user2489122
サービスからWifiManagerのインスタンスを取得する特別な理由はありますか?あなたはあなたの '' 'サービス' 'と' '活動' 'の間でインスタンスを共有していますか、' '' '' 'WifiManager''と対話している' '' 'サービス' 'だけですか?また、これらのメソッドはすべて、さまざまな状態の状態変更コールバックを提供する必要があります。私はそれが複数の参照ではなく、イベントの同期に関する問題だとは思わない。 '' 'enableNetwork()' '自身が指定されたnetworkIdに接続しようとするので、すぐに実行する必要はありません(接続を試みるためにreconnect()は必要ありません) – Nishkarsh