2012-06-25 27 views

回答

0

您可以使用MSDN Bluetooth Functions下記錄的功能來完成此操作。

這些可以編程方式搜索和配對藍牙設備。

+0

感謝您快速回答 – Taras

0

我遇到了同樣的問題,我已經解決了這個問題,也許你可以試試:

  1. 使名爲pairtool.exe一個Windows工具,它幫助您通過命令行來配對。關鍵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代碼:

    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 
    

這裏是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; 
} 
+0

嗨。你能分享你的應用程序的詳細代碼嗎?謝謝! – AccurateEstimate

+0

代碼已更新 – zhouziwei

1

的Python是一個誘人而且整體簡單的解決方案,但是PyBlueZ並沒有在這裏公開Windows藍牙認證API: https://msdn.microsoft.com/en-us/library/windows/desktop/cc766819(v=vs.85).aspx

解決這個問題的一種方法是創建一個命令行工具並通過Python使用它。要爲Windows創建命令行工具,請使用Visual Studio並將必要的庫添加到項目鏈接程序屬性中:Bthprops.lib和ws2_32.lib

下面是項目的代碼,用於創建帶有1個參數的命令行工具, MAC地址,使用「Just Works」配對將指定設備配對。請參閱使用密鑰配對的註釋代碼。

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

代替創建這個你自己的,你可能會想嘗試這種預製的解決方案: http://bluetoothinstaller.com/bluetooth-command-line-tools/ 它沒有爲我的特定解決方案的工作。

然後,您將需要以管理員身份從python運行您的下載或自定義命令行工具。爲了可靠地做到這一點,我推薦了這個問題: How to run python script with elevated privilege on windows

相關問題