我需要能够使用C++ for Windows中的代码配对设备(特别是win7或更新版本).我编写了应该可以工作的代码,但事实并非如此.我可以配对很多设备,Roku,耳机,扬声器等,但由于某种原因,我需要配对的设备无法正常工作.
它总是返回错误代码0x05,根据bthdefs.h定义为BTH_ERROR_AUTHENTICATION_FAILURE.
所以这里奇怪的部分.它永远不会尝试进行身份验证.在调用期间应调用以提供密钥的回调函数不会被调用.我已经确认它会被其他设备(如耳机)调用.
我已经尝试使用BluetoothAuthenticateDeviceEx()而没有回调函数,它应该在Windows中弹出GUI来完成配对.它弹出我的耳机和其他设备,它不会弹出我的设备.
作为旁注,我可以使用Window的蓝牙向导配对设备就好了.它只是拒绝以编程方式工作.
我无法弄清楚我正在使用的winapi代码和windows'向导在配对期间正在做什么之间的区别.
这是我能得到的最简单的测试应用程序.我真正的应用程序使用Qt和mingw来构建.此应用程序使用MSVC 2012和纯Windows代码来从问题中删除任何混淆.我的所有代码都有与错误代码5相同的问题.
#include <windows.h>
#include "bthdef.h"
#include "BluetoothAPIs.h"
#include <tchar.h>
#include <string>
#include <iostream>
#include <vector>
#pragma comment(lib, "bthprops.lib")
using namespace std;
vector<BLUETOOTH_DEVICE_INFO> scanDevices()
{
vector<BLUETOOTH_DEVICE_INFO> res;
BLUETOOTH_DEVICE_SEARCH_PARAMS bdsp;
BLUETOOTH_DEVICE_INFO bdi;
HBLUETOOTH_DEVICE_FIND hbf;
ZeroMemory(&bdsp, sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS));
// set options for how we want to load our list of BT devices
bdsp.dwSize = sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS);
bdsp.fReturnAuthenticated = TRUE;
bdsp.fReturnRemembered = TRUE;
bdsp.fReturnUnknown = TRUE;
bdsp.fReturnConnected = TRUE;
bdsp.fIssueInquiry = TRUE;
bdsp.cTimeoutMultiplier = 4; …Run Code Online (Sandbox Code Playgroud)