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.