PDA

View Full Version : Connecting to a bluetooth headset programatically


PG2006
15th August 2006, 10:53 PM
Hi,

I hope someone can help me !

I'm trying to programatically make a connection to Bluetooth headset from WM5 device.

Using the GU interface provided on the device, I can
a. discover the headset device, and authenicate
b. Select the headset service, however I'm not certain what is actually
done programatically when perform this selection in the GUI
c. Redirect the audio of the WM5 device to the headset.

This all works fine !

Additionally I seen a lot of programs available to direct the audio of the
device thru the headset, but what I'm missing is the code which
allow me to do things which I currently do via the GUI.

I am now trying to do this all programatically, I've been looking through a
description of the headset profile, it seems to indicate that the AG can
initiate a connection to the HS, the AG is responsible for the creation of :

1. Connection establishment
2. SCO link establishment


In the following code, I think :
BthCreateACLConnection & BthAuthenticate are performing 1
BthCreateSCOConnection is performing 2
CreateFile and DeviceIoControl perform c.

if (ERROR_SUCCESS == BthCreateACLConnection (&b, &h)) {
iRes = BthAuthenticate (&b);
BthCloseConnection (h);
int res = BthCreateSCOConnection (&b, &hSCO);
} else {
iRes = GetLastError ();
MessageBox (hWnd, L"Connection failed", L"Error", MB_OK | MB_TOPMOST);
}

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);

I'm currently attempting to make to SCO connection to the BT_ADDR of the
device, as opposed to the socket of the headset service. I'm not certain if
this is correct or not. I get an error code 1229 which means :

An operation was attempted on a nonexistent network connection.

Am I doing something wrong !

Cheers

Paul Grant