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

如何使用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
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值