Android: Simulate WiFi in the emulator?

匿名 (未验证) 提交于 2019-12-03 02:30:02

问题:

I would like to check from my app whether the device has WiFi connectivity, but in order to do that, I must first find a way to get "WiFi" in the emulator. Just going to Settings -> Wireless controls -> Wifi says Unable to start Wi-Fi, while logcat says:

E/WifiService(  566): Failed to load Wi-Fi driver. D/SettingsWifiEnabler(  695): Received wifi state changed from Unknown to Enabling D/SettingsWifiEnabler(  695): Received wifi state changed from Enabling to Unknown 

How can I simulate WiFi connectivity in the emulator?

回答1:

Currently, while you can use your computer's Internet connection in the emulator, it's not reliable to use it for simulating Wi-Fi. The WifiManager gets confused when you switch/want to test your connectivity state.

Even in Android 1.5, there were some problems with it (Issue 2571 - android), and while you can use your Internet for simulating HTTP connections and downloading data, finer control over Wi-Fi connectivity should better be done with a device.



回答2:

Since this seems to be a popular question, I'll go ahead and add an updated answer. The SDK still doesn't support WiFi emulation, but Genymotion does WiFi by default, so you can use that to develop/test apps with specific WiFi functionality.



回答3:

Starting from revision 26.1.3, the Android emulator now supports connecting to WiFi on a Nougat image (API 25).

Added support for Wi-Fi in some system images (currently only API level 25). An access point called "AndroidWifi" is available and Android automatically connects to it.

Source: https://developer.android.com/studio/releases/emulator.html#26-1-3

You can check the version of your emulator by going into emulator settings (three dots) > Help > About. Also note that if an AVD was created before version 26.1.3 was released, you will have to recreate your AVD. I read somewhere that wiping data might also work, but I did not try that.



回答4:

In theory, linux (the kernel underlying android) has mac80211_hwsim driver, which simulates WiFi. It can be used to set up several WiFi devices (an acces point, and another WiFi device, and so on), which would make up a WiFi network.

It's useful for testing WiFi programs under linux. Possibly, even under user-mode linux or other isolated virtual "boxes" with linux.

In theory, this driver could be used for tests in the android systems where you don't have a real WiFi device (or don't want to use it), and also in some kind of android emulators. Perhaps, one can manage to use this driver in android-x86, or--for testing--in android-x86 run in VirtualBox.



回答5:

If you don't have a real device, I think the best way is installing android-x86 or use it on live usb. There is no diffrences between android and android-x86.

And android-x86 support wi-fi. so you can install your app, and run it.



回答6:

Probably no substitute for testing on a real device, but if you're looking for a good enough test case in the emulator give Fake Wifi Connection a try:

It makes apps think they're on wifi no matter what the underlying connection is.

disclaimer: I wrote this stuff.
It works by intercepting ConnectivityManager / WifiManager calls (uses Xposed). I've tried to cover most of them so it does a pretty good job, nevertheless it's always possible some is missing (if you find something let me know).



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!