简体   繁体   English

Android 通过 WiFi 以编程方式连接到设备

[英]Android programmatically connect to device via WiFi

My app needs to connect to an external device (camera) via WiFi (the device is a local network access point).我的应用需要通过 WiFi 连接到外部设备(相机)(该设备是本地网络接入点)。 When I connect to the device's WiFi via Android settings, everything works fine, but I've got a requirement that the user should be able to connect with the device right from the app.当我通过 Android 设置连接到设备的 WiFi 时,一切正常,但我要求用户应该能够直接从应用程序连接设备。

I know that there is an option to connect to a specific network via WifiManager.addNetwork but 1) it not always connects me to the network I added 2) this method is deprecated and will stop working when my app targets API 29 (which will happen soon).我知道有一个选项可以通过WifiManager.addNetwork连接到特定网络,但 1)它并不总是将我连接到我添加的网络 2)此方法已弃用,并且当我的应用程序以 API 29 为目标时将停止工作(这将发生)很快)。

I know that the proposed solution for deprecated addNetwork is suggestion API , but it is available only on Android 10 and above (and my app supports API 21).我知道建议的已弃用addNetwork解决方案是建议 API ,但它仅适用于 Android 10 及更高版本(我的应用程序支持 API 21)。

Are there any other stable options for connecting to a WiFi network (with no internet access) from the app that are available from API 21 and will be working when I'm targeting API 29? API 21 提供的应用程序是否有任何其他稳定的选项可用于连接到 WiFi 网络(无法访问互联网),并且在我的目标是 API 29 时可以使用?


edit : Ok, now I see that for my use case the Network Request API for peer-to-peer connection is more suitable, but still - it works only for Android 10 +.编辑:好的,现在我看到对于我的用例, 用于点对点连接的网络请求 API更合适,但仍然 - 它仅适用于 Android 10 +。

After a few months working on this case I now know that:在处理这个案例几个月后,我现在知道:

  1. Android documentation is not really correct about the old connection API not working when you target Android 10. It still works on devices with Android < 10. So if you target API 29 and above, you have to write two separate logics for Andoid <10 and >=10... Android documentation is not really correct about the old connection API not working when you target Android 10. It still works on devices with Android < 10. So if you target API 29 and above, you have to write two separate logics for Andoid <10 and >=10...
  2. In my opinion, the new connection API sucks.在我看来,新的连接 API 很烂。 I use this new peer-to-peer API, and it takes much longer to connect, the system popup is always in English, and there are some connection (eg. in terms of using streams, reconnecting, or video streaming) issues when you connect from the app vs when you connect from settings.我使用这个新的点对点API,连接需要更长的时间,系统弹出总是英文的,并且当您使用流时,存在一些连接(例如在使用流、重新连接或视频流方面)问题从应用程序连接与从设置连接时连接。

It seems that Google gave as a new solution that is not really stable (some issues are fixed only in Android 11) and force us to use it.似乎 Google 提供了一个不太稳定的新解决方案(某些问题仅在 Android 11 中修复)并强迫我们使用它。 Not really happy about it, but what can you do...不是很高兴,但你能做什么......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM