Example #1
0
        public static void UnpairByName(string name, int discoveryTime, Utils.DeviceType deviceType)
        {
            var devices = DeviceFinder.FindDevicesByName(DeviceDiscoverer.DiscoverPairedBluetoothDevices(discoveryTime), name, deviceType);

            if (devices.Count > 1)
            {
                throw new Exception($"{devices.Count} devices found, don't know which one to choose");
            }

            DevicePairer.UnpairDevice(devices[0]);
        }
Example #2
0
        /*
         * public static void Execute(PairDeviceOptions opts)
         * {
         *  if (!string.IsNullOrEmpty(opts.Mac))
         *  {
         *      PairWithMac(new MacAddress(opts.Mac), opts.DiscoveryTime, opts.Type, opts.Pin);
         *  }
         *  else if (!string.IsNullOrEmpty(opts.DeviceName))
         *  {
         *      PairWithName(opts.DeviceName, opts.DiscoveryTime, opts.Type, opts.Pin);
         *  }
         *  else
         *  {
         *      throw new Exception("Mac or device name must be specified");
         *  }
         * }
         */

        private static async void PairWithMac(MacAddress mac, int discoveryTime, Utils.DeviceType deviceType, string pin)
        {
            String deviceId = $"Bluetooth#Bluetootha8:6d:aa:5f:35:51-{mac}";
            IAsyncOperation <DeviceInformation> devInfoTask =
                DeviceInformation.CreateFromIdAsync(deviceId);

            Console.WriteLine($"Paring with Device deviceId-{deviceId} Step 1");

            DeviceInformation devInfo = await devInfoTask;

            Console.WriteLine($"Paring with Device deviceId-{deviceId} Step 2");

            Device device = new Device(devInfo);

            DevicePairer.PairDevice(device, pin);
            return;
        }
Example #3
0
        /*
         * private static void PairWithMac(MacAddress mac, int discoveryTime, Utils.DeviceType deviceType, string pin)
         * {
         *  var devices = DeviceFinder.FindDevicesByMac(DeviceDiscoverer.DiscoverBluetoothDevices(discoveryTime), mac, deviceType);
         *
         *  if (devices.Count == 1)
         *  {
         *      DevicePairer.PairDevice(devices[0], pin);
         *      return;
         *  }
         *
         *  if (devices.Count == 2)
         *  {
         *      throw new Exception(
         *          $"2 devices with the mac '{mac}' found \"{devices[0]}\" and \"{devices[1]}\". Don't know which one to choose.\n");
         *  }
         *
         *  throw new Exception(
         *      $"{devices.Count} devices with the mac '{mac}' found. Don't know which one to choose");
         * }
         */

        private static void PairWithName(string name, int discoveryTime, Utils.DeviceType deviceType, string pin)
        {
            var devices = DeviceFinder.FindDevicesByName(DeviceDiscoverer.DiscoverBluetoothDevices(discoveryTime), name, deviceType);

            if (devices.Count == 1)
            {
                DevicePairer.PairDevice(devices[0], pin);
                return;
            }

            if (devices.Count == 2 && devices[0].Type == Bluetooth.DeviceType.BluetoothLE && devices[1].Type == Bluetooth.DeviceType.BluetoothLE)
            {
                HandleSituation_2_BluetoothLe_devices_with_the_same_name_found(devices[0], devices[1], pin);
                return;
            }

            throw new Exception($"{devices.Count} devices with the name '{name}' found. Don't know which one to choose");
        }
        private static void PairWithMac(MacAddress mac, int discoveryTime, Utils.DeviceType deviceType, string pin)
        {
            var devices = DeviceFinder.FindDevicesByMac(DeviceDiscoverer.DiscoverBluetoothDevices(discoveryTime), mac, deviceType);

            if (devices.Count == 1)
            {
                DevicePairer.PairDevice(devices[0], pin);
                return;
            }

            if (devices.Count == 2)
            {
                throw new Exception(
                          $"2 devices with the mac '{mac}' found \"{devices[0]}\" and \"{devices[1]}\". Don't know which one to choose.\n");
            }

            throw new Exception(
                      $"{devices.Count} devices with the mac '{mac}' found. Don't know which one to choose");
        }
        private static async void PairWithMac(MacAddress mac, int discoveryTime, Utils.DeviceType deviceType, string pin)
        {
            BluetoothAdapter defaultBtAdapter = await BluetoothAdapter.GetDefaultAsync();

            string localBtAddress = string.Join(":",
                                                BitConverter.GetBytes(defaultBtAdapter.BluetoothAddress).Reverse().Select(b => b.ToString("X2"))).Substring(6).ToLower();

            string deviceId = $"Bluetooth#Bluetooth{localBtAddress}-{mac}";

            try
            {
                DeviceInformation devInfo = await DeviceInformation.CreateFromIdAsync(deviceId);;
                Console.WriteLine($"Paring with Device deviceId-{deviceId}");

                Device device = new Device(devInfo);
                DevicePairer.PairDevice(device, "0000");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception: {ex.StackTrace}");
            }
            return;
        }