Can't set "WifiConfiguration" when enabling wifi-hotspot using "setWifiApEnabled" - android

I'm trying to set my Android device to be an Access-Point using the code I've seen here before:
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiConfiguration netConfig = new WifiConfiguration();
netConfig.SSID = "MyAccessPoint";
Method method = wifi.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method.invoke(wifi, netConfig, true);
now, I managed to turning it on but without the SSID which I set in WifiConfiguration.
This is driving me crazy.
Anyone?

Before Invoking the Method "setWifiApEnabled" you need to call "getWifiApConfiguration" to get the default WifiConfiguration
Then change the SSID and Password and then invoke "setWifiApConfiguration" with the modified WifiConfiguration and after that call "setWifiApEnabled"
Here is the Code.
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
getWifiConfig = wifi.getClass().getMethod("getWifiApConfiguration",null);
WifiConfiguration myConfig = (WifiConfiguration) getWifiConfig.invoke(wifi,null);
myConfig.SSID = "Hello World";
setWifiConfig = wifi.getClass().getMethod("setWifiApConfiguration",WifiConfiguration.class);
setWifiConfig.invoke(wifi,new Object[]{myConfig,true});
enableWifi = wifi.getClass().getMethod("setWifiEnabled",WifiConfiguration.class,boolean.class);
enableWifi.invoke(wifi,null,true);

See how I got this working at Android 2.3 wifi hotspot API.

Related

Programmatically connecting to a specific wifi

I would like to connect to specific wifi programmatically in android. I referred lot of links. But still not yet achieve the goal. I am following below code to connect specific wifi.
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiConfiguration configuration = new WifiConfiguration();
configuration.SSID = String.format("\"%s\"", "ssid");
configuration.preSharedKey = String.format("\"%s\"", "paasword");
configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
int netId = wifiManager.addNetwork(configuration);
wifiManager.disconnect();
wifiManager.enableNetwork(netId,true);
wifiManager.reconnect();
When I was try to run this code stuff.. wifi connection will be disconnected and again automatically connected previous wifi network.I am using redmi 6a mobile(version 9) for testing purpose.
Can any one guide me how to resolve this one.
Thanks in advance.
I believe you need to loop through all available networks, then connect to right one if it is available.
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
break;
}else{Log.e("TAG","Network Not Available")}
}

how to get BSSID of Hotspot

I need to get bssid of my hotspot using android.
WifiManager wifimanager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
Method[] methods = wifimanager.getClass().getDeclaredMethods();
for (Method m : methods) {
if (m.getName().equals("getWifiApConfiguration")) {
WifiConfiguration config = null;
config = (WifiConfiguration) m.invoke(wifimanager);
String ssid = config.SSID;
String bssid = config.BSSID;
System.out.println(config);
}
}
i tryed using above code. but it give bssid null value
Wifi.Configuration.BSSID does not return the BSSID of the AP on your device but is used to further narrow down the network selection when connecting to a Wi-Fi in station mode.
If set, the device will only connect to a network with exactly that BSSID.
For details, see:
https://developer.android.com/reference/android/net/wifi/WifiConfiguration.html#BSSID

How does ShareIt create and configure hotspot

I have to create a Wifi Hotspot with specific SSID and PASSWORD dynamically in my Android app project. I checked the ShareIt mobile app which creates a hotspot with SSID and PASSWORD and the receiver will connect to that hotspot, am expecting something similar to that.
Requirement is like, Android app should be able to create the Wifi hotspot with specific SSID and PASSWORD which will get from the server.
WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
WifiConfiguration wificonfiguration = null;
try {
wificonfiguration = new WifiConfiguration();
wificonfiguration.SSID = apName;
// if WiFi is on, turn it off
if(isApOn(context)) {
wifimanager.setWifiEnabled(false);
// if ap is on and then disable ap
disableAp(context);
}
Method method = wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method.invoke(wifimanager, wificonfiguration, true);
return true;
}
catch (Exception e) {
e.printStackTrace();
}
I tried this code snippet, which is gives an error NoSuchMethodException.
Checking for Android version 9 Pie
Any help will be appreciated.
App like ShareIt is creating hotspot using Local Only Hotspot
But in this case android is creating hotspot with a ssid and password that we cannot configure (Android is choosing a random ssid and password)

Android wifimanager reset all settings

I tried to create an application for wifi connection. After running some piece of code, i can't even connect my phone to the wifi. I have this piece of code
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", "XXX");
wifiConfig.preSharedKey = String.format("\"%s\"", "xxxxxxxxxxx");
WifiManager wifiManager = (WifiManager)fragmentActivity.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
int netId = wifiManager.addNetwork(wifiConfig);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
The method addNetWork() always returns -1. I'd like to know what piece of code I have to write to reset all wifi settings to find my wifi back.

How do I connect to an available SSID Wifi network in Android?

How do I connect to a particular Wifi network using the SSID? I have tried using the code below.
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiConfiguration config = new WifiConfiguration();
if(wifi.startScan()){
List<ScanResult> results = wifi.getScanResults();
for(int i=0;i<results.size();i++)
{ Log.e("VALUE"," "+results.get(i).toString());
Log.e("",""+results.get(i).SSID);
if(results.get(i).SSID.equalsIgnoreCase("\"MAC\""))
{
Log.e("","mac.....");
try{
String ssid="\""+results.get(i).SSID+"\mac"";
Log.e("SSId"," "+ssid);
config.SSID=ssid;
}catch(Exception e){Log.e("","Error : "+e.toString());}
config.preSharedKey="0a0b0f62170ecc5bcf721b6ff170b8b560101b5d56b00a26abec217e0bb4aa1f";
config.status=WifiConfiguration.Status.ENABLED;
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
int res=wifi.addNetwork(config);
Log.e("ENABLE ",""+wifi.enableNetwork(res, false));
break;
}
}
}
I have not found any information anywhere on how to do this? Where am I going wrong?
Your problem may be related to the problem I was having. If you take the SSID value directly from a scan result it is not in double quotes, but for some reason WifiConfiguration requires that the SSID is in double quotes. Please see my answer.
https://stackoverflow.com/a/12616521/178931

Categories

Resources