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<ScanResult> 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<ScanResult>)(); 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 && (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<ScanResult> list) { for(int i=0;i<();i++) for(int j=1;j<();j++) { if((i).level<(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.