Search code examples
androidandroid-wifiwifimanager

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

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

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

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?


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


Solution

  • 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...
    2. In my opinion, the new connection API sucks. 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.

    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. Not really happy about it, but what can you do...