Ejemplo n.º 1
0
        public bool Initialize()
        {
            var conn  = new Connection();
            var disco = new Discovery();
            //_log.Trace("Discovering...");
            var devices = disco.DiscoverDevices();

            if (!devices.Any())
            {
                //_log.Error("No BT devices discovered");
                throw new NoDiscoveredDevicesException("No Bluetooth devices found");
            }

            var disc       = default(BluetoothDeviceInfo);
            var targetName = ConfigurationManager.AppSettings["TargetName"];

            if (!devices.Where(d => d.DeviceName.Equals(targetName)).Any())
            {
                foreach (var device in devices)
                {
                    if (disco.GetDeviceName(device).Equals(targetName))
                    {
                        disc = device;
                    }
                }
            }
            else
            {
                disc = devices.Where(d => d.DeviceName.Equals(targetName)).First();
            }
            if (disc == default(BluetoothDeviceInfo))
            {
                throw new NoTargetFoundException("No device found");
            }

            // _log.Trace("Connecting");
            try
            {
                conn.Connect(disc);
            }
            catch (Exception e)
            {
                //_log.ErrorException("Can't connect to disc", e);
                throw new ConnectionFailedException("Failed to connect" + e);
            }

            return(true);
        }