SelectBluetoothDeviceDialog.cs :  » Business-Application » 32feet.NET » InTheHand » ComponentModel » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Business Application » 32feet.NET 
32feet.NET » InTheHand » ComponentModel » SelectBluetoothDeviceDialog.cs
// 32feet.NET - Personal Area Networking for .NET
//
// InTheHand.Windows.Forms.SelectBluetoothDeviceDialog
// 
// Copyright (c) 2003-2010 In The Hand Ltd, All rights reserved.
// This source code is licensed under the In The Hand Community License - see License.txt

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;

namespace InTheHand.Windows.Forms{
  /// <summary>
  /// Provides a form to select an available Bluetooth device.
  /// </summary>
#if WinXP
    public class SelectBluetoothDeviceDialog : CommonDialog 
    {
        private BLUETOOTH_SELECT_DEVICE_PARAMS dialogWin32;
#else
  public class SelectBluetoothDeviceDialog : Component
    {
#endif
        private SelectBluetoothDeviceForm dialogCustom;
        private ISelectBluetoothDevice dialog;
    private BluetoothDeviceInfo device;

        /// <summary>
        /// Initializes an instance of the <see cref="SelectBluetoothDeviceDialog"/> class.
        /// </summary>
        public SelectBluetoothDeviceDialog()
            : this(false)
        {
        }

        internal SelectBluetoothDeviceDialog(bool forceCustomDialog)
        {
#if NETCF
            InTheHand.Net.PlatformVerification.ThrowException();
#endif
#if ! WinXP
            // Always the custom dialog on WM/CE.
            forceCustomDialog = true;
#endif
            BluetoothRadio radio;
            if (forceCustomDialog || (radio = BluetoothRadio.PrimaryRadio) != null && radio.SoftwareManufacturer != Manufacturer.Microsoft) {
                dialogCustom = new SelectBluetoothDeviceForm();
                dialog = dialogCustom;
            } else {
#if WinXP
                dialogWin32 = new BLUETOOTH_SELECT_DEVICE_PARAMS();
                dialogWin32.Reset();
                dialog = dialogWin32;
#else
                Debug.Fail("Should use custom dialog on non-Win32!");
#endif
            }
        }

        /// <summary>
        /// Resets the properties of the <see cref="SelectBluetoothDeviceDialog"/> to their default values.
        /// </summary>
#if WinXP
        public override void Reset()
#else
        public void Reset()
#endif
        {
            dialog.Reset();
        }

#if NETCF
    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public DialogResult ShowDialog()
    {
            return ShowCustomDialog();
        }
#endif

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    DialogResult ShowCustomDialog()
    {
            DialogResult dr = dialogCustom.ShowDialog();
            device = dialogCustom.selectedDevice;
#if NETCF
            if (ForceAuthentication) {
                ForceAuthenticationCE(device);
            }
#endif
      return dr;
    }

#if NETCF
        void ForceAuthenticationCE(BluetoothDeviceInfo device)
        {
            int hresult;
            ushort handle = 0;

            //connect to device
            try
            {
                hresult = NativeMethods.BthCreateACLConnection(device.DeviceAddress.ToByteArray(), out handle);
                if (hresult != 0)
                {
                    //success = false;
                }
                else
                {
                    //force authentication
                    hresult = NativeMethods.BthAuthenticate(device.DeviceAddress.ToByteArray());
                    /*if (hresult != 0)
                    {
                        success = false;
                    }*/
                }

            }
            finally
            {
                if (handle != 0)
                {
                    //close connection
                    hresult = NativeMethods.BthCloseConnection(handle);
                }
            }

        }
#else
        /// <summary>
        /// Specifies a common dialog box.
        /// </summary>
        /// <param name="hwndOwner">A value that represents the window handle of the owner window for the common dialog box.</param>
        /// <returns>true if the dialog box was successfully run; otherwise, false.</returns>
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            if (dialogCustom == null) {
                return RunDialogMsft(hwndOwner);
            } else {
                DialogResult rslt = ShowCustomDialog();
                return rslt == DialogResult.OK;
            }
        }

        bool RunDialogMsft(IntPtr hwndOwner)
        {
            //set parent window
            dialogWin32.hwndParent = hwndOwner;

            bool success = NativeMethods.BluetoothSelectDevices(ref dialogWin32);

            if (success)
            {
                if (dialogWin32.cNumDevices > 0)
                {
                    device = new BluetoothDeviceInfo(new WindowsBluetoothDeviceInfo(dialogWin32.Device));
                }

                bool freed = NativeMethods.BluetoothSelectDevicesFree(ref dialogWin32);
            }

            return success;
        }

        /// <summary>
    /// Gets or sets the information text.
    /// </summary>
        /// <remarks>Supported only on Windows XP/Vista with Microsoft stack.</remarks>
    public string Info
    {
      get
      {
        return dialog.Info;
      }
      set
      {
                dialog.Info = value;
      }
    }

        /// <summary>
        /// If TRUE, invokes the Add New Device Wizard.
        /// </summary>
        /// <remarks>Supported only on Windows XP/Vista with Microsoft stack.</remarks>
        public bool AddNewDeviceWizard
        {
            get
            {
                return dialog.AddNewDeviceWizard;
            }
            set
            {
                dialog.AddNewDeviceWizard = value;
            }
        }

        

        /// <summary>
        /// If TRUE, skips the Services page in the Add New Device Wizard.
        /// </summary>
        /// <remarks>Supported only on Windows XP/Vista with Microsoft stack.</remarks>
        public bool SkipServicesPage
        {
            get
            {
                return dialog.SkipServicesPage;
            }
            set
            {
                dialog.SkipServicesPage = value;
            }
        }
#endif



        /// <summary>
    /// Gets the selected Bluetooth device.
    /// </summary>
    public BluetoothDeviceInfo SelectedDevice
    {
      get
      {
        return device;
      }
    }

        

    /// <summary>
    /// If TRUE, authenticated devices are shown in the picker.
    /// </summary>
    public bool ShowAuthenticated
    {
      get
      {
        return dialog.ShowAuthenticated;
      }
      set
      {
                dialog.ShowAuthenticated = value;
            }
    }


    /// <summary>
    /// If TRUE, remembered devices are shown in the picker.
    /// </summary>
    public bool ShowRemembered
    {
      get
      {
                return dialog.ShowRemembered;
      }
      set
      {
                dialog.ShowRemembered = value;
      }
    }

    /// <summary>
    /// If TRUE, unknown devices are shown in the picker.
    /// </summary>
    public bool ShowUnknown
    {
      get
      {
                return dialog.ShowUnknown;
      }
      set
      {
                dialog.ShowUnknown = value;
      }
    }

        /// <summary>
        /// If TRUE, forces authentication before returning.
        /// </summary>
        /// <remarks></remarks>
        public bool ForceAuthentication
        {
            get
            {
                return dialog.ForceAuthentication;
            }
            set
            {
                dialog.ForceAuthentication = value;
            }
        }

  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.