电脑蓝牙搜索并配对/连接蓝牙设备

本文介绍了一种使用C++实现的蓝牙设备搜索与配对的方法。通过调用微软提供的蓝牙API,实现了对已知和未知蓝牙设备的扫描,并完成了指定设备的配对过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从来没有搞过蓝牙只能网上找,搞了两天总算是找到了点门路,忘记借鉴的谁的代码,修改了一下终于得到了需要的功能~直接上代码(C++)

#include <windows.h>
#include "bthdef.h"
#include "BluetoothAPIs.h"    //微软蓝牙接口
#include <tchar.h>
#include <string>
#include <iostream>
#include <vector>

#pragma comment(lib, "bthprops.lib")

using namespace std;

vector<BLUETOOTH_DEVICE_INFO> scanDevices()
{
	vector<BLUETOOTH_DEVICE_INFO> res;

	BLUETOOTH_DEVICE_SEARCH_PARAMS bdsp;
	BLUETOOTH_DEVICE_INFO bdi;
	HBLUETOOTH_DEVICE_FIND hbf;

	ZeroMemory(&bdsp, sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS));

	// set options for how we want to load our list of BT devices
	bdsp.dwSize = sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS);
	bdsp.fReturnAuthenticated = TRUE;
	bdsp.fReturnRemembered = TRUE;
	bdsp.fReturnUnknown = TRUE;
	bdsp.fReturnConnected = TRUE;
	bdsp.fIssueInquiry = TRUE;
	bdsp.cTimeoutMultiplier = 4;
	bdsp.hRadio = NULL;

	bdi.dwSize = sizeof(bdi);

	// enumerate our bluetooth devices
	hbf = BluetoothFindFirstDevice(&bdsp, &bdi);
	if (hbf)
	{
		do
		{
			res.push_back(bdi);
		} while (BluetoothFindNextDevice(hbf, &bdi));

		// close our device enumerator
		BluetoothFindDeviceClose(hbf);
	}

	return res;
}

void pairDevice(BLUETOOTH_DEVICE_INFO device)
{
	wstring ws = device.szName;
	cout << "Pairing device " << string(ws.begin(), ws.end()) << endl;

	HBLUETOOTH_AUTHENTICATION_REGISTRATION hCallbackHandle = 0;
	DWORD result;  
	result = BluetoothAuthenticateDevice(NULL, NULL, &device, L"8888", 4);  //配对函数,8888是我的蓝牙配对码
	if (result != ERROR_SUCCESS)
	{
		cout << "Failed to register callback" << endl;
		return;
	}
	
	switch (result)
	{
	case ERROR_SUCCESS:
		cout << "pair device success" << endl;
		break;

	case ERROR_CANCELLED:
		cout << "pair device failed, user cancelled" << endl;
		break;

	case ERROR_INVALID_PARAMETER:
		cout << "pair device failed, invalid parameter" << endl;
		break;

	case ERROR_NO_MORE_ITEMS:
		cout << "pair device failed, device appears paired already" << endl;
		break;

	default:
		cout << "pair device failed, unknown error, code " << (unsigned int)result << endl;
		break;
	}
}

int _tmain(int argc, _TCHAR *argv[])
{
	cout << "Scanning bluetooth devices..." << endl;
	cout.flush();

	// scan devices
	vector<BLUETOOTH_DEVICE_INFO> devices = scanDevices();
	cout << "Got " << devices.size() << " devices" << endl;
	
	// list all devices
	int pdIndex = -1;
	int foundDev = -1;
	vector<BLUETOOTH_DEVICE_INFO>::const_iterator devci;
	BLUETOOTH_ADDRESS_STRUCT addr,strr;
	strr.ullLong =159652116588973;  //这是我要连接的设备地址
	for (devci = devices.begin(); devci != devices.end(); devci++)
	{
		pdIndex++;
		wstring ws = (*devci).szName;
		addr = (*devci).Address;
		
		cout << "Device: " << string(ws.begin(), ws.end())<<"	"<<"[Address]: "<<addr.ullLong<< endl;	//<< uppercase << hex << 地址转换为16进制,可以先通过这段代码获取你的设备地址,蓝牙名称不能为汉字

		// see if we find our device (case sensitive)
		if (addr.ullLong == strr.ullLong)	//如果找到要配对的设备则记录指针位置
			foundDev = pdIndex;
	}
	
	// pick our ismp device
	if (foundDev == -1)
	{
		cout << "Could not find a device to pair" << endl;
		system("pause");
		return 1;
	}
	
	BLUETOOTH_DEVICE_INFO pd = devices[foundDev];
	wstring ws = pd.szName;
	cout << "Found device to pair, " << string(ws.begin(), ws.end()) << endl;

	// attempt to pair device
	pairDevice(pd);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值