如何使用windows蓝牙api搜索蓝牙设备可参考我的另外一篇文章
Windows枚举搜索远程蓝牙设备。使用如下代码可以借助windows自动安装串口驱动(如果远程蓝牙设备支持串口服务的话)。
BluetoothSetServiceState ( hbr, &btdi, &SerialPortServiceClass_UUID, BLUETOOTH_SERVICE_ENABLE );//打开远程蓝牙设备上的服务以便使用,其中hbr为BluetoothFindFirstRadio或BluetoothFindNextRadio所得的本地蓝牙Radio对应的句柄,btdi为要设置的远程蓝牙设备对应的BLUETOOTH_DEVICE_INFO对象
使用BluetoothAuthenticateDevice来完成自动配对,如下:
BluetoothAuthenticateDevice(phnd,hbr,&(btdi),AUTHENTICATION_PASSKEY,4);//btdi为要配对的远程蓝牙设备的BLUETOOTH_DEVICE_INFO,AUTHENTICATION_PASSKEY为配对使用的配对码,是一个字符串数组的指针,之后的参数是配对码的长度。
整个Qt的代码如下:
#ifndef BTCOMTEST_H//btcomtest.h
#define BTCOMTEST_H
#pragma once
#include <QtGui/QMainWindow>
#include "ui_btcomtest.h"
#include <windows.h>
#include <BluetoothAPIs.h>
#include <conio.h>
#include <iostream>
#include <string>
#include <locale>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
#include <ws2bth.h>
#include <stdio.h>
#include <bthsdpdef.h>
#pragma comment ( lib, "Irprops.lib")
#include <tchar.h>
// 配对时用得PIN码
#define A