Qt使用Windows蓝牙API搜索蓝牙设备并建立串口服务的方法

29 篇文章 2 订阅
13 篇文章 0 订阅
如何使用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 AUTHENTICATION_PASSKEY	_T("1234")
//常用操作符
#define LENGTH(x) sizeof(x)/sizeof(x[0])


#include <QList>

//#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<BLUETOOTH_DEVICE_INFO> 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 <QString>
#include <QDebug>

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()<<QString::number(171,16);
	
}

btcomTest::~btcomTest()
{
	if (hbr)
	{
		CloseHandle(hbr);
		hbr=NULL;
	}
}

void btcomTest::searchBt()
{
	ui.btListBox->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:"<<bri.szName;
		btsp.hRadio = hbr;  
		btsp.fReturnAuthenticated = FALSE;		
		btsp.fReturnConnected = FALSE;  
		btsp.fReturnRemembered = FALSE;  
		btsp.fReturnUnknown = TRUE;  
		btsp.fIssueInquiry=TRUE;
		btsp.cTimeoutMultiplier = 5;  
		hbdf=BluetoothFindFirstDevice(&btsp, &btdi);  
		bool bfind = hbdf != NULL;  
		while (bfind)  
		{  
			QString btInfo=QString::fromWCharArray(btdi.szName)+"  "+getMAC(btdi.Address);
			//QString btInfo=QString::fromWCharArray(btdi.szName)+":"+QString::number(btdi.Address.ullLong,16);
			qDebug()<<btInfo;
			if (btInfo.contains("Alpha"))
			{
				btDeviceList.append(btdi);
				ui.btListBox->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()<<QString::fromWCharArray(btdi.szName);
	BluetoothSetServiceState ( hbr, &btdi, &SerialPortServiceClass_UUID, BLUETOOTH_SERVICE_ENABLE );

	//t_AUTHENTICATION_CALLBACK_Para *pCallback=new t_AUTHENTICATION_CALLBACK_Para;
	//pCallback->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;
}





  • 12
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 19
    评论
### 回答1: Windows 10操作系统可以通过内置的蓝牙功能来扫描蓝牙设备。用户可以在设备设置中打开蓝牙功能,并使用系统提供的蓝牙设备管理界面来搜索并连接可用的蓝牙设备。可以通过简单的操作步骤来扫描和连接蓝牙鼠标、键盘、耳机、手环等设备。 而Qt是一种跨平台的应用程序开发框架,它提供了一系列功能强大的工具和库,用于开发各种类型的应用程序。尽管Qt可以在多个平台上开发应用程序,但它并不像Windows 10操作系统那样直接支持蓝牙设备的扫描。 不过,Qt提供了第三方库和插件来支持蓝牙功能。通过使用这些库和插件,开发者可以在Qt应用程序中实现蓝牙设备的扫描和连接。开发者可以根据特定的需求选择适合的第三方插件或库,来实现在Qt应用程序中与蓝牙设备进行交互的功能。 总结来说,Windows 10操作系统具备内置的蓝牙功能,可以直接扫描蓝牙设备。而在Qt应用程序中,虽然不自带蓝牙功能,但可以通过使用第三方库和插件来实现与蓝牙设备的交互。 ### 回答2: Windows 10操作系统内置了蓝牙设备扫描功能,用户可以通过系统设置中的蓝牙设置选项进行蓝牙设备的扫描。当用户打开蓝牙功能后,系统会搜索周围的蓝牙设备,并列出可用的设备供用户连接。用户可以轻松地浏览和连接到他们想要的设备,例如蓝牙耳机、键盘、鼠标或其他外围设备。 然而,Qt是一个跨平台的应用程序开发框架,它本身并没有直接提供蓝牙设备扫描的功能。这意味着使用Qt进行应用程序开发时,开发者需要使用额外的第三方库或自行实现蓝牙设备扫描的功能。通过使用第三方蓝牙库,开发者可以在Qt应用程序中实现蓝牙设备扫描功能,以便用户能够搜索和连接蓝牙设备。 尽管Qt本身没有内置蓝牙设备扫描功能,但通过使用适当的第三方库和API,开发者可以在Qt应用程序中实现强大的蓝牙特性。这些库和API提供了与蓝牙硬件通信的功能,开发者可以利用这些功能实现诸如设备发现、连接、数据传输等蓝牙相关的操作。 综上所述,虽然Windows 10操作系统可以直接进行蓝牙设备扫描,但在Qt应用程序中需要额外的配置和编码才能实现相同的功能。开发者可以利用第三方库和API来实现蓝牙设备的扫描和连接,从而在Qt应用程序中实现与蓝牙设备的交互。 ### 回答3: Windows10操作系统具备蓝牙设备扫描功能,但Qt编程语言的库并没有直接提供蓝牙设备的扫描功能。在Windows10中,用户可以通过系统设置或任务栏中的蓝牙图标进入蓝牙设备管理界面,从而扫描并连接附近的蓝牙设备。 相比之下,Qt并没有内置的蓝牙扫描API,但开发者可以使用第三方的库或扩展来实现类似的功能。例如,QtBluetooth模块提供了基于蓝牙的通信功能,但并不支持扫描功能。因此,如果想要在Qt应用程序中实现蓝牙设备的扫描功能,开发者需要使用额外的第三方库,如BlueZ、WinRT或ioBlueberry等,来实现这个功能。 总之,尽管Windows10操作系统可以提供蓝牙设备的扫描功能,但Qt本身并没有提供直接的扫描API。开发者可以选择使用第三方库来在Qt应用程序中实现蓝牙设备的扫描功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值