Extending the Bluetooth Hands Free Profile

Windows CE supports the Bluetooth Hands Free Profile Audio Gateway (AG).  This profile allows you to use a Bluetooth headset or car kit as a hands free (HF) device.  This feature is supported on Windows CE 5.0 and Windows Mobile 5.0 (and WM2003 SE).  You can find the Hands Free specification at www.bluetooth.org.

 

On top of routing audio to the HF, this profile allows many other functions like answering/ending/placing a call, redial, memory dial, voice dial, sending DTMF tones, etc.  The Windows CE implementation supports all of the mandatory features along with some optional features.  For a list of supported features (AT commands):

 

https://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecomm5/html/wce50conhands-freeprofile.asp

 

Does this mean that your phone can only take advantage of the features Microsoft has implemented?  Of course not!  The Hands Free Profile has an extensibility mechanism to allow OEMs to handle AT commands coming from the HF:

 

https://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecomm5/html/wce50conatcommandextensionmodule.asp

 

Some optional features of the Hands Free Profile require that the AG modify its “capability” bit mask in order to claim support for the feature (see HFP spec for details).  You can override the AG capability to include the new optional feature by using the “capability” registry value:

 

https://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecomm5/html/wce50conaudiogatewayregistrysettings.asp

 

So you can handle new AT commands, but what about controlling the audio connection to the HF?  The AG service (btagsvc.dll) is a module in services.exe.  Applications can send IOControl messages to the service.  The AG supports the following IOControl messages:

 

https://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecomm5/html/wce50conatcommandparser.asp

 

Using IOCTL_AG_OPEN_AUDIO and IOCTL_AG_CLOSE_AUDIO you can allow all system audio to use your BT HF instead of the default system speaker/mic.  Keep in mind that BT audio is routed in hardware between the BT baseband module and cellular modem.  Some phones (e.g. HTC Typhoon) will support routing the audio to the OS to enable system sounds and voice dialing over BT, while other phones will only support cellular audio.

 

Here is a code sample:

 

HANDLE h = CreateFile(L"BAG0:",0,0,NULL,OPEN_EXISTING,0,NULL);
if (INVALID_HANDLE_VALUE == h) {
// TODO: Handle failure case where AG is not present
}

BOOL fStatus = DeviceIoControl(h,IOCTL_AG_OPEN_AUDIO,NULL,0,NULL,0,NULL,NULL);
if (FALSE == fStatus) {
// Failed to open BT audio!
} else {
// Success!
}

CloseHandle(h);

 

[Author: Greg Scott]