Question:
Good evening.
I am trying to write android program to control chandelier via webview. I assembled this scheme http://habrahabr.ru/post/159745/ (if anyone is interested, I can give useful tips on assembling this device), the router is configured, the wifi network is called ON-OFF the address of the router is 192.168.1.1 and from the video lesson https:/ /www.youtube.com/watch?v=SrROaEpcIp4 made the browser idle. Everything works, but there are some wishes.
I want to make it so that when the application is turned on, it connects to the ON-OFF wifi network and then opens the router address 192.168.1.1. If it is not in the coverage area or it is not possible to open the router address 192.168.1.1, it displays the corresponding messages. Please, if possible, explain in detail how to do this. Regards Tom
Answer:
Try it. Checked on WiFi WPA2-PSK
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wifiConf = new WifiConfiguration();
wifiConf.SSID = "\"ON-OFF\"";
wifiConf.wepKeys[0] = "\"(пароль)\"";
wifiConf.wepTxKeyIndex = 0;
wifiConf.preSharedKey = "\"(пароль)\"";
wifiConf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
wifiConf.status = WifiConfiguration.Status.ENABLED;
wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wifi.addNetwork(wifiConf);
wifi.saveConfiguration();
List<WifiConfiguration> list = wifi.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
Log.e("SSID", i.SSID);
if(i.SSID != null && i.SSID.equals("\"" + "ON-OFF" + "\"")) {
Log.e("RSSI_VALUE", "NET_ID " + String.valueOf(i.networkId));
wifi.setWifiEnabled(true);
boolean enable = wifi.enableNetwork(i.networkId, true);
Log.e("RSSI_VALUE", "ENABLE_WIFI " + String.valueOf(enable));
break;
}
}
Add to Manifest
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"/ > <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
It takes time to connect, so before loading the page, you should make sure that the connection is completed.