Load() public static method

public static Load ( string fileName ) : DeviceConfiguration
fileName string
return DeviceConfiguration
Example #1
0
        //This application allows two computers, each equipped with a USB Bluetooth adapter, to sit between
        //  the connection for two Bluetooth devices:
        //    Bluetooth Host <-> PC1 <-> PC2 <-> Bluetooth Peripheral
        //The PCs can log communication between the Bluetooth devices, extend the range, or
        //  whatever else you can think of.
        //Each PC requires knowledge of where the other one is on the network, as well as information
        //  about both the Bluetooth device it's "emulating" AND the one it's talking to.
        //It stores information about the device it's "emulating" in a file called emulated.txt.
        //It stores information about the device it's actually talking to in a file called real.txt.
        //When running it the first time, it will perform an inquiry scan to find each device;
        //  you select the device from a list, and it will save the information to the text file.
        //Parameters:
        //  /vid=[USB vendor ID of the USB Bluetooth adapter to use]
        //  /pid=[USB product ID of the USB Bluetooth adapter to use]
        //  /buddy=[IP address/hostname of the other PC]
        //  /inport=[TCP port number for incoming connections; should match other PC's outport]
        //  /outport=[TCP port number for outgoing connections; should match other PC's inport]
        //Remember to install the LibUsbDotNet filter driver for the USB Bluetooth adapter as well.
        //  You should probably just install a generated libusb-win32 driver instead, so that Windows
        //    won't try to communicate with it at the same time.
        static void Main(string[] args)
        {
            try
            {
                //Parse command line arguments
                ushort?vid     = null;
                ushort?pid     = null;
                string buddy   = String.Empty;
                int?   inport  = null;
                int?   outport = null;
                foreach (var arg in args)
                {
                    if (arg.StartsWith("/") && arg.Contains("="))
                    {
                        var key   = arg.Substring(1, arg.IndexOf("=") - 1);
                        var value = arg.Substring(arg.IndexOf("=") + 1);

                        switch (key.ToLower())
                        {
                        case "vid":
                        {
                            vid = Convert.ToUInt16(value, 16);
                            break;
                        }

                        case "pid":
                        {
                            pid = Convert.ToUInt16(value, 16);
                            break;
                        }

                        case "inport":
                        {
                            inport = Convert.ToInt32(value);
                            break;
                        }

                        case "outport":
                        {
                            outport = Convert.ToInt32(value);
                            break;
                        }

                        case "buddy":
                        {
                            buddy = value;
                            break;
                        }

                        default:
                        {
                            Logger.WriteLine("Unknown command line argument: " + arg);
                            break;
                        }
                        }
                    }
                }

                //Validate parameters
                if (String.IsNullOrEmpty(buddy))
                {
                    throw new ArgumentException("No buddy name/IP specified");
                }
                if (!inport.HasValue || !outport.HasValue)
                {
                    throw new ArgumentException("No incoming/outgoing port(s) specified");
                }
                if (!vid.HasValue || !pid.HasValue)
                {
                    throw new ArgumentException("No vendor/product ID(s) specified");
                }

                //Initialize the USB Bluetooth adapter device
                Properties.Adapter = new USBBluetoothAdapter(vid.Value, pid.Value);
                Properties.Adapter.Open();

                //Make sure we have device configurations; get them if not
                if (!File.Exists(REAL_CONFIG_FILENAME))
                {
                    if (!_CreateConfiguration(REAL_CONFIG_FILENAME))
                    {
                        return;
                    }
                }
                if (!File.Exists(EMULATED_CONFIG_FILENAME))
                {
                    if (!_CreateConfiguration(EMULATED_CONFIG_FILENAME))
                    {
                        return;
                    }
                }

                //Retrieve device configurations
                Properties.RealConfiguration     = DeviceConfiguration.Load(REAL_CONFIG_FILENAME);
                Properties.EmulatedConfiguration = DeviceConfiguration.Load(EMULATED_CONFIG_FILENAME);

                //Set ourselves as the device from the configuration file
                Properties.Adapter.SetLocalName(Properties.EmulatedConfiguration.RemoteName);
                Properties.Adapter.SetDeviceClass(Properties.EmulatedConfiguration.DeviceClass);

                //Set ourselves discoverable
                Properties.Adapter.SetDiscoverableMode(true);

                //Set up the server for talking to the other PC
                Properties.Server = new BloxyServer(buddy, inport.Value, outport.Value);
                Properties.Server.Start();

                //Main loop...
                Logger.WriteLine("Waiting for event...");
                while (true)
                {
                    if (Console.KeyAvailable)
                    {
                        if (Console.ReadKey(true).Key == ConsoleKey.Escape)
                        {
                            break;
                        }
                    }

                    Thread.Sleep(100);
                }

                //Clean up
                Properties.Server.Stop();
                Logger.WriteLine("Done.");
            }
            catch (Exception ex)
            {
                Logger.WriteLine("ERROR: " + ex.ToString());
            }
            finally
            {
                try
                {
                    //More cleanup...
                    if (Properties.Adapter != null)
                    {
                        Properties.Adapter.Close();
                    }
                }
                catch
                {
                    //Whatever...
                }
            }
        }