mercredi 2 août 2017

C++ Bluetooth - Error connexion

I've a small problem with my program. This program is used for interface bluetooth API and other programs.

vector scanDevices() : Scan all bluetooth devices. This method return a BLUETOOTH_DEVICE list. This method work fine.

BOOL WINAPI BluetoothAuthCallback(LPVOID pvParam, PBLUETOOTH_AUTHENTICATION_CALLBACK_PARAMS pAuthCallbackParams) : Send an Authentification response. The response is adapted to authentification method. This method work fine is authentification method == NUMERIC_COMPARISON (my phone) This method work bad is authentification method == METHOD_LEGACY (my headset)

void pairDevice(BLUETOOTH_DEVICE_INFO device) : Pair a device. This method work fine is authentification method == NUMERIC_COMPARISON (my phone) This method work bad is authentification method == METHOD_LEGACY (my headset)

I don't understand how connect my headset with this program. Somebody have an idea ?

#include <windows.h>
#include "bthdef.h"
#include "BluetoothAPIs.h"
#include <tchar.h>
#include <string>
#include <iostream>
#include <vector>

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

#define InterF4

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;
}

// Authentication callback
BOOL WINAPI BluetoothAuthCallback(LPVOID pvParam, PBLUETOOTH_AUTHENTICATION_CALLBACK_PARAMS pAuthCallbackParams)
{
    DWORD dwRet;

    fprintf(stderr, "BluetoothAuthCallback 0x%x\n", static_cast<unsigned int>(pAuthCallbackParams->deviceInfo.Address.ullLong));
    BLUETOOTH_AUTHENTICATE_RESPONSE AuthRes;
    AuthRes.authMethod = pAuthCallbackParams->authenticationMethod;
    fprintf(stderr, "Authmethod %d\n", AuthRes.authMethod);
    fprintf(stderr, "I/O : %d\n", pAuthCallbackParams->ioCapability);

// With BLUETOOTH_AUTHENTICATION_METHOD_NUMERIC_COMPARISON connection working
if (AuthRes.authMethod == BLUETOOTH_AUTHENTICATION_METHOD_NUMERIC_COMPARISON)
{
    fprintf(stderr, "Numeric Comparison supported\n");

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

    // Respond with numerical value for Just Works pairing
    AuthRes.numericCompInfo.NumericValue = 1;

    // Send authentication response to authenticate device
    dwRet = BluetoothSendAuthenticationResponseEx(NULL, &AuthRes);
}
// With BLUETOOTH_AUTHENTICATION_METHOD_LEGACY connection not working
else if (AuthRes.authMethod == BLUETOOTH_AUTHENTICATION_METHOD_LEGACY)
    {
        AuthRes.bthAddressRemote = pAuthCallbackParams->deviceInfo.Address;
        AuthRes.negativeResponse = FALSE;

        //Pin
        UCHAR pin[] = "0000";
        std::copy(pin, pin + sizeof(pin), AuthRes.pinInfo.pin);
        AuthRes.pinInfo.pinLength = sizeof(pin) - 1;;

        // Respond with numerical value for Just Works pairing
        AuthRes.numericCompInfo.NumericValue = 1;

        fprintf(stderr, "Authentication via a PIN\n");
        dwRet = BluetoothSendAuthenticationResponseEx(NULL, &AuthRes);
    }

    if (dwRet != ERROR_SUCCESS)
    {
        fprintf(stderr, "BluetoothSendAuthenticationResponseEx ret %d\n", dwRet);
    }
    else
    {
        fprintf(stderr, "BluetoothAuthCallback finish\n");
    }

    return 1; // This value is ignored
}

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

    // register callback
    cout << "Registering callback" << endl;
    HBLUETOOTH_AUTHENTICATION_REGISTRATION hCallbackHandle = 0;
    DWORD result = BluetoothRegisterForAuthenticationEx(NULL, &hCallbackHandle, (PFN_AUTHENTICATION_CALLBACK_EX)&BluetoothAuthCallback, NULL);
    if (result != ERROR_SUCCESS)
    {
        return;
    }

    // authenticate
    result = BluetoothAuthenticateDeviceEx(NULL, NULL, &device, NULL, MITMProtectionNotRequired);

    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;
    for (devci = devices.begin(); devci != devices.end(); devci++)
    {
        pdIndex++;
        wstring ws = (*devci).szName;
        cout << "Device: " << string(ws.begin(), ws.end()) << endl;

        // see if we find our device (case sensitive)
#ifdef InterF4
        if (ws.find(L"Interphone F4") != string::npos)
#else
        if (ws.find(L"Redmi") != string::npos)
#endif
            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);

    system("PAUSE");

    return 0;
}

1 commentaire:

  1. We are exactly the same problem. We are exactly the samedi bluetooth devices.
    It's so curious.

    RépondreSupprimer