Program Mobile Devices
Use the .NET Compact Framework to write an application for sending SMS messages on Bluetooth-enabled Pocket PCs.
Technology Toolbox: VB.NET, .NET Compact Framework, Pocket PC, Bluetooth
Short Message Service (SMS) first became popular in European and Asian countries, but now the SMS wave has hit the U.S. digital world. Users typically employ the numeric keypad on their cell phones to compose SMS messages and view them on the small screen of the mobile handset.
I'll show you how to use the .NET Compact Framework to write a Pocket PC application that sends SMS messages through a Bluetooth-enabled cell phone. Your users will be able to jot down messages on their Pocket PCs (or use the built-in virtual keyboard) to send and receive SMS messages directly.
There is a problem with this scenario: The Compact Framework lacks managed classes for programming Bluetooth. However, you can use a workaround involving the Platform Invoke technique to invoke the Bluetooth functionality through serial ports. Also, the Windows CE operating system (which powers the Pocket PC) lacks native APIs for sending SMS messages. However, the Pocket PC Phone Edition does provide such APIs, which you should use instead of the serial ports if you're working with this edition.
This article requires Microsoft Visual Studio .NET 2003, along with a Pocket PC and a mobile handsetboth with Bluetooth functionality. I've developed a sample SMS app, which I tested with an HP iPAQ 4150 (also a 3870) Pocket PC and a Sony Ericsson T68i cell phone (download the sample app here).
You need to understand how to communicate with mobile handsets through the AT command set before writing the app itself. Essentially, you issue the "AT" keyword followed by the various commands you want to execute (see Table 1). For example, "AT+CMGR" is the command for reading a message at a specified index:
This command returns a long string of alphanumeric characters in the Protocol Data Unit (PDU) format. Some handsets can send or receive SMS messages in both plain text and PDU format. Others, including the Sony Ericsson T68i, support only PDU.
You use "AT+CMGS" to send a message like this:
AT+CMGS=24 (size of message)
The ">" character means the command is waiting for your entry of the long PDU format of the message you want to send. You end the entry with Ctrl-Z, which you can implement programmatically using chr(26). Use the appropriate tables for translating between SMS and PDU in messages to be sent in PDU (see Table 2), then received by the handset (see Table 3).
Next you encode and decode messages. You convert plain-text messages into PDU in hexadecimal format (see Table 4). You reverse the process to decode them (see Table 5).
Write Your SMS App
Now you're ready to write the app. Launch Visual Studio .NET 2003 and create a new Smart Device application using Visual Basic .NET. Choose Pocket PC for the project type. Populate the Label, TextBox, Button, and ListView controls (see Figure 1).
Add a module to your project that contains all the supporting functions used in the application (see Listing 1). You need to communicate with the Bluetooth-enabled handset using serial communication, which means you must use Platform Invoke (P/Invoke) to import this namespace:
Declare some functions defined in the Windows CE operating system (see Listing 2), then define three global variables within the default Form1:
Dim fileHandler As Long
Dim readcount As Integer
Dim numRead As Integer
Open the port for Bluetooth communication when the form is loaded:
Private Sub Form1_Load(ByVal sender _
As System.Object, ByVal e As _
System.EventArgs) Handles _
'port no. for Bluetooth connection
Dim port As Short = 6
'Check your device for exact port no.
'Opens the port for Bluetooth
fileHandler = CreateFile("COM" & _
port & ":", &HC0000000, 0, _
0, 3, 0, 0)
The Send() function sends the formatted message to the handset:
Public Function send(ByVal message As _
String) As Integer
'Send message through serial port
Dim value As String = message & _
readcount = WriteFile(fileHandler, _
GetBytes(value), value.Length(), _
Conversely, the receive() function receives messages from the handset:
Public Function receive() As String
'Receive message through serial port
Dim inbuff(300) As Byte
numRead = 0
readcount = ReadFile(fileHandler, _
inbuff, inbuff.Length, numRead, _
Write a subroutine that formats and sends a message in PDU format when the subroutine is invoked by clicking on the "Send SMS" button (see Listing 3). Then write another subroutine that reverses this process when users click on the "Get SMS" button (see Listing 4). Finally, add a "number of characters typed so far" counter to the user interface's label control, because SMS messages are limited to 160 characters:
Private Sub _
txtMessage_TextChanged(ByVal sender _
As System.Object, ByVal e As _
System.EventArgs) Handles _
' Displays total characters used for
' the current message
lblCount.Text = _
txtMessage.Text.Length & "/160"
Test Your App
Deploy your finished application to your Pocket PCs using ActiveSync, then test it before general deployment. First, turn on Discoverable mode on your T68i (or comparable device). Launch Bluetooth Manager, which launches Bluetooth Browser. Your application should detect your Bluetooth-enabled cell phone. Tap the cell phone icon on your Pocket PC. A dialog asks you to select a serial port. Select an available port and tap OK. Some Pocket PC devices don't map all serial ports to the same port number, so check the port number to ensure that your app can connect to the correct port. You can check the Bluetooth serial port mapping on your Pocket PC by invoking its Bluetooth Settings. You should be able to see the port mapping under the Serial Port tab (see Figure 2).
The app prompts your cell phone to accept the connection. It asks you to enter a PIN for authentication on your cell phone, then on your Pocket PC (see Figure 3). The Bluetooth SMS application should launch now. Enter the phone number to which you're sending the SMS message (your own number for the purposes of this test), and type in that message. Then tap on the "Send SMS" button.
Your phone should now receive the message you just sent to yourself. View it on your Pocket PC by tapping on the "Get SMS" button. The message appears in the ListView control. That's it. You can now deploy the app to your users so they can send and receive SMS messages on their Pocket PCs.