shahzaib ali
shahzaib ali

Reputation: 37

how to get SSID and BSSID OF Hotspot of my device programmatically

I want to get my own device Wi-Fi SSID and BSSID name. How can I get this? I tried this

WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wifiConfiguration = new WifiConfiguration();
System.out.println("BSSID"+wifiConfiguration.BSSID);

But this code gives the BSSID of the device to which i currently connected but i wants to get my own device BSSID ssid through code?? Please help Me.

Upvotes: 2

Views: 5163

Answers (2)

Jack
Jack

Reputation: 5614

If you want to get the device's hotspot SSID or BSSID, use something like this:

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
Method[] methods = wifimanager.getClass().getDeclaredMethods();
for (Method m: methods) {           
    if (m.getName().equals("getWifiApConfiguration")) {
        WifiConfiguration config = (WifiConfiguration)m.invoke(wifimanager);
        String ssid = config.SSID;
        String bssid = config.BSSID;
    }
}

You can use WifiManager and WifiInfo for the Wifi info the device connected to, like this:

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();
String ssid  = info.getSSID();
String bssid = info.getBSSID();

You would need the following permissions:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Note: since Android 8.0, you would also need location permissions (ACCESS_COARSE_LOCATION) to access the SSID or BSSID because of this, also, I think you need to have the device's location settings turned on for this to work even if you have the location permissions.

Upvotes: 2

Logic
Logic

Reputation: 2258

Add these permissions

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

and then use this method

public static String getBSSID(Context mContext) {
    ConnectivityManager mConnectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if(mNetworkInfo.isConnected()) {
        final WifiManager mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        final WifiInfo mWifiInfo = mWifiManager.getConnectionInfo();
        if(mWifiInfo != null) {
            return mWifiInfo.getBSSID();
        }
    }
    return null;
}

Upvotes: 0

Related Questions