Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

wifimanager - How to restore Wi-Fi connection after using requestNetwork on Android

Background: I have an Android app which configures an IoT device's Wi-Fi connection:

  1. The IoT device exposes an Access Point with a specific name
  2. The app connects to the AP
  3. The app sends the SSID & passphrase of the user's Wi-Fi network to the device
  4. Both the device and the smartphone connect to the home Wi-Fi network.

I've had a working solution based on WifiManager.addNetwork & WiFiManager.enableNetwork, but these APIs are unavailable with targetSdkLevel set to 29 or above (which is a hard requirement now on Google Play).

The new API offered by Android is ConnectivityManager.requestNetwork. This works quite well during steps 1-3, but after disconnecting from the temporary network (by removing the network request with ConnectivityManager.unregisterNetworkCallback), the smartphone does not reconnect to the original Wi-Fi network - it stays disconnected from Wi-Fi until the user manually selects a Wi-Fi network in Settings. Even if there is no other data connection available.

Is there a way to force the phone to connect to its preferred Wi-Fi network after using requestNetwork?

question from:https://stackoverflow.com/questions/65938574/how-to-restore-wi-fi-connection-after-using-requestnetwork-on-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As you mentioned, from targedSdkLevel 29 you cannot directly enable specific wifi network. But as documentation says you can add network suggestion, which "is used to provide a Wi-Fi network for consideration when auto-connecting to networks."

You can find it here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...