目前共有3篇帖子。 內容轉換:不轉換▼
 
點擊 回復
542 2
【程序】蓝牙设备的搜索与配对
一派護法 十九級
1樓 發表于:2016-7-16 18:26
#include <iomanip>
#include <iostream>
#include <Windows.h>
#include <BluetoothAPIs.h>

#pragma comment(lib, "Bthprops.lib")
#pragma warning(disable: 4995)

using namespace std;

ostream &operator << (ostream &os, const wchar_t *wstr)
{
    if (os == cout)
        WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), wstr, wcslen(wstr), NULL, NULL);
    return os;
}

void show_address(BLUETOOTH_ADDRESS *addr)
{
    int i;
    cout << hex << uppercase << setfill('0');
    for (i = 5; i >= 0; i--)
    {
        cout << setw(2) << (int)addr->rgBytes[i];
        if (i != 0)
            cout << ':';
    }
    cout << dec;
}

void show_time(LPSYSTEMTIME lpTime, bool fConvertToLocal = true)
{
    SYSTEMTIME stLocal;
    ZeroMemory(&stLocal, sizeof(stLocal));
    if (memcmp(lpTime, &stLocal, sizeof(SYSTEMTIME)) == 0)
    {
        cout << L"无";
        return;
    }
    if (fConvertToLocal)
    {
        SystemTimeToTzSpecificLocalTime(NULL, lpTime, &stLocal); // 转换为系统时区
        lpTime = &stLocal;
    }

    cout << setfill('0');
    cout << setw(4) << lpTime->wYear << '-' << lpTime->wMonth << '-' << lpTime->wDay << ' ';
    cout << setw(2) << lpTime->wHour << ':';
    cout << setw(2) << lpTime->wMinute << ':';
    cout << setw(2) << lpTime->wSecond;
}

int main(void)
{
    /* 查找蓝牙发射器 */
    BLUETOOTH_FIND_RADIO_PARAMS btfrp;
    btfrp.dwSize = sizeof(BLUETOOTH_FIND_RADIO_PARAMS);

    HANDLE hRadio;
    HBLUETOOTH_RADIO_FIND hFind = BluetoothFindFirstRadio(&btfrp, &hRadio);
    if (hFind == NULL)
    {
        cout << TEXT("无法找到蓝牙发射器") << endl;
        return 0;
    }

    BLUETOOTH_RADIO_INFO btri;
    btri.dwSize = sizeof(BLUETOOTH_RADIO_INFO);
    int i = 0;
    do
    {
        i++;
        BluetoothGetRadioInfo(hRadio, &btri);
        cout << TEXT("[蓝牙发射器") << setw(3) << setfill('0') << i << ']' << endl;
        cout << TEXT("名称: ") << btri.szName << endl;
        cout << TEXT("地址: ");
        show_address(&btri.address);
        cout << endl << TEXT("制造商编号: ") << btri.manufacturer << endl;
        
        CloseHandle(hRadio);
    } while (BluetoothFindNextRadio(hFind, &hRadio));
    BluetoothFindRadioClose(hFind);
    cout << endl;

    /* 查找蓝牙设备 */
    BLUETOOTH_DEVICE_SEARCH_PARAMS btsp;
    btsp.dwSize = sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS);
    btsp.cTimeoutMultiplier = 10;
    btsp.fReturnAuthenticated = TRUE;
    btsp.fReturnConnected = TRUE;
    btsp.fReturnRemembered = TRUE;
    btsp.fReturnUnknown = TRUE;
    btsp.fIssueInquiry = FALSE; // 是否启动新的查询
    btsp.hRadio = NULL; // 使用所有收发器

    bool found = false;
    BLUETOOTH_DEVICE_INFO btdi, found_info;
    btdi.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
    DWORD dwErr;
    HBLUETOOTH_DEVICE_FIND hFindDevice = BluetoothFindFirstDevice(&btsp, &btdi);
    if (hFindDevice == NULL)
    {
        dwErr = GetLastError();
        if (dwErr == ERROR_NO_MORE_ITEMS)
            cout << TEXT("未找到任何设备") << endl;
        else
            cout << TEXT("查找设备时出错") << endl;
        return 0;
    }
    i = 0;
    do
    {
        i++;
        cout << TEXT("[设备") << setw(3) << setfill('0') << i << ']' << endl;
        cout << TEXT("名称: ") << btdi.szName << endl;
        if (wcscmp(btdi.szName, L"octMCU2") == 0)
        {
            found = true;
            found_info = btdi;
        }
        cout << TEXT("地址: ");
        show_address(&btdi.Address);
        cout << endl << TEXT("连接状态: ") << ((btdi.fConnected) ? TEXT("已连接") : TEXT("未连接")) << endl;
        cout << TEXT("配对状态: ") << ((btdi.fAuthenticated) ? TEXT("已配对") : TEXT("未配对")) << endl;
        cout << TEXT("发现时间: ");
        show_time(&btdi.stLastSeen);
        cout << endl << TEXT("最后使用时间: ");
        show_time(&btdi.stLastUsed);
        cout << endl << endl;
    } while (BluetoothFindNextDevice(hFindDevice, &btdi));
    BluetoothFindDeviceClose(hFindDevice);

    if (found)
    {
        cout << L"已找到所需设备";
        if (!found_info.fAuthenticated)
            cout << L", 开始配对...";
        cout << endl;
        if (!found_info.fAuthenticated)
        {
            WCHAR szKey[] = L"1805";
            dwErr = BluetoothAuthenticateDevice(NULL, NULL, &found_info, szKey, wcslen(szKey));
            switch (dwErr)
            {
            case ERROR_SUCCESS:
                cout << L"配对成功" << endl;
                break;
            default:
                cout << L"配对失败, 错误码为: " << dwErr << endl;
            }
        }
    }
    else
        cout << L"未找到所需设备" << endl;
    return 0;
}
10.110.197.*
2樓 發表于:2017-10-16 19:21
HI 請問一下

您的bluetooth的PIN馬是如何得到的呢

我配對的時候都會有提示,但我不想要有提示出現


180.84.26.*
3樓 發表于:2017-10-16 22:15
回復2樓 10.110.197.* 的內容:
HI 請問一下
您的bluetooth的PIN馬是如何得到的呢
我配對的時候都會有提示,但我不想要有提示出現
配对码是WCHAR szKey[] = L"1805";这个字符串提供的,不会有提示出现的。要不就是驱动的问题。

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:542 回複數:2
評論數: ?
作者:巨大八爪鱼
最後回復:180.84.26.*
最後回復時間:2017-10-16 22:15
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。