Search code examples
c#windows-runtimewindows-phonewin-universal-appwindows-10-universal

Internet access in a long-running background task


In my Windows Phone 10 app, I've got a long-running background task, triggered by an RfcommConnectionTrigger. This task will run for as long as my bluetooth Rfcomm device is connected.

Now what I want to achieve is this:

  • The Bluetooth device sends some data to the app on the phone (working)
  • The phone looks up a value using an HTTP request (not working)
  • The phone sends the result back to the Bluetooth device (working)

The problem with #2 is that as soon as I lock the device, all HTTP requests for some reason start failing. I'm just using the System.Net.Http.HttpClient to do a request, nothing special is going on there. More specifically, the exception is thrown in HttpClient.SendAsync.

How do I get around this limitation?


Solution

  • I found the solution. This seems to be a completely undocumented "feature" -- you need to add a SystemConditionType.InternetAvailable condition to your background task to allow it to use internet when the device is locked. I'm hoping that this doesn't mean that when the phone loses internet access, the background task is killed.

    It also means that the phone now needs to be connected to the internet in order to start the background task, which is still annoying, but I guess I can live with that.