SelectBluetoothDeviceForm.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 » SelectBluetoothDeviceForm.cs
// 32feet.NET - Personal Area Networking for .NET
//
// InTheHand.Windows.Forms.SelectBluetoothDeviceDialogForm
// 
// Copyright (c) 2003-2008 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.Drawing;
using System.Collections;
#if NETCF
using InTheHand.ComponentModel;
#else
using System.ComponentModel;
#endif
using System.Net.Sockets;
using System.Windows.Forms;
using InTheHand.Net.Sockets;
using InTheHand.Net.Bluetooth;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace InTheHand.Windows.Forms{
  /// <summary>
  /// Managed code dialog for Windows CE systems.
  /// </summary>
    internal sealed class SelectBluetoothDeviceForm : System.Windows.Forms.Form, ISelectBluetoothDevice
  {
#if NETCF
        private const int LVS_EX_GRADIENT = 0x20000000;
        private const int LVM_SETEXTENDEDLISTVIEWSTYLE = 0x1036;
        //private const int GWL_EXSTYLE = -20;

        [DllImport("coredll")]
        private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        //private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

        //[DllImport("coredll")]
        //private static extern int GetWindowLong(IntPtr hWnd, int nIndex); 
#endif


    private System.Windows.Forms.MainMenu mainMenu1;
    private System.Windows.Forms.MenuItem mnuMenu;
    private System.Windows.Forms.MenuItem mnuSelect;
    private System.Windows.Forms.MenuItem mnuAgain;
    private System.Windows.Forms.ListView lvDevices;
    private System.Windows.Forms.MenuItem mnuCancel;

    public BluetoothDeviceInfo selectedDevice;
    private System.Windows.Forms.ColumnHeader clmDevice;
        bool fShowAuthenticated, fShowRemembered, fShowUnknown, fForceAuthentication;
        //private BLUETOOTH_SELECT_DEVICE_PARAMS selectDeviceParams;
    
    private BackgroundWorker bkSearch;
  
    public SelectBluetoothDeviceForm(/*BLUETOOTH_SELECT_DEVICE_PARAMS p*/)
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
#if ! WinCE
            this.Size = new Size(400, 250);
#endif
            //selectDeviceParams = p;

      bkSearch = new BackgroundWorker();
      bkSearch.DoWork+=new DoWorkEventHandler(bkSearch_DoWork);
            bkSearch.WorkerReportsProgress = true;
            bkSearch.ProgressChanged += new ProgressChangedEventHandler(bkSearch_ProgressChanged);
      bkSearch.RunWorkerCompleted+=new RunWorkerCompletedEventHandler(bkSearch_RunWorkerCompleted);

            fShowAuthenticated = true;
            fShowRemembered = true;
            fShowUnknown = true;
            fForceAuthentication = false;

            
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
            System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem();
            this.lvDevices = new System.Windows.Forms.ListView();
            this.clmDevice = new System.Windows.Forms.ColumnHeader();
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.mnuSelect = new System.Windows.Forms.MenuItem();
            this.mnuMenu = new System.Windows.Forms.MenuItem();
            this.mnuAgain = new System.Windows.Forms.MenuItem();
            this.mnuCancel = new System.Windows.Forms.MenuItem();
            // 
            // lvDevices
            // 
            this.lvDevices.Columns.Add(this.clmDevice);
            this.lvDevices.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
            listViewItem1.Text = "Scanning for Bluetooth devices...";
            this.lvDevices.Items.Add(listViewItem1);
            this.lvDevices.Location = new System.Drawing.Point(8, 32);
            this.lvDevices.View = System.Windows.Forms.View.Details;
            this.lvDevices.ItemActivate += new System.EventHandler(this.deviceSelected);
            // 
            // clmDevice
            // 
            this.clmDevice.Text = "ColumnHeader";
            this.clmDevice.Width = 100;
            // 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.Add(this.mnuSelect);
            this.mainMenu1.MenuItems.Add(this.mnuMenu);
            // 
            // mnuSelect
            // 
            this.mnuSelect.Enabled = false;
            this.mnuSelect.Text = "Select";
            this.mnuSelect.Click += new System.EventHandler(this.deviceSelected);
            // 
            // mnuMenu
            // 
            this.mnuMenu.MenuItems.Add(this.mnuAgain);
            this.mnuMenu.MenuItems.Add(this.mnuCancel);
            this.mnuMenu.Text = "Menu";
            // 
            // mnuAgain
            // 
            this.mnuAgain.Enabled = false;
            this.mnuAgain.Text = "Search Again";
            this.mnuAgain.Click += new System.EventHandler(this.mnuAgain_Click);
            // 
            // mnuCancel
            // 
            this.mnuCancel.Text = "Cancel";
            this.mnuCancel.Click += new System.EventHandler(this.mnuCancel_Click);
            // 
            // SelectBluetoothDeviceForm
            // 
            this.ClientSize = new System.Drawing.Size(634, 671);
            this.ControlBox = false;
            this.Controls.Add(this.lvDevices);
            this.Menu = this.mainMenu1;
            this.Text = "Bluetooth";
            this.Resize += new System.EventHandler(this.SelectBluetoothDeviceForm_Resize);
            this.Load += new System.EventHandler(this.SelectBluetoothDeviceForm_Load);

    }
    #endregion

    private void mnuCancel_Click(object sender, System.EventArgs e)
    {
      this.DialogResult = DialogResult.Cancel;
    }

    private void SelectBluetoothDeviceForm_Load(object sender, System.EventArgs e)
    {
            Start();
        }

        void Start()
        {
      Cursor.Current = Cursors.WaitCursor;
            lvDevices.Items.Clear();
            this.mnuAgain.Enabled = false;
      bkSearch.RunWorkerAsync();
    }

    private void SelectBluetoothDeviceForm_Resize(object sender, System.EventArgs e)
    {
#if NETCF
#if !V1
            //do gradient view
            SendMessage(lvDevices.Handle, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_GRADIENT, LVS_EX_GRADIENT);
#endif

      //set layout dynamically
      this.Bounds = new Rectangle(0,0,Screen.PrimaryScreen.WorkingArea.Width,Screen.PrimaryScreen.WorkingArea.Height);
#endif
            //fill the panel
      lvDevices.Bounds = new Rectangle(-1,-1,this.Width+2,this.Height+2);
      lvDevices.Columns[0].Width = this.Width;
    }

    private void bkSearch_DoWork(object sender, DoWorkEventArgs e)
    {
      BluetoothClient bc = new BluetoothClient();
            BluetoothDeviceInfo[] devicesR, devicesD;
            if (fShowAuthenticated || fShowRemembered) {
                devicesR = bc.DiscoverDevices(255, fShowAuthenticated, fShowRemembered, false);
            } else {
                devicesR = new BluetoothDeviceInfo[0];
            }
            bkSearch.ReportProgress(-1, devicesR);
            //
            if (fShowUnknown) {
                devicesD = bc.DiscoverDevices(255, false, false, true);
            } else {
                devicesD = new BluetoothDeviceInfo[0];
            }
            e.Result = devicesD;
      bc.Dispose();
    }

        void bkSearch_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            object list = e.UserState;
            BluetoothDeviceInfo[] newDevices = (BluetoothDeviceInfo[])list;
            AppendDevices(newDevices);
        }

        private void bkSearch_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
            // See if the background worker method failed
            if (e.Error != null) {
#if DEBUG
                MessageBox.Show("e.Error is set, must have been an exception!");
#endif
                System.Diagnostics.Debug.Assert(e.Error as PlatformNotSupportedException != null,
                    "When devices=null, expect PlatformNotSupportedException.");
                System.Diagnostics.Debug.Assert(e.Error.InnerException as SocketException != null,
                    "When devices=null, expect innerException SocketException.");
                ListViewItem lvi = new ListViewItem("Failed: " + e.Error);
                lvDevices.Items.Add(lvi);
            } else {
                BluetoothDeviceInfo[] newDevices = (BluetoothDeviceInfo[])e.Result;
                if (newDevices == null) {
#if DEBUG
                    MessageBox.Show("devices is null, must have been an exception!");
#endif
                }
                AppendDevices(newDevices);
            }

      mnuAgain.Enabled = true;

      lvDevices.Focus();

      Cursor.Current = Cursors.Default;
        }



        void AppendDevices(BluetoothDeviceInfo[] newDevices)
        {
            if (newDevices != null) {
                foreach (BluetoothDeviceInfo bdi in newDevices) {
                    BdiListViewItem lvi = new BdiListViewItem(bdi);
                    lvDevices.Items.Add(lvi);
                }
                //
                if (lvDevices.Items.Count > 0) {
                    mnuSelect.Enabled = true;
                }
            }
    }

        class BdiListViewItem : ListViewItem
        {
            readonly BluetoothDeviceInfo m_bdi;

            public BdiListViewItem(BluetoothDeviceInfo device)
                :base(device.DeviceName)
            {
                m_bdi = device;
            }

            public BluetoothDeviceInfo Device { get { return m_bdi; } }
        }

    private void deviceSelected(object sender, System.EventArgs e)
    {
      if(lvDevices.SelectedIndices.Count > 0 && lvDevices.SelectedIndices[0] > -1 && lvDevices.SelectedIndices[0] < lvDevices.Items.Count )
      {
                ListViewItem item0 = lvDevices.Items[lvDevices.SelectedIndices[0]];
                BdiListViewItem item = item0 as BdiListViewItem;
                Debug.Assert(item != null, "non BdiListViewItem selected!!");
                if (item != null) {
                    selectedDevice = ((BdiListViewItem)item0).Device;

                    if (fForceAuthentication)
                    {
                        //authenticate with the selected device
                    }

          this.DialogResult = DialogResult.OK;
                }
      }
    
    }

    private void mnuAgain_Click(object sender, System.EventArgs e)
    {
            Start();
    }

        public override void Refresh()
        {
            lvDevices.Items.Clear();
            fShowAuthenticated = true;
            fShowUnknown = true;
            fForceAuthentication = false;
            base.Refresh();
        }

        #region ISelectBluetoothDevice Members
        void ISelectBluetoothDevice.Reset()
        {
            this.Refresh();
        }

        bool InTheHand.Windows.Forms.ISelectBluetoothDevice.ShowAuthenticated
        {
            get { return fShowAuthenticated; }
            set { fShowAuthenticated = value; }
        }

        bool InTheHand.Windows.Forms.ISelectBluetoothDevice.ShowRemembered
        {
            get { return fShowRemembered; }
            set { fShowRemembered = value; }
        }

        bool InTheHand.Windows.Forms.ISelectBluetoothDevice.ShowUnknown
        {
            get { return fShowUnknown; }
            set { fShowUnknown = value; }
        }

        bool InTheHand.Windows.Forms.ISelectBluetoothDevice.ForceAuthentication
        {
            get { return fForceAuthentication; }
            set { fForceAuthentication = value; }
        }

#if WinXP
        string InTheHand.Windows.Forms.ISelectBluetoothDevice.Info
        {
            get { return string.Empty; }
            set { throw new NotImplementedException("The method or operation is not implemented."); }
        }

        bool InTheHand.Windows.Forms.ISelectBluetoothDevice.AddNewDeviceWizard
        {
            get { return false; }
            set { throw new NotImplementedException("The method or operation is not implemented."); }
        }

        bool InTheHand.Windows.Forms.ISelectBluetoothDevice.SkipServicesPage
        {
            get { return true; }
            set { throw new NotImplementedException("The method or operation is not implemented."); }
        }
#endif
        #endregion
    }
}


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