Android WIFI How To Detect When Specific WIFI Connection is Available

后端 未结 4 1821
迷失自我
迷失自我 2020-11-28 05:31

I need to detect when I have network connectivity to a SPECIFIC WIFI network.

For example: As soon as you walk into your house, and your phone picks up your home Wi

4条回答
  •  囚心锁ツ
    2020-11-28 06:24

    Use the standard code to list all available networks:

    • start the scan

      String connectivity_context = Context.WIFI_SERVICE;
                  final WifiManager wifi = (WifiManager) getSystemService(connectivity_context);  
      if (wifi.isWifiEnabled()) {
                              wifi.startScan();
                          }
      
    • register a receiver for the data

      IntentFilter i = new IntentFilter();
      i.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
      
      BroadcastReceiver receiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent i) {
              // TODO Auto-generated method stub
              ScanWiFiActivity a = ScanWiFiActivity.instance();
              WifiManager w = (WifiManager) context
                      .getSystemService(Context.WIFI_SERVICE);
              List l = w.getScanResults();
              a.Clear();
              for (ScanResult r : l) {
                                    //use r.SSID or r.BSSID to identify your home network and take action
                  a.setText(r.SSID + "" + r.level + "\r\n");
              }
          }
      };
      registerReceiver(receiver, i);
      

    In the FOR block work your magic and take action when you identify your network by SSID or BSSID

提交回复
热议问题