Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
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:

These values are defined in the BTH_RADIO_MODEenumeration.

Sample Code

The following code example shows how to change the Bluetooth radio state.

[C++]

Copy Code
  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#]

Copy Code
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]

Copy Code
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 

See Also

Other Resources