-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Only consider paired devices when unpair devices by name
* Improve code quality
- Loading branch information
1 parent
cba8a5c
commit e9bf3c0
Showing
26 changed files
with
522 additions
and
568 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/build/ | ||
.vs/ | ||
/src/FodyWeavers.xsd | ||
/.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,29 @@ | ||
using System; | ||
|
||
namespace BluetoothDevicePairing.Bluetooth.Adapter | ||
namespace BluetoothDevicePairing.Bluetooth.Adapters; | ||
|
||
internal sealed class Adapter | ||
{ | ||
internal sealed class Adapter | ||
{ | ||
private readonly Windows.Devices.Enumeration.DeviceInformation adapterInfo; | ||
private readonly Windows.Devices.Bluetooth.BluetoothAdapter adapterDevice; | ||
private readonly Windows.Devices.Radios.Radio radio; | ||
private readonly Windows.Devices.Enumeration.DeviceInformation adapterInfo; | ||
private readonly Windows.Devices.Radios.Radio radio; | ||
|
||
public Windows.Devices.Radios.RadioState State => radio.State; | ||
public string Name => adapterInfo.Name; | ||
public AdapterMacAddress MacAddress { get; } | ||
public bool IsDefault { get; } | ||
public Windows.Devices.Radios.RadioState State => radio.State; | ||
public string Name => adapterInfo.Name; | ||
public AdapterMacAddress MacAddress { get; } | ||
public bool IsDefault { get; } | ||
|
||
public Adapter(Windows.Devices.Enumeration.DeviceInformation bluetoothAdapterInfo, | ||
AdapterMacAddress defaultAdapterMacAddress) | ||
{ | ||
adapterInfo = bluetoothAdapterInfo; | ||
adapterDevice = Windows.Devices.Bluetooth.BluetoothAdapter.FromIdAsync(bluetoothAdapterInfo.Id).GetAwaiter().GetResult(); | ||
radio = adapterDevice.GetRadioAsync().GetAwaiter().GetResult(); | ||
MacAddress = new AdapterMacAddress(adapterDevice); | ||
IsDefault = MacAddress.RawAddess == defaultAdapterMacAddress.RawAddess; | ||
} | ||
public Adapter(Windows.Devices.Enumeration.DeviceInformation bluetoothAdapterInfo, | ||
AdapterMacAddress defaultAdapterMacAddress) | ||
{ | ||
adapterInfo = bluetoothAdapterInfo; | ||
var adapterDevice = Windows.Devices.Bluetooth.BluetoothAdapter.FromIdAsync(bluetoothAdapterInfo.Id).GetAwaiter().GetResult(); | ||
radio = adapterDevice.GetRadioAsync().GetAwaiter().GetResult(); | ||
MacAddress = new AdapterMacAddress(adapterDevice); | ||
IsDefault = MacAddress.RawAddress == defaultAdapterMacAddress.RawAddress; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return $"name:'{Name}' mac:'{MacAddress}' state:'{State}'"; | ||
} | ||
public override string ToString() | ||
{ | ||
return $"name:'{Name}' mac:'{MacAddress}' state:'{State}'"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
namespace BluetoothDevicePairing.Bluetooth.Adapter | ||
namespace BluetoothDevicePairing.Bluetooth.Adapters; | ||
|
||
internal sealed class AdapterMacAddress : MacAddress | ||
{ | ||
internal sealed class AdapterMacAddress : MacAddress | ||
public AdapterMacAddress(Windows.Devices.Bluetooth.BluetoothAdapter adapter) : base(adapter.BluetoothAddress) | ||
{ | ||
public AdapterMacAddress(Windows.Devices.Bluetooth.BluetoothAdapter adapter) : base(adapter.BluetoothAddress) | ||
{ | ||
} | ||
} | ||
|
||
public AdapterMacAddress(string mac) : base(mac) | ||
{ | ||
} | ||
public AdapterMacAddress(string mac) : base(mac) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,24 @@ | ||
using System; | ||
|
||
namespace BluetoothDevicePairing.Bluetooth.Devices | ||
namespace BluetoothDevicePairing.Bluetooth.Devices; | ||
|
||
internal sealed class BluetoothDevice : Device | ||
{ | ||
internal sealed class BluetoothDevice : Device | ||
{ | ||
public readonly Windows.Devices.Bluetooth.BluetoothDevice device; | ||
protected override bool IsConnected => device.ConnectionStatus == Windows.Devices.Bluetooth.BluetoothConnectionStatus.Connected; | ||
private 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; | ||
} | ||
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 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()); | ||
} | ||
public static BluetoothDevice FromMac(DeviceMacAddress mac) | ||
{ | ||
return new BluetoothDevice(Windows.Devices.Bluetooth.BluetoothDevice.FromBluetoothAddressAsync(mac.RawAddress).GetAwaiter().GetResult()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,27 @@ | ||
using System; | ||
|
||
namespace BluetoothDevicePairing.Bluetooth.Devices | ||
namespace BluetoothDevicePairing.Bluetooth.Devices; | ||
|
||
internal class BluetoothLeDevice : Device | ||
{ | ||
internal class BluetoothLeDevice : Device | ||
{ | ||
private readonly Windows.Devices.Bluetooth.BluetoothLEDevice device; | ||
protected override bool IsConnected => device.ConnectionStatus == Windows.Devices.Bluetooth.BluetoothConnectionStatus.Connected; | ||
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; | ||
} | ||
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 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); | ||
} | ||
public static BluetoothLeDevice FromMac(DeviceMacAddress mac) | ||
{ | ||
var device = Windows.Devices.Bluetooth.BluetoothLEDevice.FromBluetoothAddressAsync(mac.RawAddress).GetAwaiter().GetResult(); | ||
return 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") | ||
: new BluetoothLeDevice(device); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,41 @@ | ||
namespace BluetoothDevicePairing.Bluetooth.Devices | ||
namespace BluetoothDevicePairing.Bluetooth.Devices; | ||
|
||
internal enum ConnectionStatus | ||
{ | ||
internal enum ConnectionStatus | ||
{ | ||
NotPaired, | ||
Paired, | ||
Connected | ||
} | ||
NotPaired, | ||
Paired, | ||
Connected | ||
} | ||
|
||
internal enum DeviceType | ||
{ | ||
Bluetooth, | ||
BluetoothLE | ||
} | ||
internal enum DeviceType | ||
{ | ||
Bluetooth, | ||
BluetoothLE | ||
} | ||
|
||
internal abstract class Device | ||
{ | ||
private readonly Windows.Devices.Enumeration.DeviceInformation info; | ||
protected abstract bool IsConnected { get; } | ||
internal abstract class Device | ||
{ | ||
private readonly Windows.Devices.Enumeration.DeviceInformation info; | ||
protected abstract bool IsConnected { get; } | ||
|
||
public ConnectionStatus ConnectionStatus => | ||
IsConnected | ||
? ConnectionStatus.Connected | ||
: PairingInfo.IsPaired | ||
? ConnectionStatus.Paired | ||
: ConnectionStatus.NotPaired; | ||
public Windows.Devices.Enumeration.DeviceInformationPairing PairingInfo => info.Pairing; | ||
public DeviceInfoId Id { get; } | ||
public string Name => info.Name; | ||
public ConnectionStatus ConnectionStatus => | ||
IsConnected | ||
? ConnectionStatus.Connected | ||
: PairingInfo.IsPaired | ||
? ConnectionStatus.Paired | ||
: ConnectionStatus.NotPaired; | ||
public Windows.Devices.Enumeration.DeviceInformationPairing PairingInfo => info.Pairing; | ||
public DeviceInfoId Id { get; } | ||
public string Name => info.Name; | ||
|
||
protected Device(Windows.Devices.Enumeration.DeviceInformation info) | ||
{ | ||
this.info = info; | ||
Id = new DeviceInfoId(info); | ||
} | ||
protected Device(Windows.Devices.Enumeration.DeviceInformation info) | ||
{ | ||
this.info = info; | ||
Id = new DeviceInfoId(info); | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return $"name:'{Name}' mac:'{Id.DeviceMac}' type:'{Id.DeviceType}' ConnectionStatus:'{ConnectionStatus}'"; | ||
} | ||
public override string ToString() | ||
{ | ||
return $"name:'{Name}' mac:'{Id.DeviceMac}' type:'{Id.DeviceType}' ConnectionStatus:'{ConnectionStatus}'"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.