Intercepting SMS Messages with Windows Mobile V5 and the Compact Framework V2

Check out for a working application utilizing the SMS Message Interceptor...

Windows Mobile 5.0 and the Compact Frame V2 introduce a neat and simple concept that allows you to easily intercept a SMS message and do some processing on it effectively enabling you to use SMS as an underlying transport for your applications - be it a game or some line of business app. 

You can tell the SMS Interceptor to look out for specific message types by specifying a message signatures and then just structuring the SMS message body with the data you want to ship around, pretty darn cool and simple.  

For the moment you wont find a lot about the "Microsoft.WindowsMobile.PocketOutlook.MessageInterception Namespace" in the VS 2005 B2 documentation but you can find enough to get you going on MSDN here and just spotted a webcast on MSDN titled "New Managed Messaging, State, and Notification APIs in Windows Mobile (Level 300)" probably worth a gander...

Appreciating that Windows Mobile 5.0 is not broadly available in real devices yet and the compact framework V2 is still beta there is still plenty that you can do with the technology using the Windows Mobile 5.0 emulators. You can download the emulator/sdk from here or you can order it here. Interestingly enough I found the emulator running on a 3.2 GHz HT CPU faster than a Motorola MPx200 and fast enough to try stuff for real, yeah contrary to my "emulator is slow article:-)"

To enable the SMS Interceptor you need need the following lines of code...

Declare and instantiate the message interceptor classes

using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;

private MessageInterceptor SMSProcessor = new MessageInterceptor(InterceptionAction.NotifyAndDelete, true);
private MessageCondition msgCondition = new MessageCondition();

In your start up code

//set up filter - for messages to be intercepted by this app then they must start
msgCondition.Property = MessageProperty.Body;
msgCondition.ComparisonType = MessagePropertyComparisonType.StartsWith;
msgCondition.ComparisonValue = "?"; //Intercept messages starting with a ?

SMSProcessor.MessageCondition = msgCondition;

//set up event handler to process incoming messages
SMSProcessor.MessageReceived += new MessageInterceptorEventHandler(SMSProcessor_MessageReceived);

Declare your event handler

void SMSProcessor_MessageReceived(object sender, MessageInterceptorEventArgs e)
theSMS = (SmsMessage)e.Message;
// process your message
// work around to a message pump issue

To send a SMS Message inside the emulator to the emulator then use the magic +14250010001 number.

using (ol = new OutlookSession())
SmsMessage testMessage = new SmsMessage();
testMessage.To.Add(new Recipient("+14250010001"));

//Using the '?' character as the message signature
testMessage.Body = string.Format("?{0}", messageToSend);

I'm working on a larger app that implements the interceptor and will post up on to web when done, it's 60% done, just need a few free evenings to finish it off - I'll blog it's location when it's done

A Bug
Alas for the moment there is a bug in the compact framework that tends to blow up your app if it's left too long or gets switched out of focus, it's a known bug that is being worked on. Don't let it dampen your enthusiasm for giving this stuff a go but just be aware that there is an issue.

If run the interceptor on the same thread as your form then you may get a

testIntercept.exe ArgumentException at SafeNativeMethods.DispatchMessage() at Microsoft.WindowsMobilePocketOutlook.MessageInterception.MessagingMessageWindow.MessagePump() "

if you run the interceptor on it's own thread then you'll get

"An unhandled exception of type 'System.ArgumentException' occurred in Microsoft.WindowsMobile.PocketOutlook.dll.”

Cheers and have fun!! Dave