jaklsdkfjosfij 2017-08-10 05:50 采纳率: 60%
浏览 2006
已采纳

windows 蓝牙 api,主动配对手机失败。

下面是我的配对代码 和 回掉函数,我发现,我的电脑主动配对手机,手机上会提示失败:"无法和xxx-pc配对",但是我用手机主动配对电脑,就提示配对成功,请问各位大神,哪里有问题呢?

1.配对代码

 BOOL CBluetooth::BtSocketAuthenticate(char *szAddr)
{
    SOCKADDR_BTH sa = { 0 };
    int sa_len = sizeof(sa);
    DWORD dwRet;
    BLUETOOTH_DEVICE_INFO  btdi = { 0 };
    HBLUETOOTH_AUTHENTICATION_REGISTRATION hRegHandle = 0;

    // initialize windows sockets
    WORD wVersionRequested;
    WSADATA wsaData;
    wVersionRequested = MAKEWORD(2, 0);
    if (WSAStartup(wVersionRequested, &wsaData) != 0) {
        return FALSE;
    }

    dwRet = BluetoothRegisterForAuthenticationEx(&m_device.bdi, &hRegHandle, &BluetoothAuthCallback, NULL);
    if (dwRet != ERROR_SUCCESS)
    {
        return FALSE;
    }

    if ( m_device.bdi.fAuthenticated ) 
    {   
        return TRUE;    // 已经配对了
    }

    DWORD status = BluetoothUpdateDeviceRecord ( &m_device.bdi );
    if ( ERROR_SUCCESS != status )
        return FALSE;

    // authenticate device (will call authentication callback)
    AUTHENTICATION_REQUIREMENTS authreqs = MITMProtectionRequired ;
    dwRet = BluetoothAuthenticateDeviceEx(NULL, m_radio.hRadio , &m_device.bdi, NULL, authreqs);
}

2. 回掉函数

 BOOL CALLBACK BluetoothAuthCallback(LPVOID pvParam, PBLUETOOTH_AUTHENTICATION_CALLBACK_PARAMS pAuthCallbackParams)
{
    DWORD dwRet;

    BLUETOOTH_AUTHENTICATE_RESPONSE AuthRes;
    AuthRes.authMethod = pAuthCallbackParams->authenticationMethod;

    AuthRes.bthAddressRemote = pAuthCallbackParams->deviceInfo.Address;
    AuthRes.negativeResponse = FALSE;
    AuthRes.numericCompInfo.NumericValue = pAuthCallbackParams->Numeric_Value;

    // Send authentication response to authenticate device
    dwRet = BluetoothSendAuthenticationResponseEx(NULL, &AuthRes);
}
  • 写回答

2条回答 默认 最新

  • sinat_35781426 2017-08-21 02:14
    关注

    BOOL CALLBACK BluetoothAuthCallback(LPVOID pvParam, PBLUETOOTH_AUTHENTICATION_CALLBACK_PARAMS pAuthCallbackParams)
    {
    DWORD dwRet;

    BLUETOOTH_AUTHENTICATE_RESPONSE AuthRes;
    AuthRes.authMethod = pAuthCallbackParams->authenticationMethod;
    
    AuthRes.bthAddressRemote = pAuthCallbackParams->deviceInfo.Address;
    AuthRes.negativeResponse = FALSE;
    AuthRes.numericCompInfo.NumericValue = pAuthCallbackParams->Numeric_Value;
    
    // Send authentication response to authenticate device
    dwRet = BluetoothSendAuthenticationResponseEx(NULL, &AuthRes);
    

    }


    这部分应该有问题,检查一下

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎么改成输入一个要删除的数后现实剩余的数再输入一个删除的数再现实剩余的数用yes表示继续no结束程序
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?