Android won’t connect to Wifi

Since Android has a Linux-based kernel, the Wifi interface is managed by the wpa_supplicant as on most Unix-based systems. My personal experience during the last years of using wpa_supplicant is that there are many times troubles with it which are hard to debug and even sometimes not reproducable. On the other hand it often works very well. I am not sure but I think in many cases the reason for this “unstable” behavior is that many wireless network card APIs are not documented by the vendors (at least for the public) and they use closed-source firmware. Unfortunately, this makes life really difficult.

Of course I use Wifi with my Android smartphone. I have a list of networks stored to which it will connect automatically, if in range. During the last days a “played” a lot with Android networking (see also the article about OpenVPN). During my experiments I rebooted the phone several times. After a while it did not find any Wifi network anymore although I knew that they are there (I didn’t move and I still found them with my notebook). Switching Wifi off/on didn’t solve it as well as rebooting. I remember that I had the same problem several months ago. The solution then was to delete all stored networks in the list and turn Wifi off and on. It will immediately work again.

But what is the reason for that? I do not know what exactly I did (switching Wifi off/on, rebooting,…) but I somehow reproduced it. I logged into my smart phone using `adb shell` and had a look at the logs. Most processes log to kernel log devices which are found in /dev/log/. Unfortunately, they produce binary output but the command line tool `logcat` will convert it to  be human readable. Thus, you simply can look at all wpa_supplicant messages with the command `logcat | grep wpa`. Everything looks ok so far although there is a lot of weird debugging output. I had a look at the configuration file which is found at /data/misc/wifi/wpa_supplicant.conf, at least on my system. You can find out the location of the config file with the command `ps w | grep wpa` (Wifi must be switched on). The file shows the list of networks as usual but in the beginning there was the configuration line “ap_scan=2”. I remembered that I had troubles with ap_scan two years ago on my notebook.

Start the `wpa_cli` (while wpa_supplicant is running) and type `ap_scan 1`. In my case it immediately found all networks again. Great success! Just save the config with `save_config` and it will also work the next time. Have a look at the config file (`cat /data/misc/wifi/wpa_supplicant.conf`) to make sure that it worked. Optionally, you could of course stop the wpa_supplicant and simply edit the wpa_supplicant.conf with vi to set ap_scan=1.

What I could not figure out yet is why it switched ap_scan from 1 to 2. Maybe I’ll investigate that.

1 comment

    • manuman on October 7, 2012 at 2:10 pm

    Thank you! Your post was a huge help.

Comments have been disabled.