根據微軟官網的範例說明,廠商在設計Windows8的藍芽驅動程式,都需要預留一組通用的控制介面,分別是負責藍芽開關(BluetoothEnableRadio)、負責確認藍芽狀態(IsBluetoothRadioEnabled),這組廠商所寫通用控制介面,我們就可以使用自己寫好的程式去控制Bluetooth,不過還有個前提就是必須安裝驅動程式才行。
範例說明如下
- 開啟登錄檔。
- 找"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\BTHPORT\Parameters\Radio Support"路徑下的"SupportDLL"。
- 取得電腦上DLL完整路徑。
- 載入DLL,使用DLL中的BluetoothEnableRadio、IsBluetoothRadioEnabled函數。
#include "stdafx.h" #include <Shlwapi.h> #pragma comment(lib, "Shlwapi.lib") typedef DWORD (WINAPI * DLL_BluetoothEnableRadio)(BOOL fEnable); DLL_BluetoothEnableRadio BluetoothEnableRadio; typedef DWORD (WINAPI * DLL_IsBluetoothRadioEnabled)(BOOL* pfEnabled); DLL_IsBluetoothRadioEnabled IsBluetoothRadioEnabled; int _tmain(int argc, TCHAR* argv[]) { HKEY hk; LONG lreturn; CHAR cDLLPath[200]; DWORD dwLen = 200; HINSTANCE hDLL = NULL; BOOL fEnabled; lreturn = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\BTHPORT\\Parameters\\Radio Support", 0, KEY_READ, &hk); if(lreturn==ERROR_SUCCESS) { ZeroMemory(cDLLPath, sizeof(cDLLPath)); lreturn = RegQueryValueEx(HKEY_LOCAL_MACHINE, "SupportDLL", NULL, NULL, (UCHAR*)cDLLPath, &dwLen); if(lreturn==ERROR_SUCCESS) { ZeroMemory(cDLLPath, sizeof(cDLLPath)); //轉換路徑%SystemRoot% => C:\Windows lreturn = SHRegGetPath(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\BTHPORT\\Parameters\\Radio Support", "SupportDLL", cDLLPath, 0); if(lreturn==ERROR_SUCCESS) { hDLL = LoadLibrary(cDLLPath); if (hDLL == NULL) { printf("Error load Bluetooth dll fail. %08X\n", GetLastError()); RegCloseKey(hk); return FALSE; } BluetoothEnableRadio = (DLL_BluetoothEnableRadio)GetProcAddress(hDLL, "BluetoothEnableRadio"); if (!BluetoothEnableRadio) { printf("Error BluetoothEnableRadio fail. %08X\n", GetLastError()); FreeLibrary(hDLL); RegCloseKey(hk); return FALSE; } IsBluetoothRadioEnabled = (DLL_IsBluetoothRadioEnabled)GetProcAddress(hDLL, "IsBluetoothRadioEnabled"); if (!IsBluetoothRadioEnabled) { printf("Error IsBluetoothRadioEnabled fail. %08X\n", GetLastError()); FreeLibrary(hDLL); RegCloseKey(hk); return FALSE; } IsBluetoothRadioEnabled(&fEnabled); printf("BT status : %s\n", fEnabled ? "Enable" : "Disable"); BluetoothEnableRadio(atoi(argv[1])); Sleep(4000);//等待BT反應 IsBluetoothRadioEnabled(&fEnabled); printf("BT status : %s\n", fEnabled ? "Enable" : "Disable"); } } else { printf("\"SupportDLL\" is not exist.\n"); RegCloseKey(hk); return FALSE; } } else { printf("\"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\BTHPORT\\Parameters\\Radio Support\" is not exist.\n"); return FALSE; } RegCloseKey(hk); return TRUE; }
沒有留言:
張貼留言