Sajmoon
Lurker
Hi Everyone...
I wonder if it is possible to enable hotspot from java code.
I got all devices running android 6.0+ and can't test current code on older androids.
Tried a few different examples and nothing works.
Example1 :
Example 2:
Example 3:
Is there anything I'm missing?
btw...targeted api are 16-25.
Just in case...I added all required permissions to manifest
I wonder if it is possible to enable hotspot from java code.
I got all devices running android 6.0+ and can't test current code on older androids.
Tried a few different examples and nothing works.
Example1 :
Java:
public boolean isHotspotON(){
WifiManager wifiMgr = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
try{
Method method = wifiMgr.getClass().getDeclaredMethod("isWifiApEnabled");
method.setAccessible(true);
return (Boolean) method.invoke(wifiMgr);
}
catch(Throwable ignoreException)
{
return false;
}
}
public void EnableHotspot()
{
if(!this.isHotspotON())
{
WifiManager wifiMgr = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wifiConfig = new WifiConfiguration();
try{
Method method = wifiMgr.getClass().getMethod("setWifiApEnabled",WifiConfiguration.class,boolean.class);
method.invoke(wifiMgr,wifiConfig,!isHotspotON());
}
catch(Exception e) {
e.getStackTrace();
}
}
}
Example 2:
Java:
public void EnableHotspov2()
{
if(!this.isHotspotON())
{
WifiManager wifiMgr = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = "Example AP";
wifiConfig.preSharedKey = "TestPassword";
wifiConfig.hiddenSSID = false;
wifiConfig.allowedKeyManagement.set((WifiConfiguration.KeyMgmt.WPA_PSK));
wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
try{
Method method = wifiMgr.getClass().getMethod("setWifiApEnabled",WifiConfiguration.class,boolean.class);
method.invoke(wifiMgr,wifiConfig,false);
wifiMgr.saveConfiguration();
}
catch (Exception e)
{
e.getMessage();
}
}
}
Java:
public void EnableHotspot3()
{
try {
WifiManager mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
mWifiManager.setWifiEnabled(false);
WifiConfiguration conf = getWifiApConfiguration();
mWifiManager.addNetwork(conf);
mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class).invoke(mWifiManager, conf, true);
} catch (Exception e) {
e.printStackTrace();
}
}
public static WifiConfiguration getWifiApConfiguration() {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "DupaHotspot";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
return conf;
}
Is there anything I'm missing?
btw...targeted api are 16-25.
Just in case...I added all required permissions to manifest