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)
Related
Is there any way besides the standard ConnectivityManager-API to modify wifi state in android q? I've just read google removed the api in android 10. Im willing to give the app android device administrator status, grant all permissions with adb, etc as I wont publish the app and will only use it for myself.
Your app must be a device owner as a variant to use wifiManager.setWifiEnabled(false) method that is deprecated started from Q.
Deprecation Exemptions:
Device Owner (DO), Profile Owner (PO) and system apps.
WifiManager
You can use the following code to disable wifi :
try {
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
return wifiManager.setWifiEnabled(false);
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
to disconnect wifi :
WifiManager wifiManager = (WifiManager)context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.disconnect();
and the following code to forget a particular wifi network :
try {
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
List wifiNetworks = wifiManager.getConfiguredNetworks();
for (Object wifiConf : wifiNetworks) {
if (((WifiConfiguration) wifiConf).SSID.equalsIgnoreCase(SSID)) {
return wifiManager.removeNetwork(wifiManager.getConnectionInfo().getNetworkId()) && wifiManager.saveConfiguration();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
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
I am developing an Android App. The app will keep trying to connect to an dedicated WiFi AP when started. This is working. However when I tested to power off the AP, so the app must have been disconnected from the AP, my app still return the SSID of the AP when I use WifiManager.getConnectionInfo().getSSID() to check the SSID, why? And how can I update the current SSID when it is not connected, even it return "unknown ssid"?
Below is the method I check the SSID, the app will invoke this method continuously:
WifiManager wifiManager; // <- In MainAativity
// ....
int checkWiFiSSID() {
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()) {
Log.d(TAG, "WiFi is disabled, enable it now");
wifiManager.setWifiEnabled(true);
} else {
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ssid = wifiInfo.getSSID();
// This line always show the AP SSID even the AP is powered down, why??
Log.d(TAG, "AP SSID: " + ssid);
}
}
Thank you.
UPDATE:
Having read the document carefully, I found that if the WifiManager is trying to connect to an AP, even it is not connected, the getSSID() will return its SSID. As my app is keep trying to connect to the AP once it is disconnected. I guess this is the reason, but I am not sure.
I've been working on wifi connecting for a while. I can use an Android device to open wifi hotspot and another Android device connect to it.
but here is a problem. I found out the wifi state will disconnect and reconnect to a wifi it already remembered and the last (I guess when it found the hotspot is ready) disconnect again and reconnect to my wifi hotspot. this process took almost 20~30 seconds.
here is my code:
private void connectToWiFiHotSpot() {
WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
mWifiConfig = new WifiConfiguration();
mWifiConfig.SSID = "\"test5678\"";
mWifiConfig.preSharedKey = "\"12345678\"";
mWifiConfig.priority = 100000;
int res = wifiManager.addNetwork(mWifiConfig);
Log.d("WifiPreference", "add Network returned " + res);
wifiManager.disconnect();
boolean isEnable = wifiManager.enableNetwork(res, true);
Log.d("WifiPreference", "enable Network returned " + isEnable);
wifiManager.reconnect();
}
my question is there any way to let my device connect to my hotspot directly without connecting to what it remembered. speed up and increase performance.
I've already tried set a high priority and remove other remember wifi configurations is not what i want.
Any help I will be appreciated.
OK, I found a solution.
To disable all other configured networks before we add a specified wifi configuration.
WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
//disable others
for (WifiConfiguration wifiConfiguration: wifiManager.getConfiguredNetworks()) {
wifiManager.disableNetwork(wifiConfiguration.networkId);
}
mWifiConfig = new WifiConfiguration();
mWifiConfig.SSID = "\"test5678\"";
mWifiConfig.preSharedKey = "\"12345678\"";
mWifiConfig.priority = 100000;
int res = wifiManager.addNetwork(mWifiConfig);
Log.d("WifiPreference", "add Network returned " + res);
wifiManager.disconnect();
boolean isEnable = wifiManager.enableNetwork(res, true);
Log.d("WifiPreference", "enable Network returned " + isEnable);
wifiManager.reconnect();
it will directly connect to wifi you assigned instead connect to other wifi it remembered first.
and in the end, after you disconnect to that wifi. Don't forget to enable other wifi.
it will connect to wifi it had connected to.
for (WifiConfiguration config: wifiManager.getConfiguredNetworks()) {
wifiManager.enableNetwork(config.networkId, true);
}
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.