Showing posts with label WindowsMobile-C#. Show all posts
Showing posts with label WindowsMobile-C#. Show all posts

How to manage incoming call with OpenNetCf TAPI (Windows Mobile C

How to manage incoming call with OpenNetCf TAPI (Windows Mobile C#)
G.Morreale

Introduction

If you want to manage an incoming call you can use the windows mobile status api.
By this managed api you can get the status of many system properties: http://msdn.microsoft.com/en-us/library/microsoft.windowsmobile.status.systemproperty.aspx

But if you more than get the status by using an event handler, you must use the tapi mobile version, like the previous article I find more useful the opennetcf tapi wrapper: http://tapi.codeplex.com

Now I try to describe the way for get a call instance of an incoming call.

The example

Make a smart device c# project with a Form.

The first step like make a call as we see in the previous article is to initialize the tapi api.

        public Form1()
        {
            InitializeComponent();
            this.InitializeTelephony();
        }

        private void InitializeTelephony()
        {

            m_telephony = new Telephony();

            //Initialize the telephony class
            m_telephony.Initialize();

            //Get the cellular line with Monitor and Owner privileges
            m_line = m_telephony.CellularLine(MediaMode.InteractiveVoice, CallPrivilege.Monitor | CallPrivilege.Owner);

            //Add a new call event
            m_line.NewCall += new Line.NewCallHandler(m_line_NewCall);
        }

As you see in the code the call handler delegates the new call event to m_line_NewCall method


        /// <summary>
        /// Handles any new calls coming in/out of the phone the 
        /// </summary>
        /// <param name="call"></param>
        void m_line_NewCall(Call call)
        {
            lineSetCallPrivilege(call.Handle, CallPrivilege.Owner);
        }

        
In order to take the right privilege on the phone line you must use this coredll method:
        
        [DllImport("coredll")]
        extern static public int lineSetCallPrivilege(IntPtr hCall, CallPrivilege dwPrivilege);

 
so for example if you want to hang up all the incoming call you have to add only this line of code

        void m_line_NewCall(Call call)
        {
            lineSetCallPrivilege(call.Handle, CallPrivilege.Owner);
            call.HangUp();
        }

Also you can add a call info handler in order to monitor the call infos, for example:


        void m_line_NewCall(Call call)
        {
            lineSetCallPrivilege(call.Handle, CallPrivilege.Owner);
            call.CallInfo += new Call.CallInfoHandler(call_CallInfo);
        }


        string m_calledNumber = null;

        void call_CallInfo(Call call, CallInfoState infoState, CallInformation info)
        {
            Debug.println(info.CalledID); //print the caller id
        }

And Don't forget to shutdown the telephony instance!

        private void Form1_Closing(object sender, CancelEventArgs e)
        {
            m_telephony.Shutdown();
        }


How to make asynchrounous call with OpenNetCf TAPI (Windows Mobil

How to make asynchronous call with OpenNetCf TAPI (Windows Mobile C#)
G.Morreale

Introduction

If you want to make a synchrounous call you can read the previous article on the blog.
But if you want to make an asynchronous call you must write the following lines of code


The example

Ok, instead of calling:

call = line.MakeCall(tbNumber.Text, 39, false);

You must use

line.BeginMakeCall(tbNumber.Text, 39, null, new AsyncCallback(OnCallMade), this);

In this way the method don't block the execution and the OnCallMade method is called when the call is made!
So, implement the callback method OnCallMade

     private void OnCallMade(IAsyncResult ar)
        {
    
  //with this line of code we can bring up the Call instance in order to hang up it or do something else
            call = line.EndMakeCall(ar);
        }


How to make a call with OpenNetCf TAPI (Windows Mobile)

How to make a call with OpenNetCf TAPI (Windows Mobile C#)
G.Morreale

Introduction

The simplest way to make a call in windows mobile is by using the following

http://msdn.microsoft.com/en-us/library/microsoft.windowsmobile.telephony.phone.talk.aspx

 

that is the WindowsMobile Managed Telephony api.


Microsoft.WindowsMobile.Telephony.Phone myPhone = new Microsoft.WindowsMobile.Telephony.Phone();

myPhone.Talk("0123456789", true);


In this simple way the default phone manager appear and the call is managed by it.

Tapi

If you want a more flexible way to manage the phone module in your windows mobile device you have to use the TAPI
The TAPI is an application interface for telephony application, and microsoft make available it also for windows mobile(http://msdn.microsoft.com/en-us/library/aa455190.aspx).


Tapi Wrapper

Ok, now if you want to simplify the interaction with the unmanaged tapi library, you must use a wrapper.
A nice wrapper that I can find is an OpenNetCf Wrapper.
You can download it and various example from http://tapi.codeplex.com/

The example

Make a new smart device project, and add OpenNETCF.Telephony.dll in the references.

You can build the source code and obtaion the dll by the zip downloaded or find it in extracting TelephonySamples.zip file(makeCall ->  bin -> Debug -> OpenNETCF.Telephony.dll)รน

Now, we insert the tapi init code in the load event of the Form1.

using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenNETCF.Telephony;

namespace MakeCall2
{
    public partial class Form1 : Form
    {

        private Telephony tapi;
        private Line line;
       
        public Form1()
        {
            InitializeComponent();
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            // Open Tapi
            tapi = new Telephony();
            tapi.Initialize();      
            // Get the phone line in the specified media mode and with or not call privilege
           line = tapi.CellularLine(MediaMode.InteractiveVoice, CallPrivilege.None);
        }
    }
}
So now, add a textbox to the form for telephone number input and a button in order to permits the call.
In the click event of the call you have to add the following:

        private void btMakeCall_Click(object sender, EventArgs e)
        {
            //the tbNumber is the textbox, and 39 is the country code of the called number, the third 
            //parameter is for suppressing or not the caller id
            line.MakeCall(tbNumber.Text, 39, false);
        }

remember to shutdown the tapi instance.

     private void Form1_Closing(object sender, CancelEventArgs e)
        {
            tapi.Shutdown();
        }

Possible Issues

When using tapi.CellularLine method the line returned can be null inspite the line exist and is activable.
I debug the OpenNETCF.Telephony and the problem is on 
"if (dc.ProviderName == NativeMethods.CELLTSP_PROVIDERINFO_STRING)"

When using the .net compact framework 3.5 the dc.ProviderName string is suffixed with a different char.
Now I don't know the cause of this, but we can avoid this problem by using this matching:
"if (dc.ProviderName.contains(NativeMethods.CELLTSP_PROVIDERINFO_STRING))"