none
Keeping your app and GPS running receiver when the device is suspended

    Question

  •  

    Hi Everyone

     

    In an application I am working on the at the moment the requirement has come about to try and keep the built in GPS receiver alive when the device is suspended.  I'm trying to tackle this in 2 parts first keeping the application running whilst the device is suspended, then the reciever. 

     

    I have read a lot of different forum posts and articles around this subject but still seem to be a bit perplexed and I have a feeling I may not be the only one, Llve messenger on the device still seems to notify me of new messages even though the device has been left alone for a long time.  I was hoping some one may be able to give the definative answer to this so that everyone may benefit. 

     

    I have already tried setting my application to unattended mode using the PowerPolicyNotify API but this does not seem to have had any effect (tested with a timer in the application) although granted not 100% sure exactly what the effect should be, but then I have also read that you must use CeRunAppAtTime.  All in all I'm lost!!

     

    The next part to this whole conundrum is, does anyone have clue of how to keep the onboard GPS receiver running all the time even when the device is suspended, I have tried SetPowerRequirement, didn't really have a great deal of success but this could be due me not being able to keep the actual application running .

     

    Please if anyone can help, it really would be more than appreciated.

    Thanks in advance

    Dan

    Wednesday, April 23, 2008 3:41 PM

Answers

  • Hi Daniel,

    That is by design. For suspend mode, CPU doesn't work, without CPU work, we can't use GRS.

     

    Best regards,

    Guang-Ming Bian - MSFT

    Friday, April 25, 2008 7:36 AM
    Moderator
  •  

    Hi Daniel,

     

    Ive been having the same problem as you (i was actually the person who started the other, similar, thread!). Ive managed to keep the GPRS connection alive both when the device goes to sleep automatically (on a power timer) and when the user presses the power button.

    I run the following code on a timer every 25 seconds

     

    private void timer2_Tick(object sender, EventArgs e)

    {

    PowerPolicyNotify(PowerMode.UnattendedMode, 1);

    SystemIdleTimerReset();

    }

     

    and this seems to do the trick. No joy yet on getting the GPS signal to stay when the user presses the power button though...

    Monday, April 28, 2008 1:21 PM

All replies

  • There was a similar posting and I believe the answer was that when the device goes into suspend mode the power to the GPS is disconnected.

     

     

     

    Wednesday, April 23, 2008 3:50 PM
  •  

    I have read similar but was still holding out hope that it could be done.
    Thursday, April 24, 2008 8:01 AM
  • Hi Daniel,

    That is by design. For suspend mode, CPU doesn't work, without CPU work, we can't use GRS.

     

    Best regards,

    Guang-Ming Bian - MSFT

    Friday, April 25, 2008 7:36 AM
    Moderator
  • Hi

     

    So how do apps like activesync (Direct Push) and Windows Live Messenger keep running and seemingly keep the GPRS connection alive.  Do PowerPolicyNotify (UnattendedMode) and CeRunAppAtTime and SetPowerRequirement do anything around this area?

     

    Thanks

     

    Friday, April 25, 2008 8:23 AM
  •  

    Hi Daniel,

     

    Ive been having the same problem as you (i was actually the person who started the other, similar, thread!). Ive managed to keep the GPRS connection alive both when the device goes to sleep automatically (on a power timer) and when the user presses the power button.

    I run the following code on a timer every 25 seconds

     

    private void timer2_Tick(object sender, EventArgs e)

    {

    PowerPolicyNotify(PowerMode.UnattendedMode, 1);

    SystemIdleTimerReset();

    }

     

    and this seems to do the trick. No joy yet on getting the GPS signal to stay when the user presses the power button though...

    Monday, April 28, 2008 1:21 PM
  • Can I ask what devices you guys want to do this on?

    Is a cradle or dock something you could use to solve the power requirements?

    I do a ton of work on Industrial devices (Symbol / Motorola, Intermec) and in most cases the solution you have come up with Karen is close to what we do.

    The way we deal with having to do that is to use extended capacity batteries, most devices have them available but ship with the regular capacity battery.

    The other thing I do is to use external GPS devices with their own battery / power and bluetooth.

     

     

    Monday, April 28, 2008 6:43 PM
  • Hi Karen

     

    Thanks for your reply, so you must call PowerPolicyNotify each time in the timer event, its not just something you can call once?

     

    Stephen, one of the main devices were working with is the TyTnII, we are ok whilst the device is docked but we do need to try and make things function the same even when the user is just walking around with the device uncradled.

     

    Thanks

    Dan

    Monday, April 28, 2008 8:49 PM
  • Hi Daniel,

    While cradling/docking device, you see activesync is still connected when device is in suspend, that is a mistake. That's not real suspend status, just turn off backlight to save battery.

     

    Best regards,

    Guang-Ming Bian - MSFT

    Tuesday, April 29, 2008 3:03 AM
    Moderator
  • Hi Dan,

     

    Yes you have to keep calling it otherwise the device suspends anyway after a period of time and then the app stops running.

     

    We have found the same issue on multiple devices running both windows mobile 5 and 6 (for example: orbit 2, orange m700 and TytnII). The common factor seems to be the 3G connection, although switching the band in the settings to only use GPRS makes no difference. The devices we have out without 3G (for example: orbit 1) have never shown this issue and do not need to be kept in unattended mode, the GPS continues to run in suspend mode. We have wondered here if the 3G devices have a different suspend mode that closes more functions to save power than those without it but this is really only a theory based on our results.

     

    Karen

     

    Tuesday, April 29, 2008 8:03 AM
  •  

    Hi Karen

     

    Thanks for that, it does appear now to keep the app running when I press the power button.  I already had a section of the app that uses the ResetSystemIdleTimer, does calling this not stop the device from suspending automatically?

     

    Thanks

    Dan

     

    Tuesday, April 29, 2008 12:23 PM
  • Hi Dan,

     

    Yes it does, its another thing that needs to be called regularly though which is why i use it ont he same timer. its more for neatness of code than anything else really so as long as you are already running it you should be fine

     

    Karen

     

    Tuesday, April 29, 2008 2:31 PM
  • Hi Karen

     

    Thanks for all your help, with it I managed to make the app run even when a user presses the power button and also keep the GPS receiver going as well (Well on the HTC P3600 anyway, they dam TyTnII doesn't want to play ball yet).

     

    I am currently tinkering with power notifications (after coming across a post on xda-developers) to make it so the device can also natrually suspend but still keep things going, first tests seem positive.  I will post back when I have definite answer.

     

    Thanks again

    Dan

     

    Thursday, May 01, 2008 8:30 AM
  • Hi Daniel and Karen,

     

    Unfortunately, I'm not able to keep the GPS signal alive on my HTC P3600 (Orange SPV M700). I've tried the code Karen mentiones above, but I can't get it to work. The timer remains active and a simpel counters counts through. However, the GPS signal disappears after pressing the power button.

     

    I call PowerPolicyNotify and SystemIdleTimerReset on each timer tick. Then, I read all data from the serial port (COM9). This works fine. After I press the power button, there is no more data to read from the serial port.

     

    Are there any more calls I need to make to get this to work? Daniel says he has it working on a P3600, so it should be possible. Please help me out.

     

    Best regards,

     

    Johan

     

    /edit

    Daniel, are you dannyboy78 on XDA Developers? I came across this thread: http://forum.xda-developers.com/showthread.php?t=379366 and it looks very interesting.

     

    If so, how did you manage to call those functions from .NET? I've got a basic understanding of platform invokes, but I have no idea how to convert a message from a message queue to a structure.

    Monday, June 23, 2008 11:45 PM
  • Hi Johan

     

    Apologies for the delayed response to your post I have been busy on a new release of our software.  For the last 20 minutes I have been trying to post the code that use but everytime I post it I get an unknown error, I have placed the code in a code block and said that the post contains a code sample, I might just be doing something wrong, but I have given up the will to live

     

    If you would like to mail me I will be happy to send you the code that I have.

    I have successfully tested it on a HTC P3600 and HTC p3300, I recently got a HTC Diamond and found that my original code didn't work, it seemed to be because rather than sending a suspend message the diamond does send an unattened message, once I added a check for this it successfully worked on the Diamond as well.  The only device thus far I have not been able to get it working on is the HTC TyTNII the application stays alive and GPRS but I can not get the GPD receiver to stay on, I have read that the Qualcomm chipset in this device ignores SetPowerRequirement messages but if there is anyone that can shed anymore light on this then that would be great.

     

    Thanks
    Dan

     

    Monday, July 07, 2008 6:43 PM
  • Hi Daniel,

     

    Thanks for taking time to help me out. I sent an e-mail to the e-mail address stated in your profile on this forum. I hope you get it. I'm looking forward to hearing from you.

     

    Best regards,

     

    Johan

    Monday, July 07, 2008 7:04 PM
  •  

    Hi Dan,

     

    Actually I have the same problem with the HTC Polaris (Touch Cruise). I would like to keep my GPS application running, after the user presses the PowerButton... just the display should turn off. On older WM devices I got that behaviour just by putting the device in the unatteded mode with PowerPolicyNotify. Unfortunately that doesnt work with my HTC Polaris any more.

    Could you please help me, and send me your code.... because I think if your code works on the Diamond, it will work on the Polaris aswell.

    My email is patrick.luley@gmail.com

     

    Thanks a lot, patrick

    Monday, July 14, 2008 3:58 PM
  • Hi Patrick

     

    I have sent you an email with the code, hope it helps you.

     

    Dan

     

    Monday, July 14, 2008 5:07 PM
  • Hey daniel

    I am also in same boat with gps. could you send me the code to test out at

    gobboman@hotmail.com

    thanks
    Daniel
    Wednesday, July 16, 2008 12:58 AM
  • Hi Daniel,

    I am stuck in the same scenario. I want my application should be running even the device moves to sleep state.
    For achieving this I came across 
    "PowerPolicyNotify(PPN_UNATTENDEDMODE, true)".
    Now my application is running even when I press the power button and put the device in the sleep state, but the GPS module is not active.
    It seems like GPS connection powers down in unattended mode.

    I believe this is the same scenierio everybody else is facing here.
    If that is the case can you please send me the sample code at p.s.niranjan@gmail.com

    Thanks,
    Prashant Singh
    Wednesday, July 16, 2008 1:06 PM
  • Hi Guys

     

    Quite a few people have been asking for the code, looks like there are few of us in the same position.  Just wanted to pass on kudos to Alex Feinman for the sample app he wrote on power notifications which forms the base of this code with the message queue and to Karen for her help on getting the unattenedmode working properly.

     

    lots of  for them

    Dan

    Thursday, July 17, 2008 4:05 PM
  • Hi Dan,

     

    Thanks again for sending me your source code.

     

    Unfortunately, I have some troubles to get it working properly. The log file says only "starting"... and no other entries.

    Would you please be so kind to assemble a class, containing all the necessary code, which i can make an instance of and say start() or stop()  Smile

     

    This could help me to find out where my problem is... then I know it must work, because it keeps untouched by me Wink

     

    thanks a lot.

     

    patrick

    Monday, July 21, 2008 10:49 AM
  • Hi Everyone, I found a solution to the problems with the Mogul and this might help with other HTC phone but I am not sure. 


    Dan,Thanks again for sending me the code!  I think I have found the solution for the TyTnII/Mogul crashing problem.  What would happen is when you hit the power button it would lock up the GPS receiver and only a phone reset would fix it.  Then I introduced a bug that would crash my app when this happened, same thing then, had to do a reset on the phone.  Then I reread another posting where they changed some of the registry settings in order to get around a problem with some other app.  Once I made the changes and hit the power button the app still crashed but it didn’t lock the GPS!  So once I found my exception, threw a catch on it and then reinitialized the GPS handle everything started working fine!  So now I can hit the power button and leave it off for an indefinite amount of time and the GPS keeps running in the background.  I am using a timer to reset the idle time and setting the application to unattended mode and that is all that was needed.  The cool thing is that I don’t have to even bother with capturing the power on and off events.  Below are the registry settings I changed to make it work and I am not sure how device specific they are but my app is only running on the Mogul for now so it works fine.  Basically the driver problem is with the Assisted GPS which is really not needed on this phone anyways. I was still able to view 8-10 satellites with these settings, It may not work as well indoors on the initial fix but once it did get a fix I was able to view 6-8 satellites in my house.  These changes should also help with other apps like Google Maps and Live Search. 


    EnableAGPS = 0 (Default is 1)

    EnableGPSSmartMode = 0 (Default is 1)

    GPSMode = 1 (default is 2)


    Here is the class to call before you get the GPS handle to change the registry settings.

    public static void setGPSRegistrySettings() {

          try {

            Microsoft.Win32.RegistryKey key =

              Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\HTC\\SUPL AGPS", true);

     

            if(key != null) {

              if(key.GetValue("EnableAGPS") != null) { key.SetValue("EnableAGPS", 0); }

              if(key.GetValue("EnableGPSSmartMode") != null) { key.SetValue("EnableGPSSmartMode", 0); }

              if(key.GetValue("GPSMode") != null) { key.SetValue("GPSMode", 1); }

            }

          } catch(Exception e) { }

        }

     

    Hope this helps somebody out there.

    Tuesday, July 22, 2008 4:05 PM
  • Hi Daniel,

     

    I have exactly the same problem about keeping GPS running, please could you send me your code/solution? I will be very glad.

    Friday, August 29, 2008 8:11 PM
  •  

    Here are the P/Invoke declarations for the code mentioned on that site.  It's missing RequestPowerNotifications, but this should be enough to get you started. 

     

    Code Snippet

    [DllImport("CoreDLL", SetLastError=true)]

    public static extern IntPtr SetPowerRequirement

    (

    string pDevice,

    CEDEVICE_POWER_STATE DeviceState,

    DevicePowerFlags DeviceFlags,

    IntPtr pSystemState,

    uint StateFlagsZero

    );

     

    [DllImport("CoreDLL")]

    public static extern int PowerPolicyNotify(

    PPNMessage dwMessage,

    int option

    );

     

    [DllImport("CoreDLL")]

    public static extern int ReleasePowerRequirement(IntPtr hPowerReq);

     

    public enum CEDEVICE_POWER_STATE:int

    {

    PwrDeviceUnspecified = -1,

    //Full On: full power, full functionality

    D0 = 0,

    /// <summary>

    /// Low Power On: fully functional at low power/performance

    /// </summary>

    D1 = 1,

    /// <summary>

    /// Standby: partially powered with automatic wake

    /// </summary>

    D2 = 2,

    /// <summary>

    /// Sleep: partially powered with device initiated wake

    /// </summary>

    D3 = 3,

    /// <summary>

    /// Off: unpowered

    /// </summary>

    D4 = 4,

    PwrDeviceMaximum

    }

     

     

    I just copied this from something I posted here:

     http://www.codeproject.com/script/Articles/Article.aspx?aid=28886

     

    Saturday, August 30, 2008 2:26 AM
  • Hi Daniel,

    My application runs on Polaris and Sedna and, like everybody here, it needs GPS running even in sleep state. I would be glad if you could send me your code.

    Thanks

    Sébastien
    Wednesday, September 03, 2008 8:52 AM
  •  

    I don't know what "Polaris" or "Sedna" is, but this may help you.

     

     

     

    Code Snippet

    const string GPS_DEVICE_NAME_PATH = "DRIVERS\\Builtin\\GPSID";

    public string GpsDeviceName

    {

       get

       {

         if (_gpsDeviceName == null)

         {

           RegistryKey gpsInfoKey = Registry.LocalMachine.OpenSubKey(GPS_DEVICE_NAME_PATH);

           if (gpsInfoKey != null)

           {

             try

             {

               _gpsDeviceName = String.Format("{0}{1}:", gpsInfoKey.GetValue("Prefix"),    gpsInfoKey.GetValue("Index"));

             }

             catch { }

           }

         }

         return _gpsDeviceName;

       }

    }

     

    private void Form1_Load(object sender, EventArgs e)

    {

       CoreDLL.PowerPolicyNotify(PPNMessage.PPN_UNATTENDEDMODE, -1);

       CoreDLL.GetDevicePower(GpsDeviceName, DevicePowerFlags.POWER_NAME, out currentPowerState);

       _gpsPowerRequirements = CoreDLL.SetPowerRequirement(GpsDeviceName, CEDEVICE_POWER_STATE.D0,  DevicePowerFlags.POWER_NAME, IntPtr.Zero, 0);

     

    }

    private void Form1_Closing(object sender, CancelEventArgs e)

    {

       _gpsDevice.Close();

       CoreDLL.PowerPolicyNotify(PPNMessage.PPN_UNATTENDEDMODE, 0);

       CoreDLL.ReleasePowerRequirement(_gpsPowerRequirements);

    }

     

     

     

    Thursday, September 04, 2008 3:23 PM
  • Hi Joel,

    Thanks for your answer.

    That's more or or less what I did while waiting an answer... and I used this with a power notification to power GPS receiver when device goes in sleep mode. It works fine.

    Sedna = HTC P6500
    Polaris = HTC Touch Cruise

    Sebastien
    Friday, September 12, 2008 6:58 AM
  • Hi Daniel

    I'm sorry, I know this thread is old. But I have the same problem (HTC P3600 not keeping GPS on in Unattended mode). I've tried everything, SetPowerRequirement() with gpd1:, gpd0:, com9, all of them... The program keeps running in Unattended mode, even Wifi works, but the GPS Intermediate driver is stuck and does not call the update handler.

    Please can you share with me the code you mention?

    Many thanks

    Aleix Mercader
    • Proposed as answer by Kingslayer Wednesday, March 10, 2010 3:55 PM
    Monday, February 08, 2010 11:44 AM
  • Hi Karen!

    The code worked perfectly!!! When i press the top button of my htc touch 3g my applicattion is still alive and sending information to my server!!!!

    I do love you!! The sleepless nights are gone!!!
    Las noches sin dormir se fueron!!!!

    Thanks again!!!
    Gracias de nuevo!!!
    Wednesday, March 10, 2010 3:59 PM