从来没有搞过蓝牙只能网上找,搞了两天总算是找到了点门路,忘记借鉴的谁的代码,修改了一下终于得到了需要的功能~直接上代码(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;
}