Section 8.3. Example Service: Echo and Hello World Using a Smartphone

Using a Smartphone as an SMS Modem > Example Service: Echo and Hello World Using a Smartphone

8.3. Example Service: Echo and Hello World Using a Smartphone

To implement an SMS service using a smartphone, you don't need a web server at all. Our example smartphone service will do two things:

  • When an incoming message is received (e.g., "Testing"), it will echo back the message it received (e.g., "You said Testing").

  • It will display a form with text boxes for a phone number and a message body. When you click a button on the form, it will send your message to that number.

8.3.1. How to: Using a Smartphone to send and receive SMS

Microsoft Research India has released a free toolkit that abstracts the SMS functionality on the phone and remotes them through ActiveSync to the PC. This allows you to write and execute your application on your Windows PC. The smartphone functions as an SMS modem.

It includes the Windows PC side and Windows Mobile smartphone software, as well as a C# template for creating a service. Quite helpfully, it also includes a phone emulator, so you can develop and test your application without actually using a smartphone.

8.3.1.1. Installing the Microsoft Research SMS Toolkit

You can download the toolkit from http://www.codeplex.com/smstoolkit. The instructions on the web site are relatively self-explanatory. However, it is worth highlighting a few gotchas to avoid frustration:

  • On Windows Vista, the installer for toolkit must be run as administrator even if you are logged in with an account that has administrator privileges. To do so:

    • Save the installer to your local hard drive

    • Click Start->More Programs->Accessories

    • Right-click "Command Prompt"

    • Choose "Run as administrator"

    • Use the command line to execute the installer

  • The PC application requires SQL Server 2005 Compact Edition and ActiveSync 4.0. These are both free downloads available from Microsoft's web site, but the toolkit installer will not install them for you, neither will it check to make sure they are installed. You will simply hit errors when you try to run your application.

  • Most Windows Mobile smartphones come "application locked," meaning they are limited in terms of what types of applications can be installed and what access the install process has to the system. It is a straightforward process to unlock your phone, and instructions on doing so are provided on the SMS Toolkit web site, but at the bottom of the page. Be sure to complete this step before you try to install the Toolkit software.

  • Windows Mobile 5 or Windows Mobile 6 is required. Smartphones with earlier versions of the Windows Mobile operating system do not have the necessary APIs to support the Toolkit.

8.3.1.2. Getting started

Once you have installed the toolkit according to the instructions on the Codeplex web site and above, simply create a new project in Visual Studio using the SMS Service template. It will create several files for you, but Form1.cs is where the core of the example lies.

When the component is initialized, the following code executes:

SmsService = new SmsServiceClass("SMS_Service"); SmsService.OnDeliveryReportReceived +=     new OnDeliveryReportDelegate(SmsService_OnDeliveryReportReceived); SmsService.OnSmsReceived += new OnSmsReceivedDelegate(SmsService_OnSmsReceived);     

The first line creates the SMS service. You'll select this service when running the emulator, and invoke it when sending an SMS.

The second and third lines create event handlers for the delivery report and for incoming SMS messages. SMSService_OnSMSReceived is what will be invoked when you receive an SMS.

Taking a quick look at SMSService_OnSMSReceived, we see exactly how easy it is to write an SMS server using Microsoft Research India's SMS Toolkit:

SmsResponseClass SmsService_OnSmsReceived(SmsClass SmsMessage) { SmsResponseClass Response = new SmsResponseClass(); SmsClass SmsItem = new SmsClass(); SmsItem.Body = "Your Response"; SmsItem.Number = SmsMessage.Number; Response.AddResponseSms(SmsItem); return Response; }

If you make no changes to this code at all and simply connect the phone and run the application as it comes out of the template, the above function will be invoked and reply to whatever message was received with "Your Response."

To implement our echo response, simply modify the line:

SmsItem.Body = "Your Response";

to:

SmsItem.Body = "You said" + SmsMessage.Body;

This will place the body of the incoming message into the body of the outgoing message. When you return the Response class object that contains the SmsItem you just created, the application will send out your message to the same number it received a message from.

NOTE

If you have used your smartphone as a phone, and have entries in the address book, SmsClass.Number may have more information than you expect. For example, when receiving a text message from my friend Peter Brown, SmsMessage.Number resolved to Brown, Peter" <+12065551234>. If you simply take SmsMessage.Number, stuff it into SmsItem.Number and add it to Response, it will work...sometimes.

I found that this format worked fine when replying to messages sent from the T-Mobile and Cingular networks, but Sprint simply failed to deliver my replies.

To work around the problem, clean it up:

using System.Text.RegularExpressions; ... Regex regex = new Regex("[^0-9]"); SmsMessage.Number = regex.Replace(SmsMessage.Number, "");

This will strip out everything but actual numbers and ensure that it is respected by all carriers.

8.3.1.3. Sending a message

To send a message programmatically, we will use the SmsService.SendSms(SmsClass Sms)function.

In designer mode of Form1.cs, add two text boxes and a button. Name the text boxes txtPhoneNumber and txtMessageBody and name the button btnSendSMS. You'll probably want to make txtMessageBody multiline and add a couple of labels to keep track of what's going on as well. When you're done, it should look something Figure 8-1.

Figure 8-1. Sending a message


Now, double-click the btnSendSMS button to create a new function for click event and fill it in as follows:

private void btnSendSMS_Click(object sender, EventArgs e)     {       SmsClass SmsItem = new SmsClass();       SmsItem.Number = txtPhoneNumber.Text;       SmsItem.Body = txtMesssageBody.Text;       SmsService.SendSms(SmsItem);     }

You're ready to go. Run your application, fill in a number and message in the body, click your button and your message is on its way.

The first line of this function creates a new SmsClass instance. The next two lines fill in the number and body of the instance and the fourth line sends the message we just created. Easy, huh?

NOTE

You cannot invoke the SmsService.SendSMS method from within the SmsService.OnSmsReceived function. Because of threading issues, your application will hang if you attempt this.

Instead, if you wish to send more than one message from within the OnSmsReceived function, just create a new SmsClass instance and add it to the Response class instance of the SMSResponseClass array:

SmsClass secondResponse = new SmsClass(); secondResponse.Body = "This is the second response"; secondResponse.Number = "2065555555" Response.AddResonse(secondResponse);

 

 



How to Build an SMS Service
How to Build an SMS Service
ISBN: 789742233
EAN: N/A
Year: 2007
Pages: 52
BUY ON AMAZON

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net