Returning Tizen wearable application to active mode on screen activation

In previous article (Keeping Tizen wearable device application running in background mode) I wrote about how to keep Tizen wearable application running in background mode, e.g. for long-running sensor data monitoring. It this note let’s find out how to bring wearable application back in active state after device screen has been turned off.

All magic takes place in App.xaml.cs. For getting your application back in active state you need power provided by Tizen.System and Tizen.Application apis.

using Tizen.Applications;
using Tizen.System;
using Tizen.Applications.Exceptions;
using Application = Tizen.Applications.Application;

in constructor method add custom Display.StateChanged event handler.

public App()
{
       InitializeComponent();
       Display.StateChanged += Display_StateChanged;
}

Method Display_StateChanged checks last display state and if it is off then runs “launch-me” request.

private DisplayState _lastDisplayState;

private void Display_StateChanged(object sender, DisplayStateChangedEventArgs e)
{
      if (e.State == DisplayState.Normal && _lastDisplayState == DisplayState.Off)
          SendLaunchRequest();

      _lastDisplayState = e.State;
}

private void SendLaunchRequest()
{
     try
     {
          AppControl.SendLaunchRequest(
              new AppControl
              {
                 ApplicationId = Application.Current.ApplicationInfo.ApplicationId,
                 LaunchMode = AppControlLaunchMode.Single,
              }, (launchRequest, replyRequest, result) => { });
    }
    catch (LaunchRejectedException ex) {

        // Most likely you don't have proper permissions.
    }
    catch (Exception ex) {
        //Some general error
    }
}

Don’t forget check app’s permissions before trying to go back to active state!

protected override void OnStart()
{
    RequestPermissionAsync(PrivacyPrivilege.Power);
    RequestPermissionAsync(PrivacyPrivilege.Display);
    RequestPermissionAsync(PrivacyPrivilege.AppManagerLaunch);
}

The solution inspired by Samsung developers forum thread https://forum.developer.samsung.com/t/how-can-i-prevent-my-net-wearable-app-from-going-to-sleep/3648/11