简体   繁体   中英

Get SSID when WIFI Connected?

I want a app to display the WIFI SSID when connected to wifi using toast message and i also want to this happen in killed state also how can i do it. Thanks!reply in Advance Main Activity

public class MainActivity extends AppCompatActivity {

    private PendingIntent pendingIntent;

    TextView textConnected, textSsid, textBssid, textMac, textRssi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textConnected = (TextView) findViewById(R.id.Connected);

        textSsid = (TextView) findViewById(R.id.Ssid);
        textBssid = (TextView) findViewById(R.id.Bssid);
        textMac = (TextView) findViewById(R.id.Mac);
        textRssi = (TextView) findViewById(R.id.Rssi);

        /*DisplayWifiState();*/

        this.registerReceiver(this.myWifiReceiver,
                new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

    }

    private BroadcastReceiver myWifiReceiver
            = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (networkInfo.isConnected()) {
                final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
                if (connectionInfo != null
                        && !(connectionInfo.getSSID().equals(""))) {
                    String ssid = connectionInfo.getSSID();

                    android.widget.Toast toast = android.widget.Toast.makeText(
                            context, "Wifi Connected to " + ssid, android.widget.Toast.LENGTH_LONG);
                    toast.show();
                } else {
                    android.widget.Toast toast = android.widget.Toast.makeText(
                            context, "Connect to WiFi first", android.widget.Toast.LENGTH_LONG);
                    toast.show();
                }

            }
        }
    };




        /*private void DisplayWifiState(){

            ConnectivityManager myConnManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
            NetworkInfo myNetworkInfo = myConnManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            WifiManager myWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
            WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();

            textMac.setText(myWifiInfo.getMacAddress());

            if (myNetworkInfo.isConnected()){

                textConnected.setText("--- CONNECTED ---");

                textSsid.setText(myWifiInfo.getSSID());
                textBssid.setText(myWifiInfo.getBSSID());


                textRssi.setText(String.valueOf(myWifiInfo.getRssi()));
            }
            else{
                textConnected.setText("--- DIS-CONNECTED! ---");
                textSsid.setText("---");
                textBssid.setText("---");
                textRssi.setText("---");
            }

        }*/

    }
if (!wifiManager.isWifiEnabled()) {
        wifiManager.setWifiEnabled(true);
}
List<WifiConfiguration> configuredNetworks =  wifiManager.getConfiguredNetworks();
    boolean found = false;
    for (WifiConfiguration wifiConfig : configuredNetworks) {
        if (wifiConfig.SSID != null && (wifiConfig.SSID.toString().replaceAll("\"", "")).contains(YOUR_SSID)) {
          //YOUR CODE GOES HERE
            break;
        }
    }

This works for me

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