Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

A Gadget to Control a Cell Phone

3.48/5 (11 votes)
28 Jan 20075 min read 1   2.1K  
An article on creating a Vista Sidebar Gadget, that communicates/controls a mobile phone through a serial (COM) port.

Table of Contents

Introduction

Since I got my first mobile phone, I was always dreaming about sending my SMS messages from a PC. When I first found a program, which was able to do that, I was the happiest man around the world - no more typing on the phone's little keyboard. And a week ago, I found an article on the Internet about Vista gadgets. After I read the article, an idea came to my mind: 'Hey, maybe I could do something useful. Maybe I could make a Gadget, that is able to send SMSs..' I tried it, and the MobileControl Gadget is the result.

NOTE: I assume, you are at least a little familiar with Vista Gadgets, I won't write about how to create a Gadget, but how I've made "this" Gadget.

NOTE: The main goal was to create an SMS-sender. The call control features are experimental stuff, thus there may be some issues with them. Actually, there is one I know about: On some phones, the voice mail is called after hanging up an incoming call with the Gadget. I don't know the reason, but I hope there is a solution.

Using the Gadget

With this Gadget, you can send messages, initiate/hang up calls; incoming calls are also displayed. But wait a minute - you've not even installed it yet. So, double-click on MobileControl.Gadget file found in MobileControl.zip, and Vista will install the Gadget for you. It will even add it to the Sidebar - go, and remove it. Why? Because you have to register the ATCommandHelper.dll, which is the heart of this Gadget. Open a command prompt from the Gadget's bin directory (%USERPROFILE%\AppData\Local\Microsoft\Windows Sidebar\Gadgets\MobileControl.Gadget\bin), and type the following command: regsvr32 ATCommandHelper.dll. That's all folks, the Gadget is installed!

Add the Gadget to the Sidebar, if you haven't done that yet! Here is a picture of MobileControl in action - you have to see something similar:

MobileControl Gadget in action

OK, MobileControl is running, and you can't wait to try it. First, you need to know the port, your phone is communicating. I'll show you, how to find it out with a Bluetooth connection. Open "Control Panel", and choose "Bluetooth Devices". Select your phone, and click on the "Properties" button. On the "Properties" pane, go to "Services" tab. You will find the needed information there.

Find COM port

Now open the Settings page of the Gadget, and pick the proper port!

Select a port for the Gadget

Well, your Gadget is ready to establish a connection with your phone, so click the "Connect" button! When this button's text changes to "Disconnect", the Gadget is ready to send commands to the phone. There are two text boxes - the little for the phone number, and the bigger for the SMS text. There are some restrictions about the number and the message:

  • Phone number has to be in international format (Country Code + 9 digits).
  • SMS message cannot be longer than 160 characters.
  • SMS message can contain only ASCII characters.

How Does It Work?

Vista Sidebar Gadgets are basically HTML pages, with some JavaScript/VBScript. Well, I am not familiar with either of them. I am more familiar with C#, and of course it is possible to use ActiveX controls on a Gadget. So, the path was in front of me, I just had to follow it - I wrote my 'SMS-sender' as a UserControl DLL in C#, and I embedded it in a simple HTML.

Write an ActiveX Control in C#

This task is quite easy - you just need to create a new Windows Control Library project in Visual Studio. The Control has to have a unique ID, and it has to be COM visible. This can be done with these two lines before the Control class:

C#
[Guid("69950A2F-CE2D-4378-867B-524CA2E320CB")]
[ComVisible(true)]
public partial class ATCommandControl : UserControl
{

A GUID can be generated with Microsoft's GUIDGen program, or on this web page. That's it. Now your Control can be embedded in a Gadget with these lines in the Gadget's HTML's body section:

HTML
<object id="ATCommandHelper"
classid="clsid:69950A2F-CE2D-4378-867B-524CA2E320CB" />

OK, that's it, the Control is embedded in the Gadget. Now you can call every public function of your Control from JavaScript by getting the Control as an object, and simply call the method:

JavaScript
var o = document.getElementById("ATCommandHelper");
o.SetPortName(portName);

AT Commands and PDU Format

AT commands are used to control a phone/modem through a serial connection. These commands are stored as strings, and they're converted to a byte array before we send them to the phone. After the conversation, the byte array is written to the port. The phone receives the command, and executes the proper action. Here is the code that sends the string command to the COM port port. Variable endChar is the 'end command' character - it can be ENTER(13) or CTRL-Z(26).

C#
// Store the AT command in a byte array
byte[] data = new byte[command.Length + 1];
for (int i = 0; i < command.Length; i++)
{
    data[i] = (byte)command[i];
}
data[command.Length] = endChar;

if (port.IsOpen) // If port is open...
    port.Write(data, 0, command.Length + 1); // ...send command.
else // Else: error message.
{
    OnError("Please check, that your phone is correctly connected to the PC, 
                and you've selected the proper COM port!");
}

SMS messages are sent in PDU format, so another conversation is required for them. If you want to know more about PDU format, I recommend this page. The conversion takes place in the functions string SeptetsToOctets() and string ConvertMessageToPDUFormat() in my source code. I assume, this PDU-thing will be all clear after reading the page mentioned above and examining my source code.

A Little Trick with Settings.html

Maybe you have noticed, that instead of a Settings.html in the Gadget's folder, there is a Settings.html.template. Why? And what is that .template file? Well, I've mentioned earlier, that I'm not too familiar with JS, thus I didn't know how to pass a string array from the Control to Settings.html. The array, I wanted to pass, contains the names of the existing COM ports on the system, so I really need it to populate the ports combobox on the Settings.html. So I have written a template Settings.html, with an empty port list. The WriteSettingsHTML() function in the ATCommandHelper's constructor reads that template line by line into a buffer, appends the port name information to it, and writes the buffer to the Settings.html file. Maybe this is not the most elegant solution, but it works.

References

History

  • 2007.01.29: First published

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.