VC++实现蓝牙的对接,真实应用过处理蓝牙相关

#include "StdAfx.h"
#include "BlueTooth.h"

CBlueTooth::CBlueTooth(void)
{
    m_Ary_RemoteBthDevInfo.SetSize ( 0, 10*sizeof(BLUETOOTH_DEVICE_INFO) );
    m_Ary_LocalRadioInfo.SetSize ( 0, 10*sizeof(t_LocalRadioInfo) );
}

CBlueTooth::~CBlueTooth(void)
{
    RemoveAll_RemoteBthDevInfo ();
    RemoveAllLocalRadio ();
}

//
// 用 Socket 函数搜索附近的蓝牙设备,成功时返回设备数,否则返回-1
//
int CBlueTooth::WSAScanNearbyBthDev ()
{
    m_Ary_RemoteBthDevInfo.RemoveAll ();

    WSAQUERYSET wsaq;
    HANDLE hLookup;
    union
    {
        CHAR buf[5000];
        double __unused; // ensure proper alignment
    };

    LPWSAQUERYSET pwsaResults = (LPWSAQUERYSET) buf;
    DWORD dwSize = sizeof(buf);
    BOOL bHaveName;
    ZeroMemory(&wsaq, sizeof(wsaq));
    wsaq.dwSize = sizeof(wsaq);
    wsaq.dwNameSpace = NS_BTH;
    wsaq.lpcsaBuffer = NULL;
    if (ERROR_SUCCESS != WSALookupServiceBegin (&wsaq, LUP_CONTAINERS, &hLookup))
    {
        TRACE ( _T("WSALookupServiceBegin failed\n") );
        return -1;
    }

    ZeroMemory(pwsaResults, sizeof(WSAQUERYSET));
    pwsaResults->dwSize = sizeof(WSAQUERYSET);
    pwsaResults->dwNameSpace = NS_BTH;
    pwsaResults->lpBlob = NULL;
    while (ERROR_SUCCESS == WSALookupServiceNext (hLookup, LUP_RETURN_NAME | LUP_RETURN_ADDR, &dwSize, pwsaResults))
    {
        ASSERT (pwsaResults->dwNumberOfCsAddrs == 1);
        BTH_ADDR b = ((SOCKADDR_BTH *)pwsaResults->lpcsaBuffer->RemoteAddr.lpSockaddr)->btAddr;
        bHaveName = pwsaResults->lpszServiceInstanceName && *(pwsaResults->lpszServiceInstanceName);
        t_RemoteBthDevInfo RemoteBthDevInfo;
        if ( bHaveName )
        {
            StringCchPrintf ( RemoteBthDevInfo.szName, sizeof(RemoteBthDevInfo.szName), _T("%s"), 
                pwsaResults->lpszServiceInstanceName );
        }
        RemoteBthDevInfo.Address.ullLong = b;

        TRACE (L"%s ( %04x%08x )\n", RemoteBthDevInfo.szName, GET_NAP(b), GET_SAP(b) );
        m_Ary_RemoteBthDevInfo.Add ( RemoteBthDevInfo );
    }
    WSALookupServiceEnd(hLookup);

    return (int)m_Ary_RemoteBthDevInfo.GetSize();
}

BOOL AUTHENTICATION_CALLBACK (LPVOID pvParam, PBLUETOOTH_DEVICE_INFO pDevice)
{
    t_AUTHENTICATION_CALLBACK_Para *pAUTHENTICATION_CALLBACK_Para = (t_AUTHENTICATION_CALLBACK_Para*)pvParam;
    if ( pAUTHENTICATION_CALLBACK_Para )
    {
        pDevice->fAuthenticated = TRUE;
        DWORD result = BluetoothUpdateDeviceRecord ( pDevice );
        ASSERT ( result == ERROR_SUCCESS );
        result = BluetoothSendAuthenticationResponse ( pAUTHENTICATION_CALLBACK_Para->hRadio, pDevice, AUTHENTICATION_PASSKEY );
        if ( result == ERROR_SUCCESS )
            return TRUE;
    }
    return FALSE;
}

//
// 用蓝牙 APIs 搜索附近的蓝牙设备,成功时返回设备数,否则返回-1
//
int CBlueTooth::ScanNearbyB

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值