蓝牙的状态有三种状态,分别为:BTH_POWER_OFF、BTH_CONNECTABLE、BTH_DISCOVERABLE。
三种状态分别是关闭蓝牙;打开蓝牙,使蓝牙可连接;打开连接,使蓝牙可连接和可发现。
获取蓝牙的状态是利用API函数BthGetMode。设置蓝牙的状态是利用API函数BthSetMode。
首先我们看一下BthGetMode
其原型为:
int BthGetMode(
DWORD* pdwMode
);
其作用为:获得蓝牙设备当前的状态模式.
返回值:如果返回ERROR_SUCCESS ,则成功,否则失败。
pdwMode
);
其作用为:获得蓝牙设备当前的状态模式.
返回值:如果返回ERROR_SUCCESS ,则成功,否则失败。
BthSetMode
其原型为:
int BthSetMode(
DWORD dwMode
);
其作用为:设置蓝牙设备的状态模式,并将它表现在控制面板上。自启或硬件插入的时候都将维持此状态模式。
返回值:如果返回ERROR_SUCCESS ,则成功,否则失败。
需要在项目属性的“链接器-输入”里设置附加项bthutil.lib
// BTSwitch.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "bthutil.h"
#include "mmsystem.h"
#include "utils.h"
// TODO: Get rid of hard coded strings
// WAV Filenames
const TCHAR* kszOffFileName = _T("//OFF.WAV");
const TCHAR* kszConnectableFileName = _T("//CONNECTABLE.WAV");
const TCHAR* kszVisibleFileName = _T("//VISIBLE.WAV");
// Command line option for Visivle Mode
const TCHAR* kszVisibleMode = _T("-v");
bool bDiscovrableMode = false; // by default only toggle between off and connectable modes
int _tmain(int argc, TCHAR* argv[])
{
DWORD dwMode;
const TCHAR* pszSound = NULL;
TCHAR pszFileName[MAX_PATH];
//check for command line parameter
bDiscovrableMode = CheckCmdLineParameter(argc, argv, kszVisibleMode, true);
// Get Current BT Mode
if (BthGetMode(&dwMode) == ERROR_SUCCESS)
{
// Toggle between Off and either Connectable or Discoverable modes acording to the bDiscovrableMode flag
if (dwMode != BTH_POWER_OFF)
{
dwMode = BTH_POWER_OFF; // curently BT is on, turn it off
}
else
{ // BT is off, turn it on
bDiscovrableMode ? dwMode = BTH_DISCOVERABLE : dwMode = BTH_CONNECTABLE;
}
// Set the new mode
if (BthSetMode(dwMode) == ERROR_SUCCESS)
{
// Play a sound with the new mode
switch (dwMode)
{
case BTH_POWER_OFF:
pszSound = kszOffFileName;
break;
case BTH_CONNECTABLE:
pszSound = kszConnectableFileName;
break;
case BTH_DISCOVERABLE:
pszSound = kszVisibleFileName;
break;
}
GetCurrentDirectory(pszFileName);
_tcscat(pszFileName, pszSound);
PlaySound (pszFileName, NULL, SND_SYNC|SND_FILENAME);
}
}
return 0;
}