Exemplo n.º 1
0
        /// <summary>
        /// Select the bluetooth device which will be used for lock checking
        /// </summary>
        private static void DevicePicker()
        {
            var devices = BtDeviceScanner.GetDeviceList();

            if (devices.Length == 0) // No paired devices were found, give the user some debug tips and exit
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("\nNo paired devices were found.\n");
                Console.ResetColor();
                Console.WriteLine("Follow these steps, to see if they can solve the problem: ");
                Console.WriteLine(" - Check if your computer supports bluetooth, or if it has a bluetooth adapter connected to it.");
                Console.WriteLine(" - Check if you have correct and up-to-date drivers installed for your bluetooth adapter.");
                Console.WriteLine(" - Make sure the the device is paired with your computer and is visible in the list of paired devices, in your bluetooth management app.");
                Console.WriteLine("\nAfter you followed the steps above, restart BlueLock let it scan for paired devices once more.\n");
                Console.WriteLine("Press enter to exit BlueLock...");
                Console.ReadLine();
                return;
            }

            Console.WriteLine("\nI managed to find {0} paired device/s: \n", devices.Length);

            Console.ForegroundColor = ConsoleColor.Green;


            Console.WriteLine("ID | NAME | LAST SEEN");

            Console.ForegroundColor = ConsoleColor.Cyan;

            var index = 0;

            foreach (var device in devices) // List all found devices so the user can select which one he wants to use
            {
                Console.WriteLine("{0} | {1} | {2}", ++index, device.DeviceName, device.LastSeen);
            }

            Console.ResetColor();

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("\n Write the ID of the device you want to use:");
            Console.ResetColor();

            var deviceIdText = Console.ReadLine();

            int deviceId;

            while (!Int32.TryParse(deviceIdText, out deviceId) || deviceId > devices.Length) // This allows the user to select which device he wants to use and it makes sure the selected device exists
            {
                Console.WriteLine("The inserted ID is invalid, try again...");
                deviceIdText = Console.ReadLine();
            }

            var selectedDevice = devices[deviceId - 1];

            // Save the selected device info into the settings file, so we can load it after app launch
            Properties.Settings.Default["LockDeviceName"]   = selectedDevice.DeviceName;
            Properties.Settings.Default["LockDeviceAdress"] = selectedDevice.DeviceAddress.ToInt64();


            Console.WriteLine("\nYou have selected the {0}: {1} as a locking device. \nI will now lock this computer if I detect that {1} isn't within range.\n", deviceId, selectedDevice.DeviceName);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Check if the lock device is within range and if not, lock the PC
        /// </summary>
        private static void LockCheck()
        {
            if (!BtDeviceScanner.IsDeviceInRange(new BluetoothAddress(Properties.Settings.Default.LockDeviceAdress)))
            {
                if ((DateTime.Now - _lastTimeLockDeviceInRange).TotalSeconds > Properties.Settings.Default.LockTimeout) // Even though the device is not in range, don't lock the computer until it is not in range for the lock timeout amount of seconds
                {
                    Console.WriteLine("Locking PC.");

                    try
                    {
                        Locker.LockWorkStation();
                    }
                    catch
                    {
                        // ignored
                    }
                }
            }
            else
            {
                _lastTimeLockDeviceInRange = DateTime.Now;
            }

            _lockCheckTimer.Start(); // Wait for the device check to finish and then reset the timer
        }