SoFunction
Updated on 2025-04-10

Android obtains surrounding WIFI hotspot services

In actual development, we often need to obtain the surrounding WiFi hotspots. I recently made this demo and I will write it out and share it with you. The general idea is this: First, WifiManger obtains WiFi service, and then stores the result in ArrayList<ScanResult>. OK, you can directly look at the code for the specific content:

public class MainActivity extends AppCompatActivity {

  ArrayList&lt;ScanResult&gt; list;  //Storage the list of surrounding wifi hotspot objects  WifiManager wifiManager;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);  //Get system wifi service    list = (ArrayList&lt;ScanResult&gt;)();
    sortByLevel(list);
    init();
  }

  private void init(){
    TextView tv1=(TextView)findViewById(.tv1);
    TextView tv2=(TextView)findViewById(.tv2);
    TextView tv3=(TextView)findViewById(.tv3);

    if ((0).SSID != null &amp;&amp; (1).SSID != null){
      ("The strongest signal is"+(0).SSID);
      ("Signal Second Bit:"+(1).SSID);
      ("Common"+()+"Wifi");
    }

  }

  //Sort the searched wifi from strong to weak according to the signal strength  private void sortByLevel(ArrayList&lt;ScanResult&gt; list) {
    for(int i=0;i&lt;();i++)
      for(int j=1;j&lt;();j++)
      {
        if((i).level&lt;(j).level)  //The level attribute is strength        {
          ScanResult temp = null;
          temp = (i);
          (i, (j));
          (j, temp);
        }
      }
  }

} 

Layout file activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  <TextView
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />
  <TextView
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />
  <TextView
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />
</LinearLayout> 

OK, that's it.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.