Skip to content

Instantly share code, notes, and snippets.

@mhddurrah
Created November 11, 2016 14:09
Show Gist options
  • Save mhddurrah/fe8271e5d366cac0f9d35e5dd60bce84 to your computer and use it in GitHub Desktop.
Save mhddurrah/fe8271e5d366cac0f9d35e5dd60bce84 to your computer and use it in GitHub Desktop.
private void doSwitchToAP() {
final WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
final WifiApControl wifiApControl = WifiApControl.getInstance(this);
if (wifiApControl.isEnabled()) {
WifiConfiguration config = wifiApControl.getConfiguration();
if (config.SSID.equals(HOTSPOT_SSID) && config.preSharedKey.equals(HOTSPOT_PWD)) {
Utilities.longToast("Already configured as hotspot!");
}
}
final WifiConfiguration apConfig = new WifiConfiguration();
apConfig.SSID = HOTSPOT_SSID;
apConfig.preSharedKey = HOTSPOT_PWD;
apConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
apConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
apConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
apConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
apConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
apConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
apConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
apConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
apConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wifiApControl.setWifiApEnabled(apConfig, false);
progressDialog = ProgressDialog.show(this, "", getString(R.string.configuring_access_point));
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
configuredWifis = wifiManager.getConfiguredNetworks();
wifiManager.setWifiEnabled(false);
wifiApControl.enable();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment