回答
是,the reference documentation is available on MSDN。
32feet.NET是C#封裝器,available here。有關配對的信息是here。
我遇到了同樣的問題,我已經解決了這個問題,也許你可以試試:
使名爲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); }
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;
}
嗨。你能分享你的應用程序的詳細代碼嗎?謝謝! – AccurateEstimate
代碼已更新 – zhouziwei
的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
- 1. 如何以編程方式配對藍牙設備在Android
- 2. 如何以編程方式配對藍牙設備Android
- 3. 有沒有辦法以編程方式打開/關閉iPhone中的藍牙?
- 4. Android以編程方式藍牙配對
- 5. 是否有可能以編程方式與藍牙設備配對?
- 6. 在android中有2種藍牙設備的自動/編程配對方式嗎?
- 7. 如何以編程方式在Symbian上使用PyS60配對藍牙設備?
- 8. 有沒有什麼辦法以編程方式從JFileChooser.showOpenDialog()返回?
- 9. 如何在沒有用戶交互的情況下以編程方式配對藍牙設備?
- 10. 以編程方式配對藍牙設備,而無需用戶輸入PIN碼
- 11. 使用配對的藍牙設備以編程方式解鎖iphone?
- 12. 藍牙沒有配對
- 13. 有沒有辦法在iOS中訪問藍牙4.0低功耗設備?
- 14. 在Android Studio中配對藍牙設備
- 15. 有沒有什麼辦法在.NET中以編程方式監聽HTTP流量?
- 16. 有沒有什麼辦法在.NET中以編程方式監聽HTTP流量?
- 17. 有沒有什麼辦法在Android KitKat中以編程方式阻止SMS?
- 18. 有沒有辦法以編程方式配置鏟子?
- 19. 有沒有辦法以編程方式配置MDBs?
- 20. 有沒有辦法在iOS中以編程方式打開和關閉藍牙和/或WiFi?
- 21. 以編程方式使用藍牙連接兩個Android設備
- 22. 以編程方式連接到藍牙設備目標c
- 23. 以編程方式連接到藍牙設備
- 24. Android +以編程方式通過藍牙連接設備
- 25. 以編程方式將WP8設備連接到藍牙耳機
- 26. 通過藍牙以編程方式連接兩個Android設備
- 27. 有沒有辦法以編程方式將ApartmentState設置爲STA?
- 28. Python/Sqlite3 - 有沒有辦法以編程方式設置.nullvalue?
- 29. 有沒有辦法以編程方式root/unroot一個android設備?
- 30. 有沒有辦法以編程方式告訴設備的VM預算?
謝謝,沒錯。 – Taras
我可以使用32feet.NET進行商業項目嗎? – Taras