RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Build a 'SNAPI' App that Utilizes Windows Mobile 5.0 Managed APIs : Page 3

The new Managed APIs in Windows Mobile 5.0 enable developers to do more, whether it's interacting with Pocket Outlook, working with the new telephony API, or sending and receiving SMSs. This article will help you get started using WM 5 with a great sample application that uses the State and Notification API to automatically sends an SMS message to callers when you are busy.

Sending an SMS
In order to send an SMS message you need to use the SmsMessage class. This class is present in the assembly Microsoft.WindowsMobile.PocketOutlook.

Add a reference to this assembly and in your form import the namespace:

Imports Microsoft.WindowsMobile.PocketOutlook
Create a Sub called SendSMSMessage which accepts as string parameter 'msg':

    Private Sub SendSMSMessage(ByVal msg As String)
           Dim _smsMessage As New SmsMessage
            _smsMessage.Body = msg
            _smsMessage.To.Add(New Recipient(PhoneNumber))
    End Sub
To send an SMS message you need to specify two things: the message text or body and the message recipient(s). In the SendSMSMessage sub, I've created an instance of the SmsMessage class called "_smsMessage." Next I've specified a value 'msg' as the message body and specified that this message should be sent to a particular phone number by passing the variable 'PhoneNumber' to the property "To" of this class by invoking it's Add method. The application supports multiple recipients and, thus, this 'To' property gets the collection of intended message recipients. Next I've created a New Recipient Object by passing it the PhoneNumber as a string parameter. Finally, send the SMS message by invoking the Send method of the SmsMessage class. That's all there is to sending an SMS message from your program.

Adding a Task Reminder
The last bit of functionality that I defined for this application is to create a reminder for the user that will prompt them to return the call later. To do this I want to create a new Outlook task for the user.

Define a new routine called SetTaskReminder.

    Private Sub SetTaskReminder()
        'Create a an outlook session
        Dim MySession As New OutlookSession
        Dim start, due, reminder As Date
        start = Now.AddMinutes(15)
        due = Now.AddMinutes(20)
        reminder = Now.AddMinutes(10)

        'Define the Task and set it's properties
        Dim ReminderTask As New Task
        ReminderTask.Subject = "Call Tel:" & PhoneNumber
        ReminderTask.DueDate = due
        ReminderTask.StartDate = start
        ReminderTask.ReminderSet = True
        'ReminderTask.ReminderTime = reminder
        'Bug in task.ReminderTime prevents one from setting time correctly, hence using
 'the following as a workaround.
        ReminderTask.ReminderTime = New Date(reminder.Year, reminder.Month, _
        reminder.Day, reminder.Hour, reminder.Minute, reminder.Second)
        ReminderTask.Body = "Call Tel:" & PhoneNumber
        'Add the task to the current outlook session

    End Sub
To add a task to Outlook tasks, you have to create an instance of the OutlookSession class and add the task. The next thing that happens in the above code is the creation of three variables to store the start time, due time, and the reminder time for the task.

Next I've created an instance of Task Class and specified its properties such as the Subject of the task, the DueDate, The StartDate, and specifying that a reminder is required. It's important to note that, as of this writing, there is a bug in the ReminderTime property, which Microsoft has not yet resolved. The property is supposed to accept a date, but in my code I've provided each value separately—including the year, month, day, reminder hour, reminder minute, and second values.

Next I've provided a value for the body of the task and added this task to the OutlookSession and fired an update. This adds a reminder task. If you also provide the subject or the body of the message the phone number data in the exact format as given above WM5 will recognize the number as a phone number and allow the device user to call that phone number without dialing.

Wrapping it Up
So far you have written code with three abilities:

  1. Gets notification of an incoming call and capture information about the call (phone number)
  2. Sends an SMS
  3. Creates a task/reminder.
You now need to orchestrate all the above and allow the user to create the message and execute this functionality when he or she chooses.

Add the following controls to the main form of your application.

  1. TextBox
  2. Button
Set the TextBox's text property to "Will call later".
Set the Button's text property to Submit.

Double-click the button to add code to its click event and call the two methods defined earlier (SendSMSMessage and SetTaskReminder) one after the other.

That's it! Run the application by hitting F5 and selecting your WM5 device as the target. The application loads with the default form. To test it, call your phone from another mobile phone. This will invoke the CheckCallStatus method. The variable PhoneNumber will be assigned the value of the incoming caller phone number. If you click the submit button, the message "Will call later" (or whatever you happened to put in the text box) will be sent to the caller. In addition a task/reminder will be created on the receiving phone.

Enhancements to Consider
Some things you might want to do to enhance the code further:

  • Provide the user the ability to have a list of common messages such as "In a meeting," "On another call" etc.
  • Add error-handling code to check that the phone number is actually a number. (For example, you want to handle blocked phones such as "Private Caller" or something similar. In addition, you need to trap for land line (POTS) numbers as these will not be able to receive a text message and different countries display numbers in different ways.)
  • Enhancing the SMS functionality by giving the user an option for delivery notification.
By now you've gotten a good introduction to SNAPI and you've seen how it can help you build a more responsive application. There are numerous others such as the Microsoft.WindowsMobile.PocketOutlook.MessageInterception Namespace, which provides classes for SMS message interception or classes to work with the Media Player etc. It is a rich environment that enables one to build a number of interesting applications that were either too difficult or impossible before.

Sanjay Shetty is the CEO of Wireless Strategist & Consultants, a consultancy for mobility, design, architecture, and Microsoft.NET. He is the Microsoft Regional Director in Mumbai, India.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date