简体   繁体   English

有没有办法以编程方式在 Windows 中配对蓝牙设备

[英]Is there any way to pair Bluetooth device in Windows programmatically

there's a question: Is there any way to pair Bluetooth device in Windows programmatically?有一个问题:有没有办法以编程方式在 Windows 中配对蓝牙设备? (c++, c#) (c++, c#)

thanks for replies谢谢回复

Yes, the reference documentation is available on MSDN .的,参考文档可在 MSDN 上找到

32feet.NET is a C# wrapper, available here . 32feet.NET 是一个 C# 包装器,可在此处获取 Information on pairing is here .有关配对的信息在 这里

Python is a tempting and overall easy solution, but PyBlueZ does not expose the windows Bluetooth authentication APIs here: https://msdn.microsoft.com/en-us/library/windows/desktop/cc766819(v=vs.85).aspx Python 是一个诱人且整体简单的解决方案,但 PyBlueZ 不会在此处公开 Windows 蓝牙身份验证 API: https://msdn.microsoft.com/en-us/library/windows/desktop/cc766819(v=vs.85)。 aspx

One way to get around this is to create a command line tool and use this through Python.解决这个问题的一种方法是创建一个命令行工具并通过 Python 使用它。 To create command line tools for Windows, use Visual Studio and add the necessary libraries to your project linker properties: Bthprops.lib and ws2_32.lib要为 Windows 创建命令行工具,请使用 Visual Studio 并将必要的库添加到项目链接器属性:Bthprops.lib 和 ws2_32.lib

Below is the code for a project to make a command line tool with 1 parameter, the MAC address, that pairs the specified device using "Just Works" pairing.下面是一个项目的代码,用于制作一个带有 1 个参数(MAC 地址)的命令行工具,该工具使用“Just Works”配对对指定设备进行配对。 See commented code for using passkey pairing.请参阅使用密码配对的注释代码。

#include "stdafx.h"
#include <initguid.h>
#include <winsock2.h>
#include <BluetoothAPIs.h>
#include <ws2bth.h>

BOOL WINAPI BluetoothAuthCallback(LPVOID pvParam, PBLUETOOTH_AUTHENTICATION_CALLBACK_PARAMS pAuthCallbackParams);

int _tmain(int argc, _TCHAR* argv[])
{
    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) {
        ExitProcess(2);
    }

    // parse the specified Bluetooth address
    if (argc < 2) {
        fprintf(stderr, "usage: csbtpair <addr>\n"
            "\n  addr must be in the form (XX:XX:XX:XX:XX:XX)");
        ExitProcess(2);
    }
    if (SOCKET_ERROR == WSAStringToAddress(argv[1], AF_BTH,
        NULL, (LPSOCKADDR)&sa, &sa_len)) {
        ExitProcess(2);
    }

    // setup device info
    btdi.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
    btdi.Address.ullLong = sa.btAddr;
    btdi.ulClassofDevice = 0;
    btdi.fConnected = false;
    btdi.fRemembered = false;
    btdi.fAuthenticated = false;

    // register authentication callback. this prevents UI from showing up.
    dwRet = BluetoothRegisterForAuthenticationEx(&btdi, &hRegHandle, &BluetoothAuthCallback, NULL);
    if (dwRet != ERROR_SUCCESS)
    {
        fprintf(stderr, "BluetoothRegisterForAuthenticationEx ret %d\n", dwRet);
        ExitProcess(2);
    }

    // authenticate device (will call authentication callback)
    AUTHENTICATION_REQUIREMENTS authreqs = MITMProtectionNotRequired;
    fprintf(stderr, "BluetoothAuthReqs = %d\n", authreqs);
    dwRet = BluetoothAuthenticateDeviceEx(NULL, NULL, &btdi, NULL, authreqs);
    if (dwRet != ERROR_SUCCESS)
    {
        fprintf(stderr, "BluetoothAuthenticateDevice ret %d\n", dwRet);
        if (dwRet == ERROR_CANCELLED)
        {
            fprintf(stderr, "Cancelled");
        }
        else if (dwRet == ERROR_INVALID_PARAMETER)
        {
            fprintf(stderr, "Invalid Parameter");
        }
        else if (dwRet == ERROR_NO_MORE_ITEMS)
        {
            fprintf(stderr, "Already paired!");
        }
    }

    fprintf(stderr, "pairing finish\n");
    ExitProcess(0);
    return 0;
}

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

    fprintf(stderr, "BluetoothAuthCallback 0x%x\n", pAuthCallbackParams->deviceInfo.Address.ullLong);
    BLUETOOTH_AUTHENTICATE_RESPONSE AuthRes;
    AuthRes.authMethod = pAuthCallbackParams->authenticationMethod;
    fprintf(stderr, "Authmethod %d\n", AuthRes.authMethod);
    // Check to make sure we are using numeric comparison (Just Works)
    if (AuthRes.authMethod == BLUETOOTH_AUTHENTICATION_METHOD_NUMERIC_COMPARISON) 
    {
        fprintf(stderr, "Numeric Comparison supported\n");
    }
    AuthRes.bthAddressRemote = pAuthCallbackParams->deviceInfo.Address;
    AuthRes.negativeResponse = FALSE;

    // Commented out code is used for pairing using the BLUETOOTH_AUTHENTICATION_METHOD_PASSKEY method
    //memcpy_s(AuthRes.pinInfo.pin, sizeof(AuthRes.pinInfo.pin), L"1234", 0);
    //AuthRes.pinInfo.pinLength = 0;
    // Respond with numerical value for Just Works pairing
    AuthRes.numericCompInfo.NumericValue = 1;

    // Send authentication response to authenticate device
    dwRet = BluetoothSendAuthenticationResponseEx(NULL, &AuthRes);
    if (dwRet != ERROR_SUCCESS)
    {
        fprintf(stderr, "BluetoothSendAuthenticationResponseEx ret %d\n", dwRet);
        if (dwRet == ERROR_CANCELLED)
        {
            fprintf(stderr, "Bluetooth device denied passkey response or communicatino problem.\n");
        }
        else if (dwRet == E_FAIL)
        {
            fprintf(stderr, "Device returned a failure code during authentication.\n");
        }
        else if (dwRet == 1244)
        {
            fprintf(stderr, "Not authenticated\n");
        }
    }
    else
    {
        fprintf(stderr, "BluetoothAuthCallback finish\n");
    }

    return 1; // This value is ignored
}

In lieu of creating this yourself, you may want to try this pre-made solution: http://bluetoothinstaller.com/bluetooth-command-line-tools/ It did not work for my particular solution.代替自己创建这个,您可能想尝试这个预制解决方案:http: //bluetoothinstaller.com/bluetooth-command-line-tools/它不适用于我的特定解决方案。

Then, you will need to run your downloaded or custom command line tool from python as an administrator.然后,您需要以管理员身份从 python 运行下载的或自定义的命令行工具。 To do this reliably, I recommend the stackoverflow question: How to run python script with elevated privilege on windows为了可靠地做到这一点,我推荐 stackoverflow 问题: How to run python script with elevated privilege on windows

I meet the same problem,and I have resolved the problem, Maybe you can try it:我遇到了同样的问题,我已经解决了问题,也许你可以试试:

  1. make a windows tool named pairtool.exe, it help you to pairing with command line.制作一个名为 pairtool.exe 的 windows 工具,它可以帮助您与命令行配对。 the key api is BluetoothAuthenticateDevice, please refering the functions document关键api是BluetoothAuthenticateDevice,请参考函数文档

    dwRet = BluetoothAuthenticateDevice(NULL, NULL, &btdi, L"1234", 4); if(dwRet,= ERROR_SUCCESS) { fprintf(stderr, "BluetoothAuthenticateDevice ret %d\n"; dwRet); ExitProcess(2); }
  2. python code:蟒蛇代码:

     def connect2Btdev(devName): #found the device addr addr = inquiry(devName) if addr == None: return None #pairing with pairtool.exe cmd=r'%s %s' % ('pairtool.exe',addr) ret = os.system(cmd) if ret <> 0: return None

here is all the code of pairtool.exe:以下是 pairtool.exe 的所有代码:

#include "stdafx.h"
#include <initguid.h>
#include <winsock2.h>
#include <BluetoothAPIs.h>
#include <ws2bth.h>

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

    fprintf(stderr, "BluetoothAuthCallback 0x%x\n", pAuthCallbackParams->deviceInfo.Address.ullLong);

    dwRet = BluetoothSendAuthenticationResponse(NULL, &(pAuthCallbackParams->deviceInfo), L"1234");
    if(dwRet != ERROR_SUCCESS)
    {
        fprintf(stderr, "BluetoothSendAuthenticationResponse ret %d\n", dwRet);
        ExitProcess(2);
        return 1;
    }
    fprintf(stderr, "BluetoothAuthCallback finish\n");
    ExitProcess(0);
    return 1;
}

int _tmain(int argc, _TCHAR* argv[])
{
    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 ) {
        ExitProcess(2);
    }

    // parse the specified Bluetooth address
    if( argc < 2 ) {
        fprintf(stderr, "usage: rfcomm-client <addr>\n"
                "\n  addr must be in the form (XX:XX:XX:XX:XX:XX)");
        ExitProcess(2);
    }
    if( SOCKET_ERROR == WSAStringToAddress( argv[1], AF_BTH,
                NULL, (LPSOCKADDR) &sa, &sa_len ) ) {
        ExitProcess(2);
    }

    //注册回调函数
    btdi.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
    btdi.Address.ullLong = sa.btAddr;
    btdi.ulClassofDevice = 0;
    btdi.fConnected = false;
    btdi.fRemembered = false;
    btdi.fAuthenticated = false;

    dwRet = BluetoothRegisterForAuthenticationEx(&btdi, &hRegHandle, &BluetoothAuthCallback, NULL);
    if(dwRet != ERROR_SUCCESS)
    {
        fprintf(stderr, "BluetoothRegisterForAuthenticationEx ret %d\n", dwRet);
        ExitProcess(2);
    }

    dwRet = BluetoothAuthenticateDevice(NULL, NULL, &btdi, L"1234", 4);
    if(dwRet != ERROR_SUCCESS)
    {
        fprintf(stderr, "BluetoothAuthenticateDevice ret %d\n", dwRet);
        ExitProcess(2);
    }

    Sleep(1000);
      fprintf(stderr, "pairing finish\n");
    ExitProcess(0);

    return 0;
}

Microsoft has introduced Windows.Devices.Enumeration API available for UWP and traditional applications, which makes pairing of bluetooth devices very easy (for details look at the official C# and C++ example ).微软推出了适用于 UWP 和传统应用程序的Windows.Devices.Enumeration API,这使得蓝牙设备的配对变得非常容易(有关详细信息,请参阅官方 C# 和 C++ 示例)。 As far as I understand it is the API which is used by built-in "Bluetooth & other devices" UI dialog.据我了解,它是由内置的“蓝牙和其他设备”UI 对话框使用的 API。 As of an example of what console utility you can write using this API you can take a look at my console BluetoothDevicePairing utility.作为您可以使用此 API 编写的控制台实用程序的示例,您可以查看我的控制台BluetoothDevicePairing实用程序。

You can do so by using the functions documented under MSDN Bluetooth Functions .您可以使用MSDN Bluetooth Functions下记录的函数来执行此操作。

These enable searching and pairing of bluetooth devices programmatically.这些支持以编程方式搜索和配对蓝牙设备。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM