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
Related
I want to know is there any way to determine whatever I am logging should go through WIFI instead of device network,Is there any methods as part of Log entries SDK
Try this code for wifi connection or not.
private boolean checkConnectedToDesiredWifi() {
boolean connected = false;
WifiManager wifiManager =(WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiManager.getConnectionInfo();
String desiredMacAddress = wifiInf.getMacAddress();
WifiInfo wifi = wifiManager.getConnectionInfo();
if (wifi != null) {
// get current router Mac address
String bssid = wifi.getBSSID();
connected = desiredMacAddress.equals(bssid);
}
return connected;
}
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 want to get the list of available routers, their SSID's and signal strengths, by doing a wifi scan.
Can you please help me out with the code?
P.S.: I am new to android. A related Example will be very much appreciated.
Below code is used to get SSID, BSSID, IP Address & RSSI...
WifiManager wifi = = (WifiManager) uMobility.getContext().getSystemService(Context.WIFI_SERVICE);
String ssid = wifi.getConnectionInfo().getSSID(); // for geting SSID
String bssid = wifi.getConnectionInfo().getBSSID(); // for geting BSSID
int ipAddress = wifi.getConnectionInfo().getIpAddress(); // for geting IP Address
int rssi = wifi.getConnectionInfo().getRssi(); // for geting RSSI
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
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.