2009-02-02

.Net CF программное управление bluetooth

Для начала нам нужно подключить пространство имен System.Runtime.InteropServices

using System.Runtime.InteropServices;

Теперь создаем свой класс :

public class Network
{
public enum BluetoothMode
{
Off=0,
Connectable=1,
Discoverable=2
}
[DllImport("BthUtil.dll")]
private static extern int BthGetMode(out BluetoothMode dwMode);
[DllImport("BthUtil.dll")]
public static extern int BthSetMode(BluetoothMode dwMode);
public static String GetBthMode()
{
BluetoothMode mode;
int ret = BthGetMode(out mode);
return mode.ToString();
}
}

Теперь мы можем попробовать переключить статусы. Например вот так :


if (Network.GetBthMode() == "Off") //Проверяем текущее состояние
Network.BthSetMode(Network.BluetoothMode.Connectable); //Переводим в состояние Connectable
else Network.BthSetMode(Network.BluetoothMode.Off); // или же выключаем