SirBirne
SirBirne

Reputation: 297

Using Wifi when App is not active

I have the following Problem:

I use a System.Threading.Timer in my application so that I can send a Heartbeat even if the Screen is locked (I need to do this in a short interval, let's assume every minute). But it seems that I have no WiFi connection when the screen is locked.

_timer = new Timer(TimerCallback, null, 0, 30000);

private async void TimerCallback(object state)
{
    try
    {
        await MobileApiService.Heartbeat();
    }
    catch(Exception e) { }
}

This code keeps executing (until the app is suspended but that is OK) but it throws the following Exception when the phone is locked:

The text associated with this error code could not be found.

A connection with the server could not be established

Is there anything I can do? Something like SomeWifiManager::RequestConnection()?

Upvotes: 0

Views: 82

Answers (1)

VaclavD
VaclavD

Reputation: 2622

You can prevet screen locking:

var dr = new Windows.System.Display.DisplayRequest(); 

try {
    dr.RequestActive(); 
} catch {
}

try {
    dr.RequestRelease(); 
} catch {
}

When the screen is on, Wi-Fi also keeps on. It also prevents app suspension due to user inactivity.

More details on MSDN.

Upvotes: 1

Related Questions