The Windows CE Bluetooth stack has the capability to expose a COM port interface to applications.  This allows legacy applications that communicate over serial ports to use the Bluetooth Serial Port Profile.  Common applications of this include GPS and printing.

 

On Windows Mobile 5.0, we now provide user interface to configure BT virtual COM ports.  Devices based on WM2003 might have had UI supplied by the OEM to do this.  A user can now pair with a GPS device or printer, create a BT COM port from the UI, and configure their applicaton to use that port.  In this blog entry I'll talk about how to programmatically set up virtual COM ports.

 

To create a virtual COM port you need to use the RegisterDevice API.  The following code will register the COM port at the specified index to Bluetooth.

 

PORTEMUPortParams pp;
memset(&pp, 0, sizeof(pp));
pp.device = ba;
pp.channel = channel & 0xff;

 

HANDLE h = RegisterDevice(L"COM", index, L"btd.dll", (DWORD)&pp);

 

There are two flavors of BT COM ports: client & server ports.  A server port sets pp.flocal to TRUE and does not specify a BT address in pp.device.  The purpose of server ports is to wait for incoming connections on the specified RFCOMM channel.  The above code creates a client port.  When an application calls CreateFile, the BT stack will connect to the remote device specified in pp.device.  It will disconnect on CloseHandle.

 

A client port can specify pp.uuidService instead of pp.channel.  This will tell the BT stack to find the RFCOMM channel on the remote device based on the SDP service UUID.

 

For more info on BT COM port emulation:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecomm5/html/wce50concomportemulationfacility.asp

 

For more info on the PORTEMUPortParams structure:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecomm5/html/wce50lrfportemuportparams.asp

 

For applications that are aware they are using Bluetooth, it is recommended that developers use winsock APIs.  The purpose of BT virtual COM ports is to enable legacy applications to use Bluetooth.

 

For more info on using BT winsock APIs:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecomm5/html/wce50concreatingconnectionusingwinsockextensions.asp

 

[Author: Greg Scott]