diff --git a/README.md b/README.md index 3d0a19a..19a1741 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,10 @@ BluetoothDevicePairing.exe unpair-by-mac --mac 12:34:56:78:9A:BC --type Bluetoot ``` BluetoothDevicePairing.exe unpair-by-name --name "MX Ergo" --type BluetoothLE ``` +* List all Bluetooth adapters available you your machine +``` +BluetoothDevicePairing.exe list-adapters +``` # How it works The program uses [Windows.Devices.Enumeration API](https://docs.microsoft.com/en-us/uwp/api/Windows.Devices.Enumeration?redirectedfrom=MSDN&view=winrt-22000) to work with Bluetooth. diff --git a/src/Bluetooth/Adapters/Adapter.cs b/src/Bluetooth/Adapters/Adapter.cs index 5612e88..94db5e6 100644 --- a/src/Bluetooth/Adapters/Adapter.cs +++ b/src/Bluetooth/Adapters/Adapter.cs @@ -20,7 +20,7 @@ public Adapter(Windows.Devices.Enumeration.DeviceInformation bluetoothAdapterInf adapterDevice = Windows.Devices.Bluetooth.BluetoothAdapter.FromIdAsync(bluetoothAdapterInfo.Id).GetAwaiter().GetResult(); radio = adapterDevice.GetRadioAsync().GetAwaiter().GetResult(); MacAddress = new AdapterMacAddress(adapterDevice); - IsDefault = MacAddress.Equals(defaultAdapterMacAddress); + IsDefault = MacAddress.RawAddess == defaultAdapterMacAddress.RawAddess; } public override string ToString() diff --git a/src/Bluetooth/Adapters/AdapterMacAddress.cs b/src/Bluetooth/Adapters/AdapterMacAddress.cs index 9113801..c04f4eb 100644 --- a/src/Bluetooth/Adapters/AdapterMacAddress.cs +++ b/src/Bluetooth/Adapters/AdapterMacAddress.cs @@ -1,49 +1,13 @@ -using System; -using System.Linq; -using Windows.Devices.Bluetooth; - namespace BluetoothDevicePairing.Bluetooth.Adapter { - internal sealed class AdapterMacAddress : IEquatable + internal sealed class AdapterMacAddress : MacAddress { - public string Address { get; } - - public AdapterMacAddress(BluetoothAdapter adapter) - { - Address = string.Join(":", BitConverter.GetBytes(adapter.BluetoothAddress) - .Reverse() - .Skip(2) - .Select(b => b.ToString("x2"))); - } - - public override string ToString() - { - return Address; - } - - public bool Equals(AdapterMacAddress other) - { - if (other is null) - { - return false; - } - - if (ReferenceEquals(this, other)) - { - return true; - } - - return Address == other.Address; - } - - public override bool Equals(object obj) + public AdapterMacAddress(Windows.Devices.Bluetooth.BluetoothAdapter adapter) : base(adapter.BluetoothAddress) { - return ReferenceEquals(this, obj) || obj is AdapterMacAddress other && Equals(other); } - public override int GetHashCode() + public AdapterMacAddress(string mac) : base(mac) { - return Address != null ? Address.GetHashCode() : 0; } } } diff --git a/src/Bluetooth/Devices/BluetoothDevice.cs b/src/Bluetooth/Devices/BluetoothDevice.cs new file mode 100644 index 0000000..00a054f --- /dev/null +++ b/src/Bluetooth/Devices/BluetoothDevice.cs @@ -0,0 +1,25 @@ +using System; + +namespace BluetoothDevicePairing.Bluetooth.Devices +{ + internal sealed class BluetoothDevice : Device + { + public readonly Windows.Devices.Bluetooth.BluetoothDevice device; + protected override bool IsConnected => device.ConnectionStatus == Windows.Devices.Bluetooth.BluetoothConnectionStatus.Connected; + + private BluetoothDevice(Windows.Devices.Bluetooth.BluetoothDevice device) : base(device.DeviceInformation) + { + this.device = device; + } + + public static BluetoothDevice FromDeviceInfo(Windows.Devices.Enumeration.DeviceInformation info) + { + return new BluetoothDevice(Windows.Devices.Bluetooth.BluetoothDevice.FromIdAsync(info.Id).GetAwaiter().GetResult()); + } + + public static BluetoothDevice FromMac(DeviceMacAddress mac) + { + return new BluetoothDevice(Windows.Devices.Bluetooth.BluetoothDevice.FromBluetoothAddressAsync(mac.RawAddess).GetAwaiter().GetResult()); + } + } +} diff --git a/src/Bluetooth/Devices/BluetoothLeDevice.cs b/src/Bluetooth/Devices/BluetoothLeDevice.cs new file mode 100644 index 0000000..b3e9bd4 --- /dev/null +++ b/src/Bluetooth/Devices/BluetoothLeDevice.cs @@ -0,0 +1,30 @@ +using System; + +namespace BluetoothDevicePairing.Bluetooth.Devices +{ + internal class BluetoothLeDevice : Device + { + private readonly Windows.Devices.Bluetooth.BluetoothLEDevice device; + protected override bool IsConnected => device.ConnectionStatus == Windows.Devices.Bluetooth.BluetoothConnectionStatus.Connected; + + private BluetoothLeDevice(Windows.Devices.Bluetooth.BluetoothLEDevice device) : base(device.DeviceInformation) + { + this.device = device; + } + + public static BluetoothLeDevice FromDeviceInfo(Windows.Devices.Enumeration.DeviceInformation info) + { + return new BluetoothLeDevice(Windows.Devices.Bluetooth.BluetoothLEDevice.FromIdAsync(info.Id).GetAwaiter().GetResult()); + } + + public static BluetoothLeDevice FromMac(DeviceMacAddress mac) + { + var device = Windows.Devices.Bluetooth.BluetoothLEDevice.FromBluetoothAddressAsync(mac.RawAddess).GetAwaiter().GetResult(); + if(device == null) + { + throw new Exception($"Can't create a BluetoothLE device from the provided mac address '{mac}'. Device with this mac address doesn't exist"); + } + return new BluetoothLeDevice(device); + } + } +} diff --git a/src/Bluetooth/Devices/Device.cs b/src/Bluetooth/Devices/Device.cs index 4bfc974..18d91a0 100644 --- a/src/Bluetooth/Devices/Device.cs +++ b/src/Bluetooth/Devices/Device.cs @@ -1,79 +1,42 @@ -using System; -using System.Text.RegularExpressions; - namespace BluetoothDevicePairing.Bluetooth.Devices { + internal enum ConnectionStatus + { + NotPaired, + Paired, + Connected + } + internal enum DeviceType { Bluetooth, BluetoothLE } - internal sealed class Device + internal abstract class Device { private readonly Windows.Devices.Enumeration.DeviceInformation info; - private readonly Windows.Devices.Bluetooth.BluetoothDevice bluetoothDevice; - private readonly Windows.Devices.Bluetooth.BluetoothLEDevice bluetoothLeDevice; + protected abstract bool IsConnected { get; } - public string Id => info.Id; + public ConnectionStatus ConnectionStatus => + IsConnected + ? ConnectionStatus.Connected + : PairingInfo.IsPaired + ? ConnectionStatus.Paired + : ConnectionStatus.NotPaired; public Windows.Devices.Enumeration.DeviceInformationPairing PairingInfo => info.Pairing; - public bool IsPaired => info.Pairing.IsPaired; - public DeviceMacAddress Mac { get; } - public DeviceType Type { get; } + public DeviceInfoId Id { get; } public string Name => info.Name; - public bool IsConnected - { - get - { - if (bluetoothDevice != null) - { - return bluetoothDevice.ConnectionStatus == Windows.Devices.Bluetooth.BluetoothConnectionStatus.Connected; - } - else - { - return bluetoothLeDevice.ConnectionStatus == Windows.Devices.Bluetooth.BluetoothConnectionStatus.Connected; - } - } - } - public Device(Windows.Devices.Enumeration.DeviceInformation info) + protected Device(Windows.Devices.Enumeration.DeviceInformation info) { this.info = info; - Mac = new DeviceMacAddress(info); - Type = GetDeviceType(info); - if (Type == DeviceType.Bluetooth) - { - bluetoothDevice = Windows.Devices.Bluetooth.BluetoothDevice.FromIdAsync(info.Id).GetAwaiter().GetResult(); - } - else - { - bluetoothLeDevice = Windows.Devices.Bluetooth.BluetoothLEDevice.FromIdAsync(info.Id).GetAwaiter().GetResult(); - } + Id = new DeviceInfoId(info); } public override string ToString() { - return $"name:'{Name}' mac:'{Mac}' type:'{Type}' Connected:'{IsConnected}' Paired:'{IsPaired}'"; - } - - private static DeviceType GetDeviceType(Windows.Devices.Enumeration.DeviceInformation device) - { - var match = Regex.Match(device.Id, @"(^\w*)(#)"); - if (!match.Success) - { - throw new Exception($"Failed to extract the device type from the string '{device.Id}'"); - } - - var type = match.Groups[1].Value; - switch (type) - { - case "Bluetooth": - return DeviceType.Bluetooth; - case "BluetoothLE": - return DeviceType.BluetoothLE; - default: - throw new Exception($"Wrong device type '{type}' extracted from '{device.Id}'"); - } + return $"name:'{Name}' mac:'{Id.DeviceMac}' type:'{Id.DeviceType}' ConnectionStatus:'{ConnectionStatus}'"; } } } diff --git a/src/Bluetooth/Devices/DeviceDiscoverer.cs b/src/Bluetooth/Devices/DeviceDiscoverer.cs index ae05cb8..1d0ce1a 100644 --- a/src/Bluetooth/Devices/DeviceDiscoverer.cs +++ b/src/Bluetooth/Devices/DeviceDiscoverer.cs @@ -1,3 +1,4 @@ +using BluetoothDevicePairing.Bluetooth.Devices.Utils; using System; using System.Collections.Generic; using System.Linq; @@ -5,32 +6,49 @@ namespace BluetoothDevicePairing.Bluetooth.Devices { - internal static class DeviceDiscoverer + internal class DiscoveryTime { - public static List DiscoverBluetoothDevices(int timeoutInSec) + public int Seconds { get; } + + public DiscoveryTime(int timeInSeconds) { - return Discover(AsqFilter.BluetoothDevicesFilter(), timeoutInSec); + if (timeInSeconds < 1 || timeInSeconds > 30) + { + throw new Exception($"discovery time should be in range [1; 30] but was {timeInSeconds}"); + } + + Seconds = timeInSeconds; } + } - public static List DiscoverPairedBluetoothDevices(int timeoutInSec) + internal static class DeviceDiscoverer + { + public static List DiscoverBluetoothDevices(DiscoveryTime time) { - return Discover(AsqFilter.PairedBluetoothDevicesFilter(), timeoutInSec); + return Discover(AsqFilter.BluetoothDevicesFilter(), time); } - private static List Discover(AsqFilter filter, int discoveryTimeInSec) + public static List DiscoverPairedBluetoothDevices(DiscoveryTime time) { - Console.WriteLine($"Start discovering devices for {discoveryTimeInSec} seconds"); + return Discover(AsqFilter.PairedBluetoothDevicesFilter(), time); + } - if (discoveryTimeInSec < 1 || discoveryTimeInSec > 30) - { - throw new Exception($"discovery time should be in range [1; 30] but was {discoveryTimeInSec}"); - } + private static List Discover(AsqFilter filter, DiscoveryTime time) + { + Console.WriteLine($"Start discovering devices for {time.Seconds} seconds"); var watcher = new DeviceWatcher(filter); watcher.Start(); - Thread.Sleep(discoveryTimeInSec * 1000); + Thread.Sleep(time.Seconds * 1000); var devices = watcher.Stop(); - return devices.Select(d => new Device(d)).ToList(); + return devices.Select(info => CreateDevice(info)).ToList(); + } + + private static Device CreateDevice(Windows.Devices.Enumeration.DeviceInformation info) + { + return new DeviceInfoId(info).DeviceType == DeviceType.Bluetooth + ? BluetoothDevice.FromDeviceInfo(info) + : BluetoothLeDevice.FromDeviceInfo(info); } } } diff --git a/src/Bluetooth/Devices/DeviceInfoId.cs b/src/Bluetooth/Devices/DeviceInfoId.cs new file mode 100644 index 0000000..80086f7 --- /dev/null +++ b/src/Bluetooth/Devices/DeviceInfoId.cs @@ -0,0 +1,26 @@ +using BluetoothDevicePairing.Bluetooth.Adapter; +using System; +using System.Text.RegularExpressions; + +namespace BluetoothDevicePairing.Bluetooth.Devices +{ + internal sealed class DeviceInfoId + { + public DeviceType DeviceType { get; } + public AdapterMacAddress AdapterMac { get; } + public DeviceMacAddress DeviceMac { get; } + + public DeviceInfoId(Windows.Devices.Enumeration.DeviceInformation info) + { + var match = Regex.Match(info.Id, @"(^\w+)#(?Bluetooth|BluetoothLE)(?(..:){5}(..))-(?(..:){5}(..))$"); + if (!match.Success) + { + throw new Exception($"Failed to parse DeviceInformation.Id '{info.Id}'"); + } + + DeviceType = match.Groups["Type"].Value == "Bluetooth" ? DeviceType.Bluetooth : DeviceType.BluetoothLE; + AdapterMac = new AdapterMacAddress(match.Groups["AdapterMac"].Value); + DeviceMac = new DeviceMacAddress(match.Groups["DeviceMac"].Value); + } + } +} diff --git a/src/Bluetooth/Devices/DeviceMacAddress.cs b/src/Bluetooth/Devices/DeviceMacAddress.cs index e62612c..f1ef744 100644 --- a/src/Bluetooth/Devices/DeviceMacAddress.cs +++ b/src/Bluetooth/Devices/DeviceMacAddress.cs @@ -1,61 +1,9 @@ -using System; -using System.Text.RegularExpressions; -using Windows.Devices.Enumeration; - namespace BluetoothDevicePairing.Bluetooth.Devices { - internal sealed class DeviceMacAddress : IEquatable + internal sealed class DeviceMacAddress : MacAddress { - public DeviceMacAddress(DeviceInformation device) + public DeviceMacAddress(string mac) : base(mac) { - var match = Regex.Match(device.Id, @"(..:){5}(..)$"); - if (!match.Success) - { - throw new Exception($"Failed to extract mac address from the string '{device.Id}'"); - } - Address = match.Value.ToUpper(); - } - - public DeviceMacAddress(string mac) - { - var match = Regex.Match(mac, @"^(..:){5}(..)$"); - if (!match.Success) - { - throw new Exception($"MacAddress address '{mac}' is not a valid mac address"); - } - Address = mac; - } - - public string Address { get; } - - public override string ToString() - { - return Address; - } - - public bool Equals(DeviceMacAddress other) - { - if (other is null) - { - return false; - } - - if (ReferenceEquals(this, other)) - { - return true; - } - - return Address == other.Address; - } - - public override bool Equals(object obj) - { - return ReferenceEquals(this, obj) || obj is DeviceMacAddress other && Equals(other); - } - - public override int GetHashCode() - { - return Address != null ? Address.GetHashCode() : 0; } } } diff --git a/src/Bluetooth/Devices/DevicePairer.cs b/src/Bluetooth/Devices/DevicePairer.cs index fb0909e..18f28f6 100644 --- a/src/Bluetooth/Devices/DevicePairer.cs +++ b/src/Bluetooth/Devices/DevicePairer.cs @@ -8,12 +8,12 @@ public static void PairDevice(Device device, string pinCode) { Console.WriteLine($"Request to pair device \"{device}\""); - if (device.IsConnected) + if (device.ConnectionStatus == ConnectionStatus.Connected) { throw new Exception("Device is already connected, no need to pair"); } - if (device.IsPaired) + if (device.ConnectionStatus == ConnectionStatus.Paired) { throw new Exception("Device is already paired"); } diff --git a/src/Bluetooth/Devices/DeviceUnPairer.cs b/src/Bluetooth/Devices/DeviceUnPairer.cs index a153c95..a035ac1 100644 --- a/src/Bluetooth/Devices/DeviceUnPairer.cs +++ b/src/Bluetooth/Devices/DeviceUnPairer.cs @@ -8,7 +8,7 @@ public static void UnpairDevice(Device device) { Console.WriteLine($"Request to unpair device \"{device}\""); - if (!device.IsPaired) + if (device.ConnectionStatus == ConnectionStatus.NotPaired) { throw new Exception("Device is not paired"); } diff --git a/src/Bluetooth/Devices/AsqFilter.cs b/src/Bluetooth/Devices/Utils/AsqFilter.cs similarity index 62% rename from src/Bluetooth/Devices/AsqFilter.cs rename to src/Bluetooth/Devices/Utils/AsqFilter.cs index 48ccdc0..1e62038 100644 --- a/src/Bluetooth/Devices/AsqFilter.cs +++ b/src/Bluetooth/Devices/Utils/AsqFilter.cs @@ -1,6 +1,4 @@ -using Windows.Devices.Bluetooth; - -namespace BluetoothDevicePairing.Bluetooth.Devices +namespace BluetoothDevicePairing.Bluetooth.Devices.Utils { internal sealed class AsqFilter { @@ -25,15 +23,15 @@ public static AsqFilter BluetoothDevicesFilter() public static AsqFilter PairedBluetoothDevicesFilter() { - var bPaired = BluetoothDevice.GetDeviceSelectorFromPairingState(true); - var blePaired = BluetoothLEDevice.GetDeviceSelectorFromPairingState(true); + var bPaired = Windows.Devices.Bluetooth.BluetoothDevice.GetDeviceSelectorFromPairingState(true); + var blePaired = Windows.Devices.Bluetooth.BluetoothLEDevice.GetDeviceSelectorFromPairingState(true); return new AsqFilter($"({bPaired}) OR ({blePaired})"); } public static AsqFilter NonPairedBluetoothDevicesFilter() { - var bNonPaired = BluetoothDevice.GetDeviceSelectorFromPairingState(false); - var bleNonPaired = BluetoothLEDevice.GetDeviceSelectorFromPairingState(false); + var bNonPaired = Windows.Devices.Bluetooth.BluetoothDevice.GetDeviceSelectorFromPairingState(false); + var bleNonPaired = Windows.Devices.Bluetooth.BluetoothLEDevice.GetDeviceSelectorFromPairingState(false); return new AsqFilter($"({bNonPaired}) OR ({bleNonPaired})"); } } diff --git a/src/Bluetooth/Devices/DeviceWatcher.cs b/src/Bluetooth/Devices/Utils/DeviceWatcher.cs similarity index 97% rename from src/Bluetooth/Devices/DeviceWatcher.cs rename to src/Bluetooth/Devices/Utils/DeviceWatcher.cs index e655586..5455b3e 100644 --- a/src/Bluetooth/Devices/DeviceWatcher.cs +++ b/src/Bluetooth/Devices/Utils/DeviceWatcher.cs @@ -3,7 +3,7 @@ using System.Linq; using System.Threading; -namespace BluetoothDevicePairing.Bluetooth.Devices +namespace BluetoothDevicePairing.Bluetooth.Devices.Utils { internal sealed class DeviceWatcher { diff --git a/src/Bluetooth/MacAddress.cs b/src/Bluetooth/MacAddress.cs new file mode 100644 index 0000000..0114fca --- /dev/null +++ b/src/Bluetooth/MacAddress.cs @@ -0,0 +1,37 @@ +using System; +using System.Linq; +using System.Text.RegularExpressions; + +namespace BluetoothDevicePairing.Bluetooth +{ + internal class MacAddress + { + public string Address { get; } + public ulong RawAddess { get; } + + public MacAddress(string mac) + { + var match = Regex.Match(mac, @"^(..:){5}(..)$"); + if (!match.Success) + { + throw new Exception($"MacAddress address '{mac}' is not a valid mac address"); + } + Address = mac; + RawAddess = Convert.ToUInt64(Address.Replace(":", ""), 16); + } + + public MacAddress(ulong mac) + { + Address = string.Join(":", BitConverter.GetBytes(mac) + .Reverse() + .Skip(2) + .Select(b => b.ToString("x2"))); + RawAddess = mac; + } + + public override string ToString() + { + return Address; + } + } +} diff --git a/src/Commands/DiscoverDevices.cs b/src/Commands/DiscoverDevices.cs index 1933cf6..5ee8f31 100644 --- a/src/Commands/DiscoverDevices.cs +++ b/src/Commands/DiscoverDevices.cs @@ -20,36 +20,23 @@ internal static class DiscoverDevices { public static void Execute(DiscoverDevicesOptions opts) { - var devices = DeviceDiscoverer.DiscoverBluetoothDevices(opts.DiscoveryTime).OrderBy(d => d.Name); - Console.WriteLine("----------------------------------------------------------"); + var devices = DeviceDiscoverer.DiscoverBluetoothDevices(new DiscoveryTime(opts.DiscoveryTime)).OrderBy(d => d.Name); + Console.WriteLine(new string('-', 73)); foreach (var d in devices) { PrintDevice(d); } - Console.WriteLine("----------------------------------------------------------"); + Console.WriteLine(new string('-', 73)); } private static void PrintDevice(Device d) { - Console.WriteLine($"{GetType(d),2}|{d.Mac}|{GetStatus(d),-9}|{GetName(d),-40}|"); + Console.WriteLine($"|{GetType(d),2}|{d.Id.DeviceMac}|{GetConnectionStatus(d),-9}|{GetName(d),-40}|"); } private static string GetType(Device d) { - return d.Type == DeviceType.BluetoothLE ? "LE" : ""; - } - - private static string GetStatus(Device d) - { - if (d.IsConnected) - { - return "Connected"; - } - if (d.IsPaired) - { - return "Paired"; - } - return ""; + return d.Id.DeviceType == DeviceType.BluetoothLE ? "LE" : ""; } private static string GetName(Device d) @@ -59,7 +46,7 @@ private static string GetName(Device d) private static string GetConnectionStatus(Device d) { - return d.IsConnected ? "Connected" : ""; + return d.ConnectionStatus == ConnectionStatus.NotPaired ? "" : d.ConnectionStatus.ToString(); } } } diff --git a/src/Commands/ListAdapters.cs b/src/Commands/ListAdapters.cs new file mode 100644 index 0000000..d2f5f3a --- /dev/null +++ b/src/Commands/ListAdapters.cs @@ -0,0 +1,37 @@ +using BluetoothDevicePairing.Bluetooth.Adapter; +using CommandLine; +using System; + +namespace BluetoothDevicePairing.Commands +{ + [Verb("list-adapters", + HelpText = "Lists bluetooth adapters. Prints a table with the following fields:\n" + + "|Is default|Radio mac address|Name|State|")] + internal sealed class ListAdaptersOptions + { + } + + internal static class ListAdapters + { + public static void Execute(ListAdaptersOptions opts) + { + var adapters = AdapterFinder.FindBluetoothAdapters(); + Console.WriteLine(new string('-', 71)); + foreach (var a in adapters) + { + PrintAdapter(a); + } + Console.WriteLine(new string('-', 71)); + } + + private static void PrintAdapter(Adapter a) + { + Console.WriteLine($"|{GetIsDefailt(a),1}|{a.MacAddress}|{a.Name,-40}|{a.State,-8}|"); + } + + private static string GetIsDefailt(Adapter a) + { + return a.IsDefault ? "*" : ""; + } + } +} diff --git a/src/Commands/PairDeviceByMac.cs b/src/Commands/PairDeviceByMac.cs index 03180d7..c790940 100644 --- a/src/Commands/PairDeviceByMac.cs +++ b/src/Commands/PairDeviceByMac.cs @@ -6,7 +6,7 @@ namespace BluetoothDevicePairing.Commands { [Verb("pair-by-mac", HelpText = "Pair and connect to a device using its mac address")] - internal sealed class PairDeviceByMacOptions : PairAndUnpairDeviceByMacOptions + internal sealed class PairDeviceByMacOptions : MacAndDeviceTypeOptions { [Option("pin", Default = "0000", @@ -19,7 +19,6 @@ internal static class PairDeviceByMac public static void Execute(PairDeviceByMacOptions opts) { var mac = new DeviceMacAddress(opts.Mac); - var device = DeviceFinder.FindDevicesByMac(mac, opts.DeviceType); DevicePairer.PairDevice(device, opts.PinCode); } diff --git a/src/Commands/PairDeviceByName.cs b/src/Commands/PairDeviceByName.cs index e06540a..6805b6b 100644 --- a/src/Commands/PairDeviceByName.cs +++ b/src/Commands/PairDeviceByName.cs @@ -19,7 +19,7 @@ internal static class PairDeviceByName { public static void Execute(PairDeviceByNameOptions opts) { - var devices = DeviceFinder.FindDevicesByName(opts.DiscoveryTime, opts.DeviceName, opts.DeviceType); + var devices = DeviceFinder.FindDevicesByName(new DiscoveryTime(opts.DiscoveryTime), opts.DeviceName, opts.DeviceType); if (devices.Count == 1) { diff --git a/src/Commands/UnPairDeviceByMac.cs b/src/Commands/UnPairDeviceByMac.cs index 44015ab..8054898 100644 --- a/src/Commands/UnPairDeviceByMac.cs +++ b/src/Commands/UnPairDeviceByMac.cs @@ -5,7 +5,7 @@ namespace BluetoothDevicePairing.Commands { [Verb("unpair-by-mac", HelpText = "Unpair a device using its mac address")] - internal sealed class UnpairDeviceByMacOptions : PairAndUnpairDeviceByMacOptions + internal sealed class UnpairDeviceByMacOptions : MacAndDeviceTypeOptions { } diff --git a/src/Commands/UnPairDeviceByName.cs b/src/Commands/UnPairDeviceByName.cs index eccd7cf..17cf5b9 100644 --- a/src/Commands/UnPairDeviceByName.cs +++ b/src/Commands/UnPairDeviceByName.cs @@ -14,7 +14,7 @@ internal static class UnPairDeviceByName { public static void Execute(UnpairDeviceByNameOptions opts) { - var devices = DeviceFinder.FindDevicesByName(opts.DiscoveryTime, opts.DeviceName, opts.DeviceType); + var devices = DeviceFinder.FindDevicesByName(new DiscoveryTime(opts.DiscoveryTime), opts.DeviceName, opts.DeviceType); if (devices.Count > 1) { throw new Exception($"{devices.Count} devices found, don't know which one to choose"); diff --git a/src/Commands/Utils/CommonOptions.cs b/src/Commands/Utils/CommonOptions.cs index 1c87d05..225f720 100644 --- a/src/Commands/Utils/CommonOptions.cs +++ b/src/Commands/Utils/CommonOptions.cs @@ -2,7 +2,7 @@ namespace BluetoothDevicePairing.Commands.Utils { - internal class PairAndUnpairDeviceOptions + internal class DeviceTypeOption { [Option("type", Required = true, @@ -10,7 +10,7 @@ internal class PairAndUnpairDeviceOptions public Bluetooth.Devices.DeviceType DeviceType { get; set; } } - internal class PairAndUnpairDeviceByMacOptions : PairAndUnpairDeviceOptions + internal class MacAndDeviceTypeOptions : DeviceTypeOption { [Option("mac", Required = true, @@ -18,7 +18,7 @@ internal class PairAndUnpairDeviceByMacOptions : PairAndUnpairDeviceOptions public string Mac { get; set; } } - internal class PairAndUnpairDeviceByNameOptions : PairAndUnpairDeviceOptions + internal class PairAndUnpairDeviceByNameOptions : DeviceTypeOption { [Option("discovery-time", Default = 10, diff --git a/src/Commands/Utils/DeviceFinder.cs b/src/Commands/Utils/DeviceFinder.cs index fc77f65..72878c6 100644 --- a/src/Commands/Utils/DeviceFinder.cs +++ b/src/Commands/Utils/DeviceFinder.cs @@ -1,4 +1,3 @@ -using BluetoothDevicePairing.Bluetooth.Adapter; using BluetoothDevicePairing.Bluetooth.Devices; using System; using System.Collections.Generic; @@ -7,10 +6,10 @@ namespace BluetoothDevicePairing.Commands.Utils { internal static class DeviceFinder { - public static List FindDevicesByName(int discoveryTimeInSeconds, string name, DeviceType deviceType) + public static List FindDevicesByName(DiscoveryTime time, string name, DeviceType deviceType) { - var devices = DeviceDiscoverer.DiscoverBluetoothDevices(discoveryTimeInSeconds); - var res = devices.FindAll(d => d.Name == name && deviceType == d.Type); + var devices = DeviceDiscoverer.DiscoverBluetoothDevices(time); + var res = devices.FindAll(d => d.Name == name && deviceType == d.Id.DeviceType); if (res.Count == 0) { @@ -22,16 +21,13 @@ public static List FindDevicesByName(int discoveryTimeInSeconds, string public static Device FindDevicesByMac(DeviceMacAddress mac, DeviceType deviceType) { - Console.WriteLine("Find default bluetooth adapter"); - var defaultAdapter = AdapterFinder.FindDefaultAdapter(); - Console.WriteLine($"Default bluetooth adapter found: \"{defaultAdapter}\""); - - var deviceId = $"{deviceType}#{deviceType}{defaultAdapter.MacAddress}-{mac}"; - - Console.WriteLine($"Create device information using ID '{deviceId}'"); - var deviceInfo = Windows.Devices.Enumeration.DeviceInformation.CreateFromIdAsync(deviceId).GetAwaiter().GetResult(); - - return new(deviceInfo); + if (deviceType == DeviceType.Bluetooth) + { + Console.WriteLine($"Create Bluetooth device using Mac:'{mac}'"); + return BluetoothDevice.FromMac(mac); + } + Console.WriteLine($"Create BluetoothLE device using Mac:'{mac}'"); + return BluetoothLeDevice.FromMac(mac); } } } diff --git a/src/Program.cs b/src/Program.cs index 6dfdb5a..ea4f4d9 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -12,12 +12,14 @@ private static void ParseCommandLineAndExecuteActions(string[] args) PairDeviceByMacOptions, PairDeviceByNameOptions, UnpairDeviceByMacOptions, - UnpairDeviceByNameOptions>(args) + UnpairDeviceByNameOptions, + ListAdaptersOptions>(args) .WithParsed(DiscoverDevices.Execute) .WithParsed(PairDeviceByMac.Execute) .WithParsed(PairDeviceByName.Execute) .WithParsed(UnPairDeviceByMac.Execute) - .WithParsed(UnPairDeviceByName.Execute); + .WithParsed(UnPairDeviceByName.Execute) + .WithParsed(ListAdapters.Execute); } private static int Main(string[] args)