using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.WindowsMobile.Status;


namespace BluetoothStatus_CS
{
    public partial class Form1 : Form
    {
         public enum RadioMode
        {
            Off = 0,
            Connectable = 1,
            Discoverable = 2
        }

        /// <summary>
        /// Получает текущий статус bluetooth
        /// </summary>
        /// <param name="dwMode">флаги</param>
        /// <returns></returns>
        [DllImport("BthUtil.dll")]
        public static extern int BthGetMode(out RadioMode dwMode);

        /// <summary>
        /// Устанавливает новый режим bluetooth
        /// </summary>
        /// <param name="dwMode">флаги для установки режима</param>
        /// <returns></returns>
        [DllImport("BthUtil.dll")]
        public static extern int BthSetMode(RadioMode dwMode);
 
        public Form1()
        {
            InitializeComponent();
            #if DEBUG
                MinimizeBox = false;
            #else
                MinimizeBox = true;
            #endif

            SystemState bluetoothState = new SystemState(SystemProperty.BluetoothStatePowerOn);
            bluetoothState.Changed += new ChangeEventHandler(bluetoothState_Changed);

        }

        void bluetoothState_Changed(object sender, ChangeEventArgs args)
        {
            ShowInfo();
        }

        private void ShowInfo()
        {
            //if (GetBluetoothMode() == RadioMode.Off)
            //{
            //    lblStatus.Text = "Bluetooth выключен";             
            //}
            //else
            //{
            //    lblStatus.Text = "Bluetooth включен.";
            //}

            label1.Text = SystemState.BluetoothStatePowerOn.ToString();
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void mnuOn_Click(object sender, EventArgs e)
        {
            
            SetBluetoothMode(RadioMode.Connectable);
            lblStatus.Text = "Bluetooth включен.";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //RadioMode mode;
            //int ret = BthGetMode (out mode);
            //ToggleMode();
            //lblStatus.Text = mode.ToString();
        }

        private void mnuOff_Click(object sender, EventArgs e)
        {
            //BthSetMode(RadioMode.Off);
            SetBluetoothMode(RadioMode.Off);
            lblStatus.Text = "Bluetooth выключен.";
        }

        private RadioMode GetBluetoothMode()
        {
            RadioMode radioMode = RadioMode.Off;

            BthGetMode(out radioMode);

            return radioMode;
        }

        private int SetBluetoothMode(RadioMode radioMode)
        {
            int ret = BthSetMode(radioMode);

            return ret;
        }

        private void ToggleMode()
        {
            if (GetBluetoothMode() == RadioMode.Off)
                SetBluetoothMode(RadioMode.Connectable);
            else
                SetBluetoothMode(RadioMode.Off);
        }


    }
}