Click here to Skip to main content
14,241,414 members

A Gadget to Control a Cell Phone

Rate this:
3.48 (11 votes)
Please Sign up or sign in to vote.
3.48 (11 votes)
28 Jan 2007
An article on creating a Vista Sidebar Gadget, that communicates/controls a mobile phone through a serial (COM) port.

Table of Contents


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, 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:

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:

<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:

var o = document.getElementById("ATCommandHelper");

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

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



  • 2007.01.29: First published


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.


About the Author

Hungary Hungary
No Biography provided

Comments and Discussions

Generalfile path not found Pin
Member 779020427-Mar-11 13:42
memberMember 779020427-Mar-11 13:42 
GeneralEntry Point not Found! [modified] Pin
Vercas17-Sep-10 5:23
memberVercas17-Sep-10 5:23 
GeneralSMS not reaching Pin
ahsal17-Aug-08 3:52
memberahsal17-Aug-08 3:52 
Generalcant use this Pin
mike ko4-Dec-07 16:51
membermike ko4-Dec-07 16:51 
GeneralRe: cant use this Pin
Sheila5-Dec-07 7:25
memberSheila5-Dec-07 7:25 
GeneralRe: cant use this Pin
dee2310-Jun-08 21:19
memberdee2310-Jun-08 21:19 
GeneralGreat Gadget but I can't use it Pin
manfredip28-Jun-07 12:53
membermanfredip28-Jun-07 12:53 
GeneralCool gadget Pin
marcasselin1-May-07 2:38
membermarcasselin1-May-07 2:38 
GeneralRe: Cool gadget Pin
sghctoma5-May-07 14:14
membersghctoma5-May-07 14:14 
QuestionHow about MMS Pin
fajamet12-Feb-07 6:07
memberfajamet12-Feb-07 6:07 
QuestionQuick Question.. Pin
ctpaquette5-Feb-07 11:09
memberctpaquette5-Feb-07 11:09 
AnswerRe: Quick Question.. Pin
sghctoma6-Feb-07 9:58
membersghctoma6-Feb-07 9:58 
Danwave1-Feb-07 4:50
memberDanwave1-Feb-07 4:50 
GeneralRe: UNABLE TO REGISTER Pin 1:19 1:19 
sergio agnelo23-Aug-10 3:58
membersergio agnelo23-Aug-10 3:58 
Generaltemplate file Pin
Dragos Sbirlea28-Jan-07 23:39
memberDragos Sbirlea28-Jan-07 23:39 
GeneralRe: template file Pin
sghctoma29-Jan-07 1:02
membersghctoma29-Jan-07 1:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 28 Jan 2007


70 bookmarked