devxlogo

Extend SMS Messaging to the Desktop Using Windows

Extend SMS Messaging to the Desktop Using Windows

MS messaging is one of today’s most common forms of communication. People send SMS messages everywhere?on the train, crossing the road, and even driving! While most SMS messages are sent on the go, a huge portion of them are sent when stationary?while people are at their desks, working. Though today’s users are accustomed to composing messages using T9 inputs (or using a virtual keyboard, etc.), nothing beats using the keyboard you have for your computer. Therefore, you’d ideally prefer to send SMS messages from the comfort of your own keyboard.

This article will show you how to write applications to allow SMS messages to be sent from within your Windows computer. All you need is a Windows Mobile device (with a valid SIM card for sending SMS messages) and a syncing cable. Figure 1 shows the flow of the applications that you’ll build in this article.

Figure 1. Architecture: Sending SMS messages from the desktop.

How It Works
To send SMS messages from within your Windows computer, you first connect your Windows Mobile device to your computer using ActiveSync. This allows the Windows computer to communicate with the device. You will write two applications: One for the device sending your messages and another for the desktop on which you’ll compose them.

These two applications will communicate with each other using TCP/IP. But how do they know each others’ IP addresses? The trick lies in understanding that when your device is connected to the computer using ActiveSync, it acts as a DHCP server and assigns a fixed IP address to the host computer. This IP address is: 169.254.2.2. The two applications can now rendezvous at this IP address.

Creating the SMS Sender Windows Mobile Application
First, you’ll create the Windows Mobile application that sends the messages. Using Visual Studio 2008, create a new Smart Device project (select the Windows Mobile Professional template and the .NET CF 3.5) and name it SMSSender.

SMSSender application does not really need a user interface; hence you don’t need to populate the default Form1 with any controls.

Add a reference to the following DLLs:

  • Microsoft.WindowsMobile.PocketOutlook
  • Microsoft.WindowsMobile.Status

Switch to the code-behind of Form1 and import the following namespaces:

using System;using System.Windows.Forms;using System.Net;using System.Net.Sockets;using System.Text;using Microsoft.WindowsMobile.PocketOutlook;Define the following constants and objects:    public partial class Form1 : Form    {        const string DESKTOP_IP_ADDRESS = "169.254.2.2";        const int portNo = 3456;        const int DATA_SIZE = 1024;        byte[] data = new byte[DATA_SIZE];        Socket desktop;

To communicate with the desktop Windows application, you’ll use a Socket object in the .NET Compact Framework.

Define the SendSMSMessage() function so that you can send a SMS message from within the Windows Mobile device:

private void SendSMSMessage(string message)        {            string[] msg = message.Split('|');            SmsMessage sms = new SmsMessage()            {                Body = msg[1]            };            sms.To.Add(new Recipient(msg[0]));            sms.Send();            //---inform the desktop---            SendMessage("Message sent!");        }

The SendSMSMessge() function takes in a single string parameter containing the recipient phone number and the message body. These two fields are separated by a pipe character (|).

The SendMessage() function sends a message back to the desktop application through the socket connection. It is defined as follows:

        //---send data to the desktop---        private void SendMessage(string message)        {            byte[] data =              Encoding.ASCII.GetBytes(message);            desktop.Send(data);        }

Define the ConnectDesktop() function so that you can establish a connection with the desktop application:

private void ConnectDesktop()        {            //---IP address and port no. of the desktop---            IPAddress desktop_Address =                IPAddress.Parse(DESKTOP_IP_ADDRESS);            IPEndPoint endPoint = new IPEndPoint(desktop_Address, portNo);            //---connect to the desktop---            desktop = new Socket(endPoint.AddressFamily,               SocketType.Stream, ProtocolType.Tcp);            desktop.Connect(endPoint);            //---read incoming data from the desktop---            IAsyncResult ar = desktop.BeginReceive(               data, 0, DATA_SIZE, SocketFlags.None,               ReceiveMessage, null);        }

The ReceiveMessage() function listens for incoming messages from the desktop application. When a message is received, it is sent to the SendSMSMessage() function, so that a message can be sent (see Listing 1).

Finally, when Form1 loads, you can make a connection to the desktop application:

        private void Form1_Load(object sender, EventArgs e)        {            ConnectDesktop();        }

That’s all for the SMSSender application.

Creating the Desktop Windows Application
The next step is to create the desktop application for sending messages. Add a new project to the current solution and name it SMSSender_Desktop.

Populate the default Form1 with the controls shown in Figure 2.

Figure 2. Form1: Populate Form1 with these controls.

Switch to the code-behind of Form1 and import the following namespaces:

using System;using System.Windows.Forms;using System.Net;using System.Net.Sockets;using System.Text;

Listing 2 defines the SMSSender class to facilitate communication with the Windows Mobile application.

The SMSSender class contains a public SendMessage() method that allows you to send a message to the application on the Windows Mobile device. The private ReceiveMessage() method receives messages from the application on the Windows Mobile device and simply shows them using the MessageBox class. The constructor for the SMSSender class takes in a TcpClient object.

In Form1, define the following constant and objects:

    public partial class Form1 : Form    {        const int portNo = 3456;        static IPAddress localAdd = IPAddress.Parse("169.254.2.2");        static TcpListener listener = new TcpListener(localAdd, portNo);        SMSSender smsSender;

When the form loads, listen for a TCP connection from the Windows Mobile device and then create an instance of the SMSSender class by passing it an instance of the TcpClient object:

private void Form1_Load(object sender, EventArgs e)        {            //---listens from a connection from the WM device---            listener.Start();            smsSender = new SMSSender(                listener.AcceptTcpClient());        }

When the user has finished composing a message and clicks the Send button, the SMSSender object sends the message to the Windows Mobile application to process:

        //----send a message using the SmsSender object---        private void btnSend_Click(object sender, EventArgs e)        {            string sms = string.Empty;            sms += txtPhoneNumber.Text + "|";            sms += txtMessage.Text;            smsSender.SendMessage(sms);        }

As the user types the message, the number of characters remaining is displayed:

private void txtMessage_TextChanged(object sender, EventArgs e)        {            lblCount.Text = txtMessage.Text.Length + "/160";        }
Figure 3. It Works! You can now send SMS messages from the comfort of your desktop.

Testing the Applications
To test the applications, first connect your device to the desktop using ActiveSync. Next, run the SMSSender_Desktop project and it will wait for an incoming socket connection from the device. At the same time, deploy the SMSSender project to the device and run it. When the application is started, it will automatically connect to the Windows application at >169.254.2.2. Once the connection is successful, you will see the Windows application appear. You can now send SMS messages from the comfort of your desktop (see Figure 3)!

You can use the techniques discussed to extend the sample applications in the following ways:

  • Modify the application to retrieve all the messages stored on the device and then send them to the Windows application.
  • Notify the user when an incoming message is received.
  • Retrieve all the contact information from the device and show them in a dropdown list on the Windows application.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist