Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Extend SMS Messaging to the Desktop Using Windows : Page 3

When you think of SMS messaging, you probably think about your mobile device. But what if you could SMS someone from the comfort of your own desktop? This article shows you two messaging applications: One application to write the messages on your desktop and one application to send the messages from your device.


advertisement
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.



Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap