Click here to Skip to main content
12,698,600 members (27,266 online)
Click here to Skip to main content
Add your own
alternative version

Stats

82.5K views
2K downloads
70 bookmarked
Posted

A Gadget to control a cell phone

, 28 Jan 2007
Rate this:
Please Sign up or sign in to vote.
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 SMS's..' 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:

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

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

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

  • First published: 2007.01.29.

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

Share

About the Author

sghctoma
Hungary Hungary
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Generalfile path not found Pin
Member 779020427-Mar-11 14:42
memberMember 779020427-Mar-11 14:42 
GeneralEntry Point not Found! [modified] Pin
Vercas17-Sep-10 6:23
memberVercas17-Sep-10 6:23 
Generalcant use this Pin
mike ko4-Dec-07 17:51
membermike ko4-Dec-07 17:51 
GeneralRe: cant use this Pin
Sheila5-Dec-07 8:25
memberSheila5-Dec-07 8:25 
GeneralRe: cant use this Pin
dee2310-Jun-08 22:19
memberdee2310-Jun-08 22:19 
GeneralGreat Gadget but I can't use it Pin
manfredip28-Jun-07 13:53
membermanfredip28-Jun-07 13:53 
GeneralCool gadget Pin
marcasselin1-May-07 3:38
membermarcasselin1-May-07 3:38 
GeneralRe: Cool gadget Pin
sghctoma5-May-07 15:14
membersghctoma5-May-07 15:14 
QuestionHow about MMS Pin
fajamet12-Feb-07 7:07
memberfajamet12-Feb-07 7:07 
QuestionQuick Question.. Pin
ctpaquette5-Feb-07 12:09
memberctpaquette5-Feb-07 12:09 
AnswerRe: Quick Question.. Pin
sghctoma6-Feb-07 10:58
membersghctoma6-Feb-07 10:58 
GeneralUNABLE TO REGISTER Pin
Danwave1-Feb-07 5:50
memberDanwave1-Feb-07 5:50 
GeneralRe: UNABLE TO REGISTER Pin
davidcbond@hotmail.co.uk4-Apr-07 2:19
memberdavidcbond@hotmail.co.uk4-Apr-07 2:19 
GeneralRe: UNABLE TO REGISTER Pin
sergio agnelo23-Aug-10 4:58
membersergio agnelo23-Aug-10 4:58 
Generaltemplate file Pin
Dragos Dumitru Sbarlea29-Jan-07 0:39
memberDragos Dumitru Sbarlea29-Jan-07 0:39 
GeneralRe: template file Pin
sghctoma29-Jan-07 2:02
membersghctoma29-Jan-07 2: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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170118.1 | Last Updated 28 Jan 2007
Article Copyright 2007 by sghctoma
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid