Welcome to Sign in | Join | Help

News

  • View Simon Guest's profile on LinkedIn Simon Guest's Facebook profile
Browse by Archive:




Battery/Email Hack with Windows Mobile 5.0 SDK
 

As of last Friday I became the proud owner of a Samsung Blackjack.  The device is very small and runs Windows Mobile 5.0, which makes reading, sending Email, and just generally being "in sync with the world" a breeze.

A device so small has it's limitations of course - battery life is one of them.  Although I've not experienced it as bad as some reviewers, when the phone is operating on a 3G network, and bluetooth is enabled, the battery can drain pretty quickly.

Having been the previous owner of a T-Mobile MDA I've been used to this - the unfortunate thing is that I always forget to put the phone on charge.  Even if the device alerts me that the battery is running low through a sound or other alert, I'm normally in the middle of a meeting or somewhere where I don't have access to a charger.  By the time I've returned to the place that does have a charger (back to my office, home, or car) I've of course forgotten that it needs charging.  The next time I come to use the phone (e.g. when I really need it) it's drained.

With a new phone, I decided that it was time to do something about it.  After thinking about the problem for a little, I figured I needed a tool on the phone that would Email me when the battery was low.  Like many at Microsoft my main channel of contact is through Email - so even if my phone is next to my on the desk it's useful to have it Email me when something is up.  If it Emails me and I'm in the middle of a meeting or on the road, the message will be waiting for me when I return.

I thought about what this would take - and admit that I had nasty visions of mobile p/Invoke calls and multiple C++ libraries. Fortunately, I stumbled across the Windows Mobile 5.0 SDK and it was super easy.  Here's the code:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;

using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.Status;

namespace RemindMe
{
  static class Program
  {
    static bool WaitingForRecharge = false;

    static int EMAIL_ACCOUNT_INDEX = 0;
    static string EMAIL_ADDRESS = "nospam@microsoft.com"
    static string EMAIL_SUBJECT = "Battery level on phone is running low"
    static string EMAIL_BODY = "You may want to think about recharging your phone - it's running low."

    static BatteryState BATTERY_LOW_LEVEL = BatteryState.Low;
    static BatteryState BATTERY_RECHARGING_LEVEL = BatteryState.Charging;
    static int BATTERY_CHECK_MILLISECONDS = 30000;

    [MTAThread]
    static void Main()
    {
      Debug.WriteLine("Application started.");

      while (true)
      {
        Thread.Sleep(BATTERY_CHECK_MILLISECONDS);
        CheckBattery();
      }
    }

    static void CheckBattery()
    {
      if (!WaitingForRecharge)
      {
        Debug.WriteLine("Checking battery level");

        if (IsBatteryLow())
        {
          Debug.WriteLine("Battery is low - Sending Email");
          SendEmail();
          WaitingForRecharge = true;
        }
      }
      else
      {
        Debug.WriteLine("Not checking battery - waiting for recharge.");
      }

      if (WaitingForRecharge & SystemState.PowerBatteryState == BATTERY_RECHARGING_LEVEL)
      {
        Debug.WriteLine("Battery has been recharged.");
        WaitingForRecharge = false;
      }
    }

    static bool IsBatteryLow()
    {
       return (SystemState.PowerBatteryState == BATTERY_LOW_LEVEL);
    }

    static void SendEmail()
    {
      try
      {
        EmailMessage message = new EmailMessage();
        message.To.Add(new Recipient(EMAIL_ADDRESS));
        message.Subject = EMAIL_SUBJECT;
        message.BodyText = EMAIL_BODY;
        message.Importance = Importance.High;

        OutlookSession session = new OutlookSession();
        session.EmailAccounts[EMAIL_ACCOUNT_INDEX].Send(message);
        session.Dispose();
      }
      catch (Exception e)
      {
        Debug.WriteLine(e.ToString());
        MessageBox.Show(e.ToString());
      }
    }
  }
}

The two things that make all this possible are:

  • Microsoft.WindowsMobile.BatteryState - an enumeration for testing the state of the battery (e.g. low, critical, is it charging etc.)
  • Microsoft.WindowsMobile.Outlook.OutlookSession - a pocket outlook session object that enables you to look at contacts, tasks, appointments, and mail (although the mail part isn't very well documented in the SDK).

I have a bit more testing to do (waiting for my battery to drain is a little time consuming - and I can't seem to figure out how to set the battery level in the SDK emulator) and I may put a nice form to properly configure the tool.  Overall using the Windows Mobile 5.0 SDK - for what I thought would be a tricky control - has been a very pleasant experience.

Comments

Jason Marlar

I was wondering if there is a way to delete some of the standard ringtones/sounds on the phone that I will never use as well as some of the background images. I would like to free up as much space as possible on my Blackjack and dont have any need for these files. They are protected and Microsoft support keeps telling me that I have to call Samsung and samsung tells me to call Microsoft. Got any advice?
# February 21, 2007 12:30 PM

admin

Have you tried using an SD card instead?  You can get a 2gb Micro SD card that works very well for photos, music etc..

# June 7, 2007 10:54 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS