简体   繁体   中英

How to take the connected wifi ssid when change the wifi in android

I need to take the SSID of connected wifi in android. And I need to take SSID when wifi network has been changed.

I searched so many things about this, but I did not get a correct answer.

I have understood that for this task need a Broadcast Reciever . But I don't know how to use the broadcast receiver for this. Anyone can help me.

What are the steps I need to follow up?

Below method is to get the SSID of connected Wifi.

    public String getConnectedSSID() {
            WifiInfo wifiInfo = getWifiInfo();
            if (wifiInfo != null && wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {
                Log.i(TAG, "getConnectedSSID" + wifiInfo.getSSID());
                return wifiInfo.getSSID().replace("\"", "");
            } else {
                if (mWifiConnectionListener != null) {
                    mWifiConnectionListener.onError(!mWifiManager.isWifiEnabled() ? WifiConnectionListener.WIFI_ERROR.WIFI_DISABLED : WifiConnectionListener.WIFI_ERROR.WIFI_NOT_CONNECTED);
                }
                return null;
            }
        }

You have to register below broadcast receiver. intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); mContext.registerReceiver(mWifiConnectivityState, intentFilter);

private final BroadcastReceiver mWifiConnectivityState = new BroadcastReceiver() {
    @Override
    public void onReceive(Context c, Intent intent) {
        String action = intent.getAction();

        switch (action) {

            case ConnectivityManager.CONNECTIVITY_ACTION:
                NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
                if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                    switch (networkInfo.getState()) {
                        case CONNECTED:
                            WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                            WifiInfo info = wifiManager.getConnectionInfo();
                            NetworkInfo networkInfo = ((ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
                            boolean isConnected = networkInfo != null && networkInfo.isConnected();
                            if (isConnected)
                                mConnectedSSID = info.getSSID();
                            //register callback and pass SSID as parameter.
                            break;
                        case CONNECTING:
                            break;
                        case DISCONNECTED:
                            break;
                        case DISCONNECTING:
                            break;
                    }
                }
                break;
        }
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM