Android:在模拟器中模拟 WiFi?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1563370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:16:50  来源:igfitidea点击:

Android: Simulate WiFi in the emulator?

androidtestingandroid-emulatorwifidevice-emulation

提问by Felix

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 -> Wifisays Unable to start Wi-Fi, while logcatsays:

我想从我的应用程序中检查设备是否具有 WiFi 连接,但为了做到这一点,我必须首先找到一种在模拟器中获取“WiFi”的方法。只需转到Settings -> Wireless controls -> WifiUnable to start Wi-Fi,而logcat说:

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?

如何在模拟器中模拟 WiFi 连接?

采纳答案by Felix

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 Genymotiondoes WiFi by default, so you can use that to develop/test apps with specific WiFi functionality.

由于这似乎是一个受欢迎的问题,我将继续添加更新的答案。SDK 仍然不支持 WiFi 仿真,但Genymotion默认支持 WiFi ,因此您可以使用它来开发/测试具有特定 WiFi 功能的应用程序。

回答by Dimitar Dimitrov

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 WifiManagergets confused when you switch/want to test your connectivity state.

目前,虽然您可以在模拟器中使用计算机的 Internet 连接,但将其用于模拟 Wi-Fi 并不可靠。在WifiManager当你切换迷糊/想测试你的连接状态。

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.

即使在 Android 1.5 中,它也存在一些问题(问题 2571 - android),虽然您可以使用 Internet 来模拟 HTTP 连接和下载数据,但最好使用设备来更好地控制 Wi-Fi 连接。

回答by Saket

Android emulator wifi

安卓模拟器wifi

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

从修订版 26.1.3 开始,Android 模拟器现在支持连接到牛轧糖映像 (API 25) 上的 WiFi。

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.

在某些系统映像中添加了对 Wi-Fi 的支持(目前只有 API 级别 25)。一个名为“AndroidWifi”的接入点可用,Android 会自动连接到它。

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

来源: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.

您可以通过进入模拟器设置(三个点)> 帮助 > 关于来检查模拟器的版本。另请注意,如果 AVD 是在 26.1.3 版发布之前创建的,则必须重新创建 AVD。我在某处读到擦除数据也可能有效,但我没有尝试过。

回答by Okan Kocyigit

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

如果您没有真机,我认为最好的方法是安装android-x86或在 live usb 上使用它。android 和 android-x86 之间没有区别。

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

并且 android-x86 支持 wi-fi。所以你可以安装你的应用程序并运行它。

回答by imz -- Ivan Zakharyaschev

In theory, linux (the kernel underlying android) has mac80211_hwsimdriver, 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.

理论上,linux(android底层的内核)有mac80211_hwsim驱动,可以模拟WiFi。它可用于设置多个 WiFi 设备(一个接入点和另一个 WiFi 设备,等等),这将构成一个 WiFi 网络。

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

它对于在 linux 下测试 WiFi 程序很有用。可能,即使在用户模式 ​​linux或其他带有 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.

理论上,此驱动程序可用于在您没有真正的 WiFi 设备(或不想使用它)的 android 系统中进行测试,也可用于某些类型的 android 模拟器。也许,人们可以设法在 android-x86 中使用这个驱动程序,或者——为了测试——在 VirtualBox 中运行的 android-x86 中。

回答by lemonsqueeze

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 Connectiona try:

可能无法替代在真实设备上的测试,但如果您正在模拟器中寻找足够好的测试用例,请尝试使用Fake Wifi Connection

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

无论底层连接是什么,它都会让应用程序认为它们在 wifi 上。

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).

免责声明:我写了这个东西。
它通过拦截 ConnectivityManager / WifiManager 调用(使用Xposed)来工作。我试图涵盖其中的大部分,所以它做得很好,但是总是有可能缺少一些(如果你发现了什么让我知道)。