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
#include "ui_btcomtest.h"
#include
#include
#include
#include
#include
#include
#include
#pragma comment(lib,"ws2_32.lib")
#include
#include
#include
#pragma comment ( lib, "Irprops.lib")
#include
// 配对时用得PIN码
#define AUTHENTICATION_PASSKEY _T("1234")
//常用操作符
#define LENGTH(x) sizeof(x)/sizeof(x[0])
#include
//#include "mybluetooth.h"
#pragma comment(lib,"Bthprops.lib")
using namespace std;
typedef struct _AUTHENTICATION_CALLBACK_Para
{
LPVOID lpBlueTooth;
HANDLE hRadio;
} t_AUTHENTICATION_CALLBACK_Para;
class btcomTest : public QMainWindow
{
Q_OBJECT
public:
btcomTest(QWidget *parent = 0, Qt::WFlags flags = 0);
~btcomTest();
public slots:
void searchBt();
void connectRemoteDevice();
private:
Ui::btcomTestClass ui;
QList btDeviceList;
HANDLE hbr;
/*static BOOL AUTHENTICATION_CALLBACK (LPVOID pvParam, PBLUETOOTH_DEVICE_INFO pDevice);*/
static BOOL AUTHENTICATION_CALLBACK (PVOID pvParam, PBLUETOOTH_DEVICE_INFO pDevice);
QString getMAC(BLUETOOTH_ADDRESS Daddress);
};
#endif // BTCOMTEST_H
#include "btcomtest.h"//btcomtest.cpp
#include
#include
btcomTest::btcomTest(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
connect(ui.searchBtn,SIGNAL(clicked()),this,SLOT(searchBt()));
connect(ui.clearBtn,SIGNAL(clicked()),ui.btListBox,SLOT(clear()));
connect(ui.connectBtn,SIGNAL(clicked()),this,SLOT(connectRemoteDevice()));
hbr = NULL;
//qDebug()<clear();
btDeviceList.clear();
HBLUETOOTH_RADIO_FIND hbf = NULL;
HBLUETOOTH_DEVICE_FIND hbdf = NULL;
BLUETOOTH_FIND_RADIO_PARAMS btfrp = { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) };
BLUETOOTH_RADIO_INFO bri = { sizeof(BLUETOOTH_RADIO_INFO)};
BLUETOOTH_DEVICE_SEARCH_PARAMS btsp = { sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS) };
BLUETOOTH_DEVICE_INFO btdi = { sizeof(BLUETOOTH_DEVICE_INFO) };
hbf=BluetoothFindFirstRadio(&btfrp, &hbr);
bool brfind = hbf != NULL;
if (brfind&&BluetoothGetRadioInfo(hbr, &bri) == ERROR_SUCCESS)
{
qDebug()<<"LocalName:"<addItem(btInfo);
}
bfind=BluetoothFindNextDevice(hbdf, &btdi);
}
BluetoothFindDeviceClose(hbdf);
}
BluetoothFindRadioClose(hbf);
}
void btcomTest::connectRemoteDevice()
{
BLUETOOTH_DEVICE_INFO btdi=btDeviceList.at(ui.btListBox->currentRow());
qDebug("hbr:%x BtName:",btdi.Address.ullLong);
qDebug()<hRadio=hbr;
//pCallback->lpBlueTooth=NULL;
//HBLUETOOTH_AUTHENTICATION_REGISTRATION phRegHandle;
////BluetoothAuthenticateDevice ( NULL, hbr, &btdi, AUTHENTICATION_PASSKEY, (ULONG)wcslen(AUTHENTICATION_PASSKEY) );
//PFN_AUTHENTICATION_CALLBACK a=(PFN_AUTHENTICATION_CALLBACK)AUTHENTICATION_CALLBACK;
//BluetoothRegisterForAuthentication (&btdi,&phRegHandle,a,hbr);
if (!btdi.fAuthenticated)
{
BluetoothSetServiceState ( hbr, &(btdi), &SerialPortServiceClass_UUID, BLUETOOTH_SERVICE_ENABLE );
BluetoothAuthenticateDevice(this->winId(),hbr,&(btdi),AUTHENTICATION_PASSKEY,4);
BluetoothUpdateDeviceRecord(&(btdi));
bool resultConnect=btdi.fAuthenticated;
while (resultConnect!=true)
{
BluetoothAuthenticateDevice(winId(),hbr,&(btdi),AUTHENTICATION_PASSKEY,4);
BluetoothUpdateDeviceRecord(&(btdi));
resultConnect=btdi.fAuthenticated;
}
}
}
BOOL btcomTest::AUTHENTICATION_CALLBACK( PVOID pvParam, PBLUETOOTH_DEVICE_INFO pDevice )
{
HANDLE mRadio=(HANDLE) pvParam;
if (mRadio)
{
DWORD result= BluetoothUpdateDeviceRecord ( pDevice );
result=ERROR_SUCCESS;
result=BluetoothSendAuthenticationResponse ( mRadio, pDevice, AUTHENTICATION_PASSKEY );
if (result==ERROR_SUCCESS)
{
return TRUE;
}
}
return FALSE;
}
QString btcomTest::getMAC( BLUETOOTH_ADDRESS Daddress )
{
//QString addr= QString::number(Daddress.rgBytes[5],16)+":"+QString::number(Daddress.rgBytes[4],16)+":"+QString::number(Daddress.rgBytes[3],16)+":"+QString::number(Daddress.rgBytes[2],16)+":"+QString::number(Daddress.rgBytes[1],16)+":"+QString::number(Daddress.rgBytes[0],16);
//QString addr=QString("%1:%2:%3:%4:%5:%6").arg(Daddress.rgBytes[5],2,16).arg(Daddress.rgBytes[4],2,16).arg(Daddress.rgBytes[3],2,16).arg(Daddress.rgBytes[2],2,16).arg(Daddress.rgBytes[1],2,16).arg(Daddress.rgBytes[0],2,16);
QString addr;
addr=addr.sprintf("%02x:%02x:%02x:%02x:%02x:%02x",Daddress.rgBytes[5],Daddress.rgBytes[4],Daddress.rgBytes[3],Daddress.rgBytes[2],Daddress.rgBytes[1],Daddress.rgBytes[0]);
return addr;
}