以编程方式将蓝牙设备配对为音频设备

时间:2019-02-07 12:17:47

标签: c++ windows winapi bluetooth

我正在开发带有Windows 10的业务应用程序,该应用程序应自动连接到给定的蓝牙扬声器/耳机(Jabra FREEWAY v5.7.0)。 到目前为止,使用BluetoothAPIs.h效果很好。问题是,它没有在Windows下显示为音频设备。我无法在其上播放测试声音(或任何其他声音)。 如何将设备与音频设备配对?还是有后续步骤将设备设置为音频?

对于移动打印机,我们具有类似的代码功能,似乎不需要在那里执行额外的步骤。 连接具有给定Windows功能的设备(Jabra)可以将其正确显示为音频设备。

BOOL CALLBACK BTHeadsetAuthCallbackEx(__in_opt LPVOID /*pvParam*/, __in PBLUETOOTH_AUTHENTICATION_CALLBACK_PARAMS pAuthCallbackParams)
{
  BLUETOOTH_AUTHENTICATE_RESPONSE resp;
  resp.bthAddressRemote = pAuthCallbackParams->deviceInfo.Address;
  resp.authMethod = pAuthCallbackParams->authenticationMethod;
  resp.negativeResponse = FALSE;
  resp.passkeyInfo.passkey = pAuthCallbackParams->Passkey;
  DWORD ret = BluetoothSendAuthenticationResponseEx( NULL, &resp );
  if( ret != ERROR_SUCCESS )
  {
    logError( "BluetoothSendAuthenticationResponseEx failed with %u", ret );
    return FALSE;
  }

  return TRUE;
}


BLUETOOTH_DEVICE_INFO deviceInfo = { sizeof(BLUETOOTH_DEVICE_INFO) };
deviceInfo.Address.ullLong = device->getAddress();

HBLUETOOTH_AUTHENTICATION_REGISTRATION regHandle;
DWORD err = BluetoothRegisterForAuthenticationEx( &deviceInfo, &regHandle, (PFN_AUTHENTICATION_CALLBACK_EX)&BTHeadsetAuthCallbackEx, NULL );
if( err != ERROR_SUCCESS )
{
  logError( "BluetoothRegisterForAuthenticationEx failed with %u", err );
  return false;
}

err = BluetoothAuthenticateDevice( NULL, NULL, &deviceInfo, L"0000", 4 );
if( err != ERROR_SUCCESS  )
{
  BluetoothUnregisterAuthentication( regHandle );
  logError( "BluetoothAuthenticateDevice failed with %u", err );
  return false;
}

BluetoothUnregisterAuthentication( regHandle );

1 个答案:

答案 0 :(得分:0)

正如Mike Petrichenko所指出的,我需要使用正确的服务GUID(在我的情况下为免提设备)调用BluetoothSetServiceState(),在这里找到:https://docs.microsoft.com/en-us/windows/client-management/mdm/policy-csp-bluetooth

完整代码:

BOOL CALLBACK BTHeadsetAuthCallbackEx(__in_opt LPVOID /*pvParam*/, __in PBLUETOOTH_AUTHENTICATION_CALLBACK_PARAMS pAuthCallbackParams)
{
  BLUETOOTH_AUTHENTICATE_RESPONSE resp;
  resp.bthAddressRemote = pAuthCallbackParams->deviceInfo.Address;
  resp.authMethod = pAuthCallbackParams->authenticationMethod;
  resp.negativeResponse = FALSE;
  resp.passkeyInfo.passkey = pAuthCallbackParams->Passkey;
  DWORD ret = BluetoothSendAuthenticationResponseEx( NULL, &resp );
  if( ret != ERROR_SUCCESS )
  {
    logError( "BluetoothSendAuthenticationResponseEx failed with %u", ret );
    return FALSE;
  }

  return TRUE;
}

BLUETOOTH_DEVICE_INFO deviceInfo = { sizeof(BLUETOOTH_DEVICE_INFO) };
deviceInfo.Address.ullLong = device->getAddress();

HBLUETOOTH_AUTHENTICATION_REGISTRATION regHandle;
DWORD err = BluetoothRegisterForAuthenticationEx( &deviceInfo, &regHandle, (PFN_AUTHENTICATION_CALLBACK_EX)&BTHeadsetAuthCallbackEx, NULL );
if( err != ERROR_SUCCESS )
{
  logError( "BluetoothRegisterForAuthenticationEx failed with %u", err );
  return false;
}

err = BluetoothAuthenticateDevice( NULL, NULL, &deviceInfo, L"0000", 4 );
if( err != ERROR_SUCCESS  )
{
  BluetoothUnregisterAuthentication( regHandle );
  logError( "BluetoothAuthenticateDevice failed with %u", err );
  return false;
}

BluetoothUnregisterAuthentication( regHandle );

HANDLE btHeadset;
BLUETOOTH_FIND_RADIO_PARAMS rfind = { sizeof( rfind ) };
BluetoothFindFirstRadio( &rfind, &btHeadset );
GUID id = { 0x0000111e, 0x0000, 0x1000, { 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb} }; 
err = BluetoothSetServiceState( &btHeadset, &deviceInfo, &id, BLUETOOTH_SERVICE_ENABLE );
if( err != ERROR_SUCCESS  )
{
  logError( "BluetoothSetServiceState failed with %u", err );
}

logNorm( "BluetoothSetServiceState successfull" );

注意:这并不是完全愚蠢的证明。我正在使用搜索参数为空的第一个设备。在那里将进行一些改进。