#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
bool Widget::scan()
{
//读取VID&PID
bool isFind = false;
HDEVINFO hDevInfo; //设备句柄
SP_DEVINFO_DATA DeviceInfoData; //设备信息
DWORD i; //设备序号
//定位注册表
const GUID classGuid = {0x36FC9E60,0xC465,0x11CF,{0x80,0x56,0x44,0x45,0x53,0x54,0x00,0x00}};
//取得设备信息集句柄
hDevInfo = SetupDiGetClassDevs(&classGuid, 0, 0, DIGCF_PRESENT);
//失败
if (hDevInfo == INVALID_HANDLE_VALUE)
return false;
//设置存储空间
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
//从设备序号0开始逐一扫描
for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)
{
WCHAR szDescBuf[MAX_PATH] = {0};
DWORD Type;
DWORD buffersize = MAX_PATH - 1;
//获得设备描述(SPDRP_DEVICEDESC),存到szDescBuf
if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC, &Type, (PBYTE)szDescBuf, buffersize, &buffersize))
{
qDebug()<< "获得设备描述失败";
continue;
}
QString strtmp=QString::fromStdWString(szDescBuf);
//查找某设备
if(strtmp.contains("Realtek Bluetooth MP Test"))
{
isFind = true;
qDebug()<<"Find Bluetooth:"<<strtmp;
WCHAR szDescBuf[MAX_PATH] = {0};
DWORD Type;
DWORD buffersize = MAX_PATH - 1;
if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_HARDWAREID, &Type, (PBYTE)szDescBuf, buffersize, &buffersize))
{
qDebug()<< "获得硬件ID失败";
continue;
}
QString str = QString::fromStdWString(szDescBuf);
QString vid = str.mid(str.indexOf("VID_")+4, 4);
QString pid = str.mid(str.indexOf("PID_")+4, 4);
qDebug()<< "VID:"<< vid<< "PID:"<< pid;
//关闭设备信息集句柄
SetupDiDestroyDeviceInfoList(hDevInfo);
QString m_VID = "0BDA";
QString m_PID = "B761";
if((vid != m_VID)||(pid != m_PID))
{
return false;
}
}
}
if(!isFind)
{
qDebug()<< "Cannot Find Bluetooth";
//关闭设备信息集句柄
SetupDiDestroyDeviceInfoList(hDevInfo);
return false;
}
return true;
}