Changing the Bluetooth Radio Mode
4/8/2010
Windows Mobile provides the following APIs that enable you to change the Bluetooth power status on a local device.
The supported power states for Bluetooth are as follows:
- Power off
Bluetooth is turned off on the device. - Connectable
Bluetooth is turned on. The local device can initiate a connection with a peer device but the local device is not discoverable by peer devices that are within range. - Discoverable
Bluetooth is turned on. The local device is listed, when a peer device searches for available devices within range. The peer device can pair with the local device.Security Note: Turn off Bluetooth after data transfer is complete. Enable Discoverable mode only when required. Use a different passcode for the pairing process each time.
These values are defined in the BTH_RADIO_MODE enumeration.
Sample Code
The following code example shows how to change the Bluetooth radio state.
[C++]
DWORD dwMode;
WCHAR szMessage[50];
iResult = BthGetMode(&dwMode);
StringCchPrintf( szMessage, sizeof(szMessage), L"Current mode: %x. Change state? ", dwMode);
if (MessageBox(NULL, szMessage, L"Info", MB_YESNO) == IDYES)
{
if (dwMode == BTH_POWER_OFF )
{
BthSetMode(BTH_DISCOVERABLE);
}
else
{
BthSetMode(BTH_POWER_OFF);
}
StringCchPrintf( szMessage, sizeof(szMessage), L"Status changed. \n Current Status: %x", dwMode);
MessageBox(NULL, szMessage, L"Info", MB_OK);
}
[C#]
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
namespace BthToggleRadio
{
class BthRadio
{
public enum RadioMode
{
Off = 0,
Connectable = 1,
Discoverable =2
}
[DllImport ("BthUtil.dll")]
public static extern int BthGetMode (out RadioMode dwMode);
[DllImport("BthUtil.dll")]
public static extern int BthSetMode(RadioMode dwMode);
public static void Main()
{
RadioMode mode=RadioMode.Off;
int ret = 0;
ret=BthGetMode (out mode);
DialogResult resDlg;
resDlg=MessageBox.Show("Current mode: " + mode.ToString() + "\n" + "Change status?", "Bluetooth Status", MessageBoxButtons .YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
if (resDlg == DialogResult.Yes)
{
if (mode == RadioMode.Off)
{
ret = BthSetMode(RadioMode.Discoverable);
}
else
{
ret = BthSetMode(RadioMode.Off);
}
ret = BthGetMode(out mode);
resDlg = MessageBox.Show("Status changed. \n" + "Current Status: " + mode.ToString(), "Bluetooth Status");
}
}
}
}
[Visual Basic]
Imports System
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Class BthRadio
Public Enum RadioMode
RadioOff = 0
Connectable = 1
Discoverable = 2
End Enum 'RadioMode
Public Declare Function BthGetMode Lib "BthUtil.dll" (ByRef dwMode As RadioMode) As Integer
Public Declare Function BthSetMode Lib "BthUtil.dll" (ByVal dwMode As RadioMode) As Integer
Public Shared Sub Main()
Dim mode As RadioMode = RadioMode.RadioOff
Dim ret As Integer = 0
ret = BthGetMode(mode)
Dim resDlg As DialogResult
resDlg = MessageBox.Show("Current mode: " + mode.ToString() + vbLf + "Change status?", "Bluetooth Status", MessageBoxButtons.YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button1)
If resDlg = DialogResult.Yes Then
If mode = RadioMode.RadioOff Then
ret = BthSetMode(RadioMode.Discoverable)
Else
ret = BthSetMode(RadioMode.RadioOff )
End If
ret = BthGetMode(mode)
resDlg = MessageBox.Show("Status changed. " + vbLf + "Current Status: " + mode.ToString(), "Bluetooth Status")
End If
End Sub 'Main
End Class 'BthRadio