Languages

Menu
Sites
Language
Vibrate when application running on background

Hi, I am developing a wearable app using Tizen 4.0. There is a timer that always runs on the background and I need to vibrate and play a sound when timer finish and the application is not displayed. I am calling Power.RequestCpuLock(0) before vibrating and playing sound and immediately release the cpu with Power.ReleaseCpuLock() after it. my question: is this the way to do it? maybe there is a better practice to achieve this behavior?
This is how my method looks like:
private void DingUser()
{
Device.BeginInvokeOnMainThread(() => Power.RequestCpuLock(0));
// vibrate
DependencyService.Get().Vibrate();
// ding
this.PlaySound();
Device.BeginInvokeOnMainThread(() => Power.ReleaseCpuLock());
}

Thank you.

Edited by: JONATHAN APPLEBAUM on 17 Oct, 2019
View Selected Answer

Responses

7 Replies
Tizen .NET

Hi,

Could you explain more about it? What's the purpose of Timer?

Regarding "when timer finish and the application is not displayed",

you may use app lifecyle(OnSleep : called each time the app goes to the background) or do something by using service app which is running in background.

We can help you if you could please share more information.

Thanks.

Tizen.NET (tizen.net@samsung.com)

 

JONATHAN APPLEBAUM

Hi,
Thank you for the quick replay,
I am building a wearable application That is intended for meditation.
The user can set a "warm up" duration time (that can be between 1-10 min) and session duration time (that can be between 1-60 min).

Let's say the user wants to start a session and this is his settings:
warmup duration: 2 min.
session duration: 10 min.

The user presses the start button and an instance of System.Diagnostics.Stopwatch starts, and TimeChange event is raised every 10 milliseconds.

In the scenario above we want to notify the user (vibrate and play some .wav file) twice - when the warmup ended and when the session was ended. i, e.  after 2 min and after 12 min. 

I have noticed that after the app is entering to sleep mode, vibration and sound not always executed. I went through some samples from Tizen-CSharp-Samples-master on GitHub and I noticed the use of Power.RequestCpuLock() in the Xstopwatch example app.
I know that locking the CPU can be battery consuming and I dont want my App to be resources absorbing.

Another issue That's also related to this issue, I want my app to be in at the foreground every time the watch display is back to DisplayState.Normal (like the running app on Samsung health)
I have tried registering an event to Tizen.System.Display.StateChanged and calling the Tizen.Applications.AppControl.SendLaunchRequest every time the display is back to normal but it's not working, is there any examples available?  is this the way to achieve such functionality?
if true I will be very thankful for general guidance.

I have tried something like that:

private void stateChanged(object sender, DisplayStateChangedEventArgs e)
{
    if (e.State == DisplayState.Normal)
    {
        Tizen.Applications.AppControl appcontrol = new Tizen.Applications.AppControl();
        appcontrol.Operation = Tizen.Applications.AppControlOperations.LaunchOnEvent;
        appcontrol.ApplicationId = Tizen.Applications.Application.Current.ApplicationInfo.ApplicationId;
        Tizen.Applications.AppControl.SendLaunchRequest(appcontrol);
    }
}

Thanks in advance.

Mark as answer
Tizen .NET

Hi,

It seems inappropriate to use AppControlOperations.LaunchOnEvent in this case.

It can be used in a service app to received system events such as usb status, wifi state, and so on.

You can get more details about it from here. (Sorry for not having a direct explanation in .NET section)

Anyway, there are two options to do this

- UI app

- Service + UI apps

Either way, please make that the app can run in background.

You can get details from here:
https://developer.tizen.org/dev-guide/2.4/org.tizen.guides/html/native/app/application_n.htm#allow_bg
https://samsung.github.io/Tizen.NET/wearables/web-proxy/#run-in-the-background

In tizen-manifest.xml, please define <background-category> and the required privileges as follows:

    <ui-application appid="juwols.Meditation" exec="Meditation.dll" multiple="false" nodisplay="false" taskmanage="true" type="dotnet" launch_mode="single">
        <label>Meditation</label>
        <icon>Meditation.png</icon>
        <metadata key="http://tizen.org/metadata/prefer_dotnet_aot" value="true" />
        <background-category value="media" />
        <splash-screens />
    </ui-application>
    <shortcut-list />
    <privileges>
        <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
        <privilege>http://tizen.org/privilege/haptic</privilege>
        <privilege>http://tizen.org/privilege/display</privilege>

    </privileges>

 

After making app supported in background, I think you can make it vibrate and play soud fileI with your code after 2 minute warmup.

I share my test just in case.

Display.StateChanged += Display_StateChanged; // remove this event handler when it's not necessary
warmupTimer = new Timer(120000)
{
    AutoReset = false,
    Enabled = true,
};
warmupTimer.Elapsed += WarmupTimer_Elapsed;


private void WarmupTimer_Elapsed(object sender, ElapsedEventArgs e)
{
    myFeedback.Play(FeedbackType.All, "WakeUp");
}

private void Display_StateChanged(object sender, DisplayStateChangedEventArgs e)
{
    if (e.State == DisplayState.Normal)
    {
        AppControl appcontrol = new AppControl()
        {
            ApplicationId = ApplicationId = Tizen.Applications.Application.Current.ApplicationInfo.ApplicationId
        };
        AppControl.SendLaunchRequest(appcontrol);
    }
}

We hope this information helps in solving your issue.

Thanks.

 

 

JONATHAN APPLEBAUM

This information has been very helpful and valuable to me.

I had the feeling that I was shooting in the dark with AppControlOperations.LaunchOnEvent

Thanks so much for the detailed explanation.

 
Tizen .NET

Hi JONATHAN APPLEBAUM

I'm sorry, I didn't proceed to test it in sleep mode. (I think Timer.Elapsed event won't be fired in sleep mode.)

To keep the CPU running to do something while the screen is off, we need to hold CPU lock only for the time needed.

Or we can use the Alarm API to wake up after a certain time.

Thanks.

JONATHAN APPLEBAUM

Hi,
that is how I handle it now and it works fine.
But in the same matter do you have any advice or enlightenment on this case: 

The application is also locking the CPU when activating the heart rate monitor for a little longer than the vibration. 
the problem is that the OS is alerting this message: "My application is using excessive battery power. Close app?"
the CPU is locked (and released after 20 seconds) in equal segments of 25% of session duration time.
i.e. if session duration is 10 minutes LocCpu() will be called 5 times in that manner:
1. beginning of the session.
2. 02:10 (20 sec before completing 25%)
3. 04:40 (20 sec before completing 50%)
4. 07:10 (20 sec before completing 75%)
5. 9:40 (20 sec before completing 100%)
Total time that CPU is locked is 60 seconds,
I am testing on Samsung gear s3 frontier.
Is there a way to handle it better in order to prevent the OS message?

Thank you again for your support and advice.

This function supposes to handle it safely, its called every time the heart rate monitor needs to be activated:

private static bool _IsCpuLocked = false;
public static bool IsCpuLocked
{
    get
    {
        return _IsCpuLocked;
    }
    private set
    {
        _IsCpuLocked = value;
    }
}

public static void LocCpu()
{
    if (!IsCpuLocked)
    {
        Xamarin.Forms.Device.BeginInvokeOnMainThread(() => Tizen.System.Power.RequestCpuLock(0));
    }
}

public static void ReleaseCpu()
{
    if (IsCpuLocked)
    {
        Xamarin.Forms.Device.BeginInvokeOnMainThread(() => Tizen.System.Power.ReleaseCpuLock());
    }
}

 

JONATHAN APPLEBAUM

OK...now that I am passing on to my replay I think I found the problem...IsCpuLocked property is never set to true...cant seem to find a delete button for this replay...
allways keep telling myself never write code on the middle of the night... (-: