藍牙無線電有打開、關閉、可發現三種狀態,那么我們如何實現編程控制呢?
藍牙設備的三種狀態
enum BTH_RADIO_MODE {
BTH_POWER_OFF,
BTH_CONNECTABLE,
BTH_DISCOVERABLE
};
API介紹
BthGetMode
作用:
This function is used to retrieve the current mode of operation of the Bluetooth radio.
原型:
int BthGetMode(DWORD* pdwMode);
返回值:
Returns ERROR_SUCCESS on success or returns an error code on failure.
BthSetMode
作用:
This function is used to set the Bluetooth mode of operation and reflect it in the control panel. This function is also used
to persist that state across hardware insertion and reboot.
原型:
int BthSetMode(DWORD dwMode );
返回值:
Returns ERROR_SUCCESS on success or returns an error code on failure.
環境:
OS Versions: Windows CE .NET 4.2 and later
Header: Declared in bthutil.h
Library: Use bthutil.lib
代碼示例:
功能:獲取藍牙設備狀態,如果設備狀態是關閉的,則將其設為可發現。設置是否成功,給出提示。
第一步:
首先需要添加頭文件和庫文件
#include <bthutil.h>
#pragma comment(lib, "bthutil.lib")
第二步:
獲取藍牙設備狀態
DWORD bthState;
if (ERROR_SUCCESS == BthGetMode(&bthState))
{
if (bthState == BTH_POWER_OFF)
{
if (ERROR_SUCCESS == BthSetMode(BTH_DISCOVERABLE))
{
MessageBox(NULL, _T("success"), _T("Note"), MB_OK);
}
else
{
MessageBox(NULL, _T("failed"), _T("Note"), MB_OK);
}
}
}
設置藍牙狀態是在模擬器上進行的,沒有設置成功,這是由于模擬器上沒有安裝藍牙設備造成的。
還需要繼續學習。
posted on 2008-07-24 17:29
Sandy 閱讀(1006)
評論(0) 編輯 收藏 引用 所屬分類:
Window Mobile