The following examples were take from several sources. The code is written in C#.
Including message interception in your application
- Instantiate a MessageInterceptor object
MessageInterceptor mi = new MessageInterceptor(InterceptionAction.NotifyAndDelete);
- Set a condition, how the SMS must look, which will be used by your application:
mi.MessageCondition = new MessageCondition(MessageProperty.Body, MessagePropertyComparisonType.StartsWith, trigger);
Now every SMS starting with the words specified in the string trigger will be intercepted..
- Tell the MessageInceptor what to do when a message was intercepted:
mi.MessageReceived += new MessageInterceptorEventHandler(mi_MessageReceived);
now mi_MessageReceived will be called when the MessageCondition is met.
- Implement mi_MessageReceived()
protected override void mi_MessageReceived(object sender, MessageInterceptorEventArgs e)
{
// add code to be executed when receiving the specific SMS
}
Registering the application
In order to activate the MessageInterceptor it must be enabled, e.g. the trigger and the application (appPath) must be put in the WM registry:
mi.EnableApplicationLauncher(trigger, appPath);
Now when your application is stopped (terminated) and even after a soft reset, a SMS with the specified trigger will start your application.
The information for the application launcher is stored in the windows registry. It will be removed through a hard reset, or by executing the following code snippet:
if (MessageInterceptor.IsApplicationLauncherEnabled(trigger))
{
mi = new MessageInterceptor(trigger);
mi.DisableApplicationLauncher();
}
The trigger from before is needed. This can be done from your application or any other program.
Using the SMSLaunch class
In order to make it easier to write an application using message interception the SMSlaunch class (using Vodafone.betavine namespace) was written.
The classes are available under the project WM_ApplTrigger. It is provided as an Visual Studio 2005 project and as Vodafone.betavine.dll.
SMSLaunch simulates a push registry. Similar to push registry applications it allows to start a message through a SMS. This is done by a trigger string included in the message body. So far it only is to be possible to send text messages using C#. In the SMSLaunch class the trigger string consists of "push: " + a 4 digit port number, e.g. "push: 0000". This trigger is used to identify the application to be launched. Please note that the port is not used as a port in the strict sense, rather as a unique identifier. The term port was "mis"-used as they play a role in MIDP. No actual ports are used in the application.
SMSLaunch extends the Form class and can be used by any application by extending it. This application needs to the have the following code included:
- Call the base constructor of SMSLaunch with a 4 digit string as argument, e.g.
public myApplication() : base("1234")
{
// add code
}
- Implement mi_MessageReceived()
protected override void mi_MessageReceived(object sender, MessageInterceptorEventArgs e)
{
// add code to be executed when receiving the specific SMS
}
Example: SMSAlarm
When you send a SMS the receiver will only be notified by the means set in the mobile device. Sometimes it might be useful to get around it and play some soundfile. The example is available under the project
WM_ApplTrigger.
using System;
using System.Windows.Forms;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;
using Microsoft.WindowsMobile.PocketOutlook;
using Vodafone.betavine;
namespace SMSAlarm
{
public partial class SMSAlarm : SMSLaunch
{
#region PlaySound
// To play a sound, we need to PInvoke into coredll.dll and call PlaySound
[System.Runtime.InteropServices.DllImport("coredll.dll")]
static extern int PlaySound(string pszSound, IntPtr hMod, int fdwSound);
const int SND_FILENAME = 0x20000;
const int SND_SYNC = 0x0;
const int SND_ASYNC = 0x1;
const int SND_LOOP = 0x8;
#endregion
public SMSAlarm() : base("1234")
{
InitializeComponent();
}
protected override void mi_MessageReceived(object sender, MessageInterceptorEventArgs e)
{
string theSMS = ((SmsMessage) e.Message).Body;
string soundFile = ""; // sound to play when event raised, empty = default sound
// Play the sound in a loop
PlaySound(soundFile, IntPtr.Zero, SND_FILENAME | SND_ASYNC | SND_LOOP);
MessageBox.Show(theSMS.Remove(0, 10), "SMS Alert");
// The message box has been dismissed, stop the sound and exit
PlaySound(null, IntPtr.Zero, 0);
}
}
}
This is just a partial code. The whole project can be downloaded here.
After installing/running it once, the alarm can be invoked by sending a SMS with the content
push: 1234 Your personal message
this will launch SMSAlarm, play a continuous beeping sound and display a message box with the title "SMS Alert" and the body "Your personal message".
Hint:
When developing, it is useful to include buttons/menu items allowing to disable (deregister) and exit the application. Otherwise you get deployment errors and risk that an old version is used while testing. The project contains menu items for these options.
Unresolved issues
Some inconveniences were encountered while programming the project. There might be trivial solution to solve them, so please post them in the
application trigger blog.
- Forms elements cannot be inherited in Visual Studio 2005. Hence including the above described exit/disable menu items could not be done in SMSLaunch
- Under C# only text messages can be send. Is it possible to send BLOBs by overloading the SMS protocol.