How do I change the Portable Hotspot's SSID in Android programmatically?
10,368
The best place to look for clues is in the AOSP source code itself. Here's the relevant part: http://androidxref.com/4.4_r1/xref/frameworks/base/wifi/java/android/net/wifi/WifiManager.java#1147
The following code worked for me:
public static boolean setHotspotName(String newName, Context context) {
try {
WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
Method getConfigMethod = wifiManager.getClass().getMethod("getWifiApConfiguration");
WifiConfiguration wifiConfig = (WifiConfiguration) getConfigMethod.invoke(wifiManager);
wifiConfig.SSID = newName;
Method setConfigMethod = wifiManager.getClass().getMethod("setWifiApConfiguration", WifiConfiguration.class);
setConfigMethod.invoke(wifiManager, wifiConfig);
return true;
}
catch (Exception e) {
e.printStackTrace();
return false;
}
}
Don't forget to add the following lines into your AndroidManifest.xml
:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Author by
salih
Updated on June 13, 2022Comments
-
salih almost 2 years
I want change the Hotspot SSID! Can anybody help?
From Android turn On/Off WiFi HotSpot programmatically, here is my code to toggle the Portable Hotspot:
import android.content.*;import android.net.wifi.*; import java.lang.reflect.*; public class ApManager { //check whether wifi hotspot on or off public static boolean isApOn(Context context) { WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE); try { Method method = wifimanager.getClass().getDeclaredMethod("isWifiApEnabled"); method.setAccessible(true); return (Boolean) method.invoke(wifimanager); } catch (Throwable ignored) {} return false; } // toggle wifi hotspot on or off public static boolean configApState(Context context) { WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE); WifiConfiguration wificonfiguration = null; try { // if WiFi is on, turn it off if(isApOn(context)) { wifimanager.setWifiEnabled(false); } Method method = wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class); method.invoke(wifimanager, wificonfiguration, !isApOn(context)); return true; } catch (Exception e) { e.printStackTrace(); } return false; } } // end of class
-
Saran Tunyasuvunakool about 9 yearsI forgot to add: you may piss off your user when she realises that her Portable Hotspot name has been inexplicably changed the next time she tried to tether and cannot find the usual SSID. Tread carefully here.
-
salih about 9 yearsBut can u please say me where to call
-
Saran Tunyasuvunakool about 9 yearsWhere to call what? You can just call
setHotspotName
as given above whenever you want to change the Hotspot SSID. Surely you decide where you want that to happen? -
Saran Tunyasuvunakool about 9 yearsSorry, it does work. I actually tested this. Please give us the details of why it doesn't work for you then we might be able to help.
-
Vineeth Kuttipurath kottayodan over 8 yearsHow to make hotspot security open?
-
Luiz Menezes almost 7 yearsCan I use wifiConfig.wepKeys[0] to change the hotspot password as well?
-
Dinith over 3 years@Saran are there any methods to on and off hotspot programmatically?