SubMenu_BtWin32Auth.cs :  » Business-Application » 32feet.NET » ConsoleMenuTesting » 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 » ConsoleMenuTesting » SubMenu_BtWin32Auth.cs
using System;
using System.Collections.Generic;
using System.Text;
using InTheHand.Net.Bluetooth;

namespace ConsoleMenuTesting{
    partial class BluetoothTesting
    {
#if !NETCF
        [MenuItem, SubMenu("Win32Auth")]
        public void BtWin32Auth()
        {
            using (BluetoothWin32Authentication ar = new BluetoothWin32Authentication(Hndlr)) {
                Console.Write("Hit return to break>");
                Console.ReadLine();
            }
        }

        //[MenuItem]
        //public void BtWin32AuthEx()
        //{
        //    using (BluetoothWin32AuthenticationEx ar = new BluetoothWin32AuthenticationEx(Hndlr)) {
        //        Console.Write("Hit return to break>");
        //        Console.ReadLine();
        //    }
        //}

        void Hndlr(object sender, BluetoothWin32AuthenticationEventArgs e)
        {
            Console.WriteLine("Hndlr!!!!!!!!!!!!");
        }

        //----
        [MenuItem, SubMenu("Win32Auth")]
        public void Win32AuthCallbackTwoSeparateAuthentications()
        {
            Console.WriteLine("Authenticate two devices");
            Console.WriteLine("Passcode respectively: '{0}', '{1}', '{2}'",
                "1234", "9876", "sdfghjkl");
            Win32AuthCallback__(Win32AuthCallbackTwoSeparateAuthenticationsHandler);
        }

        Int32 Win32AuthCallbackTwoSeparateAuthentications_count;

        void Win32AuthCallbackTwoSeparateAuthenticationsHandler(Object sender, InTheHand.Net.Bluetooth.BluetoothWin32AuthenticationEventArgs e)
        {
            Console.WriteLine("Authenticating {0} {1}", e.Device.DeviceAddress, e.Device.DeviceName);
            Console.WriteLine("  Attempt# {0}, Last error code {1}", e.AttemptNumber, e.PreviousNativeErrorCode);
            //
            if (Win32AuthCallbackTwoSeparateAuthentications_count == 0) {
                e.Pin = "1234";
            } else if (Win32AuthCallbackTwoSeparateAuthentications_count == 1) {
                e.Pin = "9876";
            } else if (Win32AuthCallbackTwoSeparateAuthentications_count == 2) {
                e.Pin = "sdfghjkl";
            }
            Console.WriteLine("Using '{0}'", e.Pin);
            Win32AuthCallbackTwoSeparateAuthentications_count += 1;
        }

        void Win32AuthCallbackJohoHandler(Object sender, InTheHand.Net.Bluetooth.BluetoothWin32AuthenticationEventArgs e)
        {
            String address = e.Device.DeviceAddress.ToString();
            Console.WriteLine("Received an authentication request from address " + address);

            //compare the first 8 hex numbers, this is just a special case because in the
            //used scenario the model of the devices can be identified by the first 8 hex
            //numbers, the last 4 numbers being the device specific part.
            if (address.Substring(0, 8).Equals("0099880D")
                    || address.Substring(0, 8).Equals("0099880E")) {
                //send authentication response
                e.Pin = "5276";
            } else if (address.Substring(0, 8).Equals("00997788")) {
                //send authentication response
                e.Pin = "1423";
            }
        }


        //The output of a standard run is the following.  Note that the error code from
        //the initial wrong response is error code 1244 which is ErrorNotAuthenticated.
        //However when we try another attempt we get error code 1167 which is
        //ErrorDeviceNotConnected.  So for the devices I've tested it's apparently not
        //worth trying a second response.  If the user attempts to send another PIN-
        //response (by setting the Pin field in the event args) then it will be ignored
        //(we probably should have the event args throw when Pin is set in that case).
        //
        //[[
        //Local radio address is 008099244999
        //Authenticate two devices
        //Passcode respectively: '1234', '9876', 'sdfghjkl'
        //Making PC discoverable
        //Hit Return to complete
        //
        //Complete
        //Authenticate one device -- with wrong passcode here the first two times.
        //Passcode respectively: 'BAD-x', 'BAD-y', '9876'
        //Making PC discoverable
        //Hit Return to complete
        //Authenticating 000A99686999 Win2kBelkin
        //  Attempt# 0, Last error code 0
        //Using '0.88516242889927'
        //Authenticating 000A99686999 Win2kBelkin
        //  Attempt# 1, Last error code 1244
        //Using '0.59947050996100'
        //Authenticating 000A99686999 Win2kBelkin
        //  Attempt# 2, Last error code 1167
        //Using '9876'
        //]]
        [MenuItem, SubMenu("Win32Auth")]
        void Win32AuthCallbackInitialBadPasscodeAndRetry()
        {
            Console.WriteLine("Authenticate one device -- with wrong passcode here the first two times.");
            Console.WriteLine("Passcode respectively: '{0}', '{1}', '{2}'",
                "BAD-x", "BAD-y", "9876");
            Win32AuthCallback__(Win32AuthCallbackInitialBadPasscodeAndRetryHandler);
        }


        void Win32AuthCallback__(System.EventHandler<BluetoothWin32AuthenticationEventArgs> handler)
        {
            Console.WriteLine("Making PC discoverable");
            InTheHand.Net.Bluetooth.BluetoothRadio radio = InTheHand.Net.Bluetooth.BluetoothRadio.PrimaryRadio;
            InTheHand.Net.Bluetooth.RadioMode origRadioMode = radio.Mode;
            radio.Mode = InTheHand.Net.Bluetooth.RadioMode.Discoverable;
            //
            using (InTheHand.Net.Bluetooth.BluetoothWin32Authentication auther
                    = new InTheHand.Net.Bluetooth.BluetoothWin32Authentication(handler)) {
                Console.WriteLine("Hit Return to complete");
                Console.ReadLine();
                Console.WriteLine("Complete");
            }
            radio.Mode = origRadioMode;
        }

        int Win32AuthCallbackInitialBadPasscodeAndRetry_count;

        void Win32AuthCallbackInitialBadPasscodeAndRetryHandler(Object sender, InTheHand.Net.Bluetooth.BluetoothWin32AuthenticationEventArgs e)
        {
            Console.WriteLine("Authenticating {0} {1}", e.Device.DeviceAddress, e.Device.DeviceName);
            Console.WriteLine("  Attempt# {0}, Last error code {1}", e.AttemptNumber, e.PreviousNativeErrorCode);
            if (e.AttemptNumber != Win32AuthCallbackInitialBadPasscodeAndRetry_count) {
                Console.WriteLine("Bad AttemptNumber!!!");
            }
            Random rnd = new Random();
            String badPasscode = rnd.NextDouble().ToString();
            badPasscode = badPasscode.Substring(0, Math.Min(16, badPasscode.Length));
            //
            if (Win32AuthCallbackInitialBadPasscodeAndRetry_count == 0
                    || Win32AuthCallbackInitialBadPasscodeAndRetry_count == 1) {
                e.Pin = badPasscode;
                e.CallbackWithResult = true;
            } else if (Win32AuthCallbackInitialBadPasscodeAndRetry_count == 2
                    || Win32AuthCallbackInitialBadPasscodeAndRetry_count == 3) {
                e.Pin = "9876";
                e.CallbackWithResult = true;
            } else if (Win32AuthCallbackInitialBadPasscodeAndRetry_count == 4) {
                e.CallbackWithResult = true; //Try to *wrongly* get another callback!
            } else {
                Console.WriteLine("Unexpected callback #{0}", Win32AuthCallbackInitialBadPasscodeAndRetry_count);
            }
            Console.WriteLine("Using '{0}'", (e.Pin == null ? "<null>" : e.Pin));
            Win32AuthCallbackInitialBadPasscodeAndRetry_count += 1;
        }


        [MenuItem, SubMenu("Win32Auth")]
        void Win32AuthCallbackInduceFinalisationFault()
        {
            Console.WriteLine("Authenticate a device two times");
            Console.WriteLine("Making PC discoverable");
            InTheHand.Net.Bluetooth.BluetoothRadio radio = InTheHand.Net.Bluetooth.BluetoothRadio.PrimaryRadio;
            InTheHand.Net.Bluetooth.RadioMode origRadioMode = radio.Mode;
            radio.Mode = InTheHand.Net.Bluetooth.RadioMode.Discoverable;
            //
            Object auther;
            auther = new InTheHand.Net.Bluetooth.BluetoothWin32Authentication(Win32AuthCallbackTwoSeparateAuthenticationsHandler);
            Console.WriteLine("Hit Return to clear and GC");
            Console.ReadLine();
            auther = "foo";
            GC.Collect();
            Console.WriteLine("Hit Return to complete");
            Console.ReadLine();
            Console.WriteLine("Complete");
            radio.Mode = origRadioMode;
        }

#endif

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