December 03, 2009

Handling calls in Windows Mobile

Last two weekends, I ventured into different kind of project. Away from web application frameworks, Java technology.. I entered into Windows mobile application programming.I decided to play with my HTC HD. My first intention was to play around the TouchFlo interface, but my mind caught on the idea of handling calls on mobile. I decided to make an application that will ignore calls from unwanted callers. Or simply hang up calls that I don't want to receive! Here is how it was built.

To start with I created a Visual C#, Smart Device project in Visual Studio. After playing around, I found that I need TAPI API in order to handle the phone calls. For my project, I used OpenNETCF.Telephony library. You will have to download the library source and build it. Also remember to add the dll into our project's Reference.

Well, I wouldn't explain the UI or other functionalities in this article. Let discuss about handling the call.

Monitoring and Getting the call
In order to handle calls, you will have to use the TAPI library. The first thing you will need to do is initialize the Telephony class provided by the OpenNETCF.Telephony library. Our requirement is to monitor and handle the incoming calls and get its information. Upon receiving any incoming call, my callback function is called where I have the necessary logic to hang up. The code below shows how to Initialize Telephony class, get the incoming calls and register the callback function.
Telephony tapi = new Telephony();
CellularLine cl = tapi.CellularLine(MediaMode.InteractiveVoice, CallPrivilege.Monitor | CallPrivilege.Owner);
cl.NewCall += new Line.NewCallHandler(callAction);
The Telephony class is initialized using the Initialize method. The CellularLine method gets the voice calls with monitor and owner privileges and finally, the NewCallHandler register callAction method as the callback function.

Handling the call
The callback function will have to code to handle all the incoming calls. During my venture, I tried different call manipulations. The most simple activity was to reject all the calls that I received. The call rejection was done using the API provided by the library itself. Below is my simple call rejection method:
void callAction(Call call) {            
Well this may not be the case always. My intention was to reject calls from specific numbers. So, I put some conditions into the callAction methods and spiced it up. The phone numbers are read from a simple text file in my mobile phone and loaded into an array when the application starts. When I receive each call, the numbers are checked and actions taken.

Getting call information
I found different ways to get call information. I could use the .Net Compact Framework's SystemState properties or use TAPI library's CallInformation class. In my application I used the SystemState properties as below:
if (checkNumber.CompareTo(SystemState.PhoneIncomingCallerNumber.Trim()) == 0)  {
The PhoneIncomingCallerNumber return empty string when there is no incoming call. When you get an incoming call, the variable gives you the caller's number.

Trouble and Conclusion
I initially planned to release the complete project code along with the article. But, I decided to release it later due to few troubles I have with the application. Even though the application handles the call, I did notice some glitches. My trouble is I am yet to solve those bugs. I observed some strange behaviour and have not got any response to my discussions at the codeplex or stackoverflow websites.

No comments :