如何使用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_OBJECTpublic: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;
}

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

  1. 检查计算机无法更新,Win7提示Windows Update当前无法检查更新,因为未运行服务解决方法...

    在使用Win7的过程中,有的时候我们需要更新至最新版本的操作系统,但是在Windows Update的检查更新的时候,却提示了"Windows Update当前无法检查更新,因为未运行服务. ...

  2. w ndows无法连接到System,Windows无法连接到System Event Notification Service服务解决方法...

    采用windows7操作系统的电脑在开机时提示"Windows 无法连接到 System Event Notification Service 服务"(如下图)的解决方法: 操作系 ...

  3. Windows蓝牙通信的开发

    周四接到关于window上的蓝牙开发项目,预定时间在五天之内结束,但是五天的时间很快过去,还是没有做出来,只能搜到蓝牙设备,并且可以本地的蓝牙设备和远程的蓝牙设备,所以现在还在持续的更新中,所以用博客 ...

  4. Android实现蓝牙的搜索,配对(不需要输入PIN,自动匹配),连接,通信

    目录 目录 一.蓝牙设置权限 二.蓝牙搜索 1.首先需要获得蓝牙适配器. 2.判断蓝牙是否打开,然后请求打开蓝牙 3.搜索蓝牙 三.蓝牙配对 1.动态注册广播接收器 2.判断蓝牙设备是否为目标设备,并 ...

  5. 服务器系统安装蓝牙驱动,安装蓝牙设备 - Windows drivers | Microsoft Docs

    安装蓝牙设备 05/29/2020 本文内容 重要 本主题面向程序员. 如果你是遇到蓝牙设备安装问题的客户,请参阅 在 Windows 中配对蓝牙设备 蓝牙配置文件驱动程序有两种安装类型: 远程设备的 ...

  6. 基于Qt的Windows平台桌面蓝牙调试器软件

    基于Qt的Windows平台桌面蓝牙调试器软件 在进行嵌入式的调试时,我们经常需要下位机给主机打印数据来观察数据变化.绘制波形等进行调试.一般的做法可以是直接通过串口与PC进行数据通信(USB to ...

  7. html5获取蓝牙设备列表,【报Bug】官方H5+示例中 搜索蓝牙设备,部分机型搜索不到任何蓝牙...

    详细问题描述 (DCloud产品不会有明显的bug,所以你遇到的问题大都是在特定环境下才能重现的问题,请仔细描述你的环境和重现方式,否则DCloud很难排查解决你的问题) [内容]我测试了H5+中的蓝 ...

  8. 蓝牙硬件设备没有链接到计算机,电脑连接蓝牙却搜索不到蓝牙设备怎么办

    电脑想要连接蓝牙却搜索不到蓝牙设备应该如何解决?一般来说,只要是Win10系统都是自带蓝牙设备的,然而最近有用户表示想要连接蓝牙却搜索不到蓝牙设备,那么这是怎么回事呢?这很有可能是因为蓝牙驱动问题或者 ...

  9. Qt windows端的蓝牙串口服务

    Qt windows端的蓝牙串口服务 环境 系统 Qt 蓝牙模块 使用步骤 蓝牙模块参数获取 配对 扫描.连接.数据收发 扫描 连接 数据收发 环境 系统 只测试过自己电脑,系统版本如下: 查看方式按 ...

  10. mac搜索不到共享计算机,mac蓝牙搜索找不到设备怎么处理 Mac蓝牙设备连不上的解决方法有哪些...

    今天给大家带来mac蓝牙搜索找不到设备怎么处理,Mac蓝牙设备连不上的解决方法有哪些,让您轻松解决问题. mac蓝牙搜索找不到设备怎么办 Mac蓝牙设备连不上的原因解决方法.我们现在有很多人都在使用苹 ...

最新文章

  1. Linux内核自旋锁使用笔记
  2. 数据框筛选特定的子集
  3. Windows消息和事件的区别 VC++解惑
  4. iOS7初体验(2)——单元测试
  5. Facebook黄毅博士:像加工艺术品一样构建技术产品
  6. input hidden的值存储在哪儿_kafka内核:消息存储模块的工作机制
  7. Ecshop源码阅读
  8. meta分析一般步骤
  9. 华为交换机做qos案例_华为交换机QoS在企业网络中的应用
  10. 华为ensp路由器静态路由配置
  11. 解决ES只能查询10000条数据的问题
  12. “企鹅号+时尚集团MCN”强强联手 打造19春夏时装周报道新模式
  13. Unity中ComputeShader入门
  14. 关于order by的一些用法
  15. CSS的鼠标手势实现
  16. 8.17vue项目搜素框的实现
  17. Bootstrap3黑色系个人网站单页
  18. C语言_if_查询分数等级
  19. linux锁机制:queued spinlock
  20. 护士排班问题matlab,护士排班,该听谁的?聪明的护士长都这样做!

热门文章

  1. 用AI对抗AI!教代码调戏深度学习算法生成的假视频
  2. 刀口法测mtf_基于特征像分析的光学镜头MTF测试方法的研究
  3. 微信小程序清除Webview缓存
  4. 化工机械基础期末复习题及答案
  5. “_CRT_SECURE_NO_DEPRECATE”: 未定义宏或在预编译头使用后定义发生改变
  6. 软件测试零基础如何快速入门 ?这里有全网最详细的学习资料
  7. 用代码关闭冰刃(IceSword)
  8. VUE filters 使用
  9. 从内观修行的角度看正念疗法
  10. 1867. 最大数量高于平均水平的订单