Android gets the wifi list. If you ignore this details, your software may crash. androidwifi

Source: Internet
Author: User

Android gets the wifi list. If you ignore this details, your software may crash. androidwifi

I. Business Description

Recently, the company has a small demand,The user clicks the wifi scan button (Note: The user actively clicks the wifi scan button)The app scans nearby wifi and displays it in listView. That's all. apps do not need to connect to a certain wifi. It seems simple. If not, the app may be directly suspended.

II. The Code is as follows:

Register to receive broadcasts that scan for wifi

private void registerBroadcast(){        IntentFilter intentFilter = new IntentFilter();        intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);        mContext.registerReceiver(mBroadcastReceiver,intentFilter); }

Start scanning

@ Override public void onClick (View v) {switch (v. getId () {case R. id. scanWifi: if (! WifiManager. isWifiEnabled () {// enable wifi wifiManager. setWifiEnabled (true);} wifiManager. startScan (); wifiSsidRightIcon. setEnabled (false); break ;}}

Obtain the broadcast of scan results

Private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver () {@ Override public void onReceive (Context context, Intent intent) {String resultAction = intent. getAction (); Logger. e (TAG + "receives wifi-Related broadcasts:" + resultAction );
// Other service code is omitted, such as how many wifi hotspots are displayed, and refreshing the list of Wi-Fi hotspots
}}

Iii. Problem Description

After the development is complete, when my colleagues test this module, they directly tell me that the app has not clicked to scan for wifi, and I used to see that the app has actually crashed, which is mandatory each time, however, the following information is displayed in the recorded log:

03-19 15:42:56. 471 2112-2112/com. advert E/PRETTYLOGGER: appsconfighelperfragment $2. onReceive (ConfigHelperFragment. java: 376) 03-19 15:42:56. 471 2112-2112/com. advert E/PRETTYLOGGER: appsconfighelperfragment: receives wifi-related broadcast: android.net. wifi. SCAN_RESULTS 03-19 15:42:56. 487 2112-2112/com. advert I/PRETTYLOGGER: appsconfighelperfragment $2. onReceive (ConfigHelperFragment. java: 387) 03-19 15:42:56. 488 2112-2112/com. advert I/PRETTYLOGGER: appsconfighelperfragment: scan result: Router-DK 03-19 15:42:58. 838 2112-2112/com. advert E/PRETTYLOGGER: appsconfighelperfragment $2. onReceive (ConfigHelperFragment. java: 376) 03-19 15:42:58. 839 2112-2112/com. advert E/PRETTYLOGGER: appsconfighelperfragment: receives wifi-related broadcast: android.net. wifi. SCAN_RESULTS 03-19 15:43:06. 496 2112-2112/com. advert E/PRETTYLOGGER: appsconfighelperfragment $2. onReceive (ConfigHelperFragment. java: 376) 03-19 15:43:06. 496 2112-2112/com. advert E/PRETTYLOGGER: appsconfighelperfragment: receives wifi-related broadcast: android.net. wifi. SCAN_RESULTS 03-19 15:43:06. 515 2112-2112/com. advert I/PRETTYLOGGER: appsconfighelperfragment $2. onReceive (ConfigHelperFragment. java: 387) 03-19 15:43:06. 515 2112-2112/com. advert I/PRETTYLOGGER: appsconfighelperfragment: scan result: YTC

The above information is: After you click to scan for wifi, you will receive a broadcast of the Wi-Fi scan result. The key is that you have not manually clicked to start the scan or received the broadcast, in addition, I received this broadcast continuously. At that time, I was a little confused, and Baidu did not find a similar problem. Let's calm down and think about it. It must have been another program that triggered a Wi-Fi scan. So I only received this broadcast, but I can't say it. Why do I keep receiving this broadcast? So I opened the settings-WLAN and saw the following scene:

I watched the Wi-Fi list for nearly 30 seconds in a row. I carefully checked the Wi-Fi list in Figure 1 and figure 2. the wifi list has changed, the cause of software failure is found, because after the android device wifi is turned on, it will automatically scan nearby wifi. If there is available wifi, the device will be connected directly. If there is no available wifi, the device will continue searching until wifi is disabled or available wifi is found.

Why is there no problem in my local environment, and my colleague's test showed up. It is precisely because the device I tested has been connected to an available wifi hotspot,My colleague's device is new, and the Wi-Fi switch is turned on before.So the app fails during the test. Knowing this is the reason, it is easy to solve this problem. When you enter this activity, make a judgment. If the current wifi is enabled, close it directly.

The Code is as follows:

  

 
public static void closeWifi(WifiManager wifiManager) {
if (wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
}
}
 

 

 

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.