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


Roll Your Own SMS Gateway : Page 2

You can do a lot of fun applications using an SMS gateway but unless you work for a major television network, good luck getting a return on your investment. In this article, we'll show you a great alternative to an expensive SMS gateway using a Pocket PC and a Bluetooth connection.

Developing the Pocket PC Application
Let's start by first developing the Pocket PC application. In Visual Studio 2005, create a new Windows Mobile 5.0 Pocket PC application and name it SMS. Populate the default Form1 with the controls shown in Figure 2.

Figure 1. The architecture of the SMS gateway system is shown.
Figure 2. Populate Form1 with the various controls as shown.

For the TextBox1 and TextBox2 controls, set their properties as follows:
  • Multiline—True
  • ScrollBars—Both
Add the following references to the project:
  • Microsoft.WindowsMobile
  • Microsoft.WindowsMobile.PocketOutlook
Switch to the code-behind of Form1 and import the following required namespaces:

Imports Microsoft.WindowsMobile.PocketOutlook
Imports Microsoft.WindowsMobile.PocketOutlook.MessageInterception
Declare the following constants and variables:

Public Class Form1
    '---the constants for the commands---
    Const VOTE_COMMAND As String = "VOTE"
    Const SEND_COMMAND As String = "SEND"

    Private msgInterceptor As MessageInterceptor
    Private WithEvents serialPort As New IO.Ports.SerialPort
The first constant, VOTE_COMMAND, is used to filter out SMS messages sent by the user. For example, if the user wants to vote for participant A, then his SMS message will be sent in the following format:

VOTE <Choice> <Social Security Number>
An example would be "VOTE A 987-65-4329." All incoming SMS messages beginning with the word VOTE would be filtered and processed by this application.

The second constant is used to filter the command sent by the server. When the server receives a SMS vote, it will send a command to the Pocket PC application so that it can send an acknowledgment SMS message back to the user. For example, the server might send the following command to the Pocket PC application: "SEND:+123456789:Thank you for your vote!." The Pocket PC will then send an SMS message to the user.

The msgInterceptor object is used for intercepting incoming SMS messages. The serialPort object is used for communicating with the server over a Bluetooth connection.

In the Form1_Load event, create an instance of the MessageInterceptor class and set its filter to intercept for incoming SMS messages that start with the word "VOTE" (regardless of casing). Then, set an event handler to service this event (when a message is intercepted). At the same time, you will also open a serial connection (over Bluetooth) to connect with the server:

    Private Sub Form1_Load( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles MyBase.Load

        '---create an instance of the MessageInterceptor class---
        msgInterceptor = New MessageInterceptor( _
           InterceptionAction.NotifyAndDelete, True)

        '---set the filter for the message---
        msgInterceptor.MessageCondition = _
           New MessageCondition( _
           MessageProperty.Body, _
           MessagePropertyComparisonType.StartsWith, _
           VOTE_COMMAND, False)

        '---set the event handler for the message interceptor
        AddHandler msgInterceptor.MessageReceived, _
           AddressOf SMSInterceptor_MessageReceived

        '---close the serial port if open---
        If serialPort.IsOpen Then
        End If

        '---open the serial port to connect to server---
            With serialPort
                .PortName = "COM6"
                .BaudRate = 9600
                .Parity = IO.Ports.Parity.None
                .DataBits = 8
                .StopBits = IO.Ports.StopBits.One
                .Handshake = IO.Ports.Handshake.None
            End With
        Catch ex As Exception
        End Try
    End Sub

Author's Note: I am assuming that COM6 is used by the Bluetooth serial port on the Pocket PC. Change this COM port according to the port number used on your Pocket PC.

When an SMS message is intercepted, the SMSInterceptor_MessageReceived event handler will be fired:

    '---event handler for the MessageReceived event---
    Private Sub SMSInterceptor_MessageReceived( _
       ByVal sender As Object, _
       ByVal e As MessageInterceptorEventArgs)

        '---extract the message received---
        Dim msg As SmsMessage = e.Message

        '---format as "+123456789,VOTE A 987-65-4329"---
        Dim s As String = msg.From.Name & "," & msg.Body

        '---update the textbox with the above data---
        TextBox1.BeginInvoke(New _
           myDelegate(AddressOf updateTextBox1), _
           New Object() {s})
    End Sub
Here, you will extract the sender's phone number as well as the content of the SMS message. You need to cast the e.Message object into a SmsMessage object in order to retrieve the detailed information of the sender. The received data is then displayed in the TextBox1 control.

Author's Note: Interestingly, if the sender's phone number has already been saved in Contacts, the From.Name property will return the sender's name (as saved in Contacts) instead of his phone number. In this case, you need to do some additional work to find out the sender’s phone number.

The delegate and the subroutine to update the TextBox control are defined as follows:

    '---delegate for updating the two TextBox controls---
Public Delegate Sub myDelegate(ByVal str As String)

    '---display received SMS data---
    Public Sub updateTextBox1(ByVal str As String)
        '---show the received data in the TextBox---
        TextBox1.Text = str & vbCrLf & TextBox1.Text

        '---Send the data to the server---
    End Sub
Figure 3 shows the application when SMS messages are received.

Figure 3. SMS messages have been received by the application.
Figure 4. Messages from users and from the server are shown.

When the SMS message is shown on the TextBox control, it is also sent to the server, through the SendDatatoServer() subroutine:

    '---sends the received SMS data to the server---
    Private Sub SendDatatoServer(ByVal str As String)
            '---write to the serial port---
            serialPort.Write(str & vbCrLf)
        Catch ex As Exception
        End Try
    End Sub
When the server receives the SMS message sent by the Pocket PC application, it will also send a command to the Pocket PC, asking it to send an acknowledgement message back to the sender. In this case, the command will be received on the Pocket PC via the serial port connection. Essentially, you will service the DataReceived event of the serialPort object:

    '---data received by the serial port (from server)---
    Private Sub DataReceived( _
           ByVal sender As Object, _
           ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
           Handles serialPort.DataReceived

        Dim data As String = serialPort.ReadLine()

        '---update the textbox with the data from server---
        TextBox2.BeginInvoke(New _
           myDelegate(AddressOf updateTextBox2), _
           New Object() {data})
    End Sub
The received command is displayed in the TextBox2 control:

    '---update the TextBox with the data received from server---
    Public Sub updateTextBox2(ByVal str As String)
        '---show the received data in the TextBox---
        TextBox2.Text = str & vbCrLf & TextBox2.Text

        If str.StartsWith(SEND_COMMAND) Then
            Dim fields() As String = str.Split(":")
            '---.e.g. SEND:+123456789:Thank you for your vote!
            '---fields(0) is SEND, fields(1) is +123456789, 
            '---fields(2) is "Thank you for your vote!"
            '---send SMS to user---
            SendSMS(fields(1), fields(2))
        End If
    End Sub
The Pocket PC application then sends an acknowledgement SMS message back to the sender using the SmsMessage class:

    '---send a SMS message to a recipient---
    Private Sub SendSMS( _
       ByVal receiptnumber As String, _
       ByVal message As String)
        '---from the Microsoft.WindowsMobile.PocketOutlook namespace---

        '---compose a new SMS message---
        Dim SMS As New SmsMessage
        '---set the body of the message---
        SMS.Body = message
        '---add the recipient---
        SMS.To.Add(New Recipient(receiptnumber))
        '---send the message---
    End Sub
Figure 4 shows messages received from users as well as commands sent by the server (more on this in the next section).

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date