Creating a custom Phone App on Windows Mobile (Part 1) - Available APIs (
Page 2 of 5 )
Available APIs
For starters, Microsoft has given us a wrapper that takes care of some of the dirty work for adding call-capabilities to any application. This class is called Phone, and it lives in the Microsoft.WindowsMobile.Telephony namespace. (Phone is the only class in that namespace.) Let’s first create an application for that.
ADVERTISEMENT
Start out by creating a new project of type Smart Device (in the C# group), as shown in the figure below.
When the wizard starts, choose Device Application for your platform, as shown here:
When the project is created, you’ll get a nice-looking form that resembles a mobile phone:
This is where you drag and drop your controls, just like a Windows Forms application. (In fact, it is a Windows Forms application; it just runs on Windows Mobile instead of the desktop.)
In order to use the Telephony API (called TAPI), you need to add an assembly. Right-click on the project name in the Solution Explorer and click Add Reference. In the Add Reference dialog box, add Microsoft.Windows.Telephony. But not that when I did this, there were two Microsoft.WindowsMobile.Telphony entries, so I had to scroll to the right to see which was which. The filename path showed me that the first goes with Windows Mobile 6.0, so that’s the one I used. (If you’re using 5.0, you’ll need to choose the appropriate one.) The dialog box is shown here:
After adding the reference, return to the form, and go ahead and drop a single button on the form. Double-click the button to get a button handler. But don’t add any code inside the handler yet. First, go to the top and add the following line after the existing using statements:
using Microsoft.WindowsMobile.Telephony;
This adds the Telephony namespace, which, as I mentioned, includes only one class, Phone.
The Phone class itself is tiny; besides a few members inherited from Object (the usuals, such as GetType), it only has a single member, the Talk function.
Now let’s add the handler code. Here’s the entire program; add the code shown inside the button1_Click function:
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.Telephony;
namespace MobilePhone
{
public partial class Form1 : Form
{
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
Phone p = new Phone();
p.Talk("2125551212", true);
}
}
}
(The phone number I’m using here in this sample is Information for New York City. Please change it to some number you’re going to call, such as your own land line, so I don’t get blamed when several thousand people all call Information this week over and over, hanging up each time!)
Now let’s try it out. Click Debug->Start Without Debugging, which opens the Deploy dialog box:
I chose Windows Mobile 6 Professional Emulator so I could just test it out in the emulator rather than the real device. (I’ll do the real device next.) By the way, don’t uncheck that box in the bottom about showing the dialog everytime you deploy. If you do, it’ll keep your setting and it’s a pain to get it to come back when you want to deploy to the actual device!)
It takes awhile to deploy the application to the emulator. (It’ll be faster when you deploy to the actual device.) The emulator will start up and run, but please wait a moment before you start clicking on things. Watch the status bar at the bottom of Visual Studio’s window to note the progress of deployment. Eventually, the application will start up on the device emulator. (Be patient!) You don’t have to start it manually. Then you’ll see something like this:
Click the button, and you’ll see a message:
Since this is an emulator without a phone, you can’t actually dial. If you click “yes” you’ll see a message, “A wireless radio must be attached to use the phone.” But at least we know it’s working so far.
Testing it out for real
Since I can’t test the phone capabilities from the emulator, I have to go through the code carefully before trying it out. The last thing I need is to have it mess up and call 911, or dial some other wrong number and end up waking some elderly person (with caller ID, no less) in the area!
Close the emulator and return to Visual Studio. Make sure your phone is connected through ActiveSync (either via bluetooth—my personal favorite—or via cable). (Don’t let the phone go to sleep or it sometimes can disconnect. That only happened once to me, but it was annoying when it did. So tap the screen if you have to to keep it awake.) Then, the fun begins. You can actually debug through Visual Studio, setting breakpoints and what-not. We won’t debug here in this article, but I encourage you to try it out. In Visual Studio, again click Debug->Start Without Debugging. But this time when the Deploy dialog box starts up, click Windows Mobile 6 Professional Device (or whichever is appropriate for your particular device). Don’t choose the emulator; you want the actual device.
Once deployed, you’ll see a message on the device that the program is from an unknown publisher. Click Yes to proceed. And then your program will start up, looking just like it did in the emulator.
But you might be a bit surprised when you see what happens after you click the button and choose Yes: The same old phone application starts up. This particular class doesn’t have any low-level phone capabilities. But it is useful if you want to add dialing capabilities to one of your existing programs. So far so good.
But what if you want full phone capabilities within your app itself? Let’s explore the next API a bit.