4

I've got a Foreground Service (extends androidx.core.app.JobIntentService) that gets work enqueued when a push notification comes in. Its job is to connect to the Wi-Fi AP of an IoT device and download a video file. It is supposed to do this whether the screen is on or off.

In its onHandleWork method, it calls startForeground, acquires a PARTIAL_WAKE_LOCK and a WIFI_MODE_FULL WifiLock. All that has been working fine.

For API <29 I use the WifiManager to add my network and enable it, then use ConnectivityManager.registerNetworkCallback with a NetworkCallback that sniffs the network to see if its our device before doing the work. This has also been working mostly fine, videos download automatically without user interaction.

Now for API 29+, I am using WifiNetworkSpecifier to make a NetworkRequest for ConnectivityManager.requestNetwork() as described here: https://developer.android.com/guide/topics/connectivity/wifi-bootstrap. According to the docs it checks all my boxes. It works fine when the user initiates a connection in the app. The Bypassing user approval part seems to work as advertised after the User has pressed Connect once. But I have two problems:

  1. If requestNetwork is invoked when the screen is off, even when it succeeds and I get the network and download the file and release the network, when the screen is turned back on, the user is met with an error message "Something came up. The application has cancelled the request to choose a device." It really kills the magic. Does anyone know how to work around this?

  2. requestNetwork always causes the "Device to use with App Name" dialog to be shown, which can be disruptive and confusing when the user is not near the IoT device and there is no connection to be made. Is there a right way to know if my device's AP is in range without involving the User?

Not sure if it's worth mentioning, but I've tried these tests with ACCESS_FINE_LOCATION and ACCESS_BACKGROUND_LOCATION granted and it didn't make a difference.

Jeff
  • 41
  • 3

0 Answers0