|
1 | | -using System; |
2 | | -using System.Text.RegularExpressions; |
3 | | - |
4 | 1 | namespace BluetoothDevicePairing.Bluetooth.Devices |
5 | 2 | { |
| 3 | + internal enum ConnectionStatus |
| 4 | + { |
| 5 | + NotPaired, |
| 6 | + Paired, |
| 7 | + Connected |
| 8 | + } |
| 9 | + |
6 | 10 | internal enum DeviceType |
7 | 11 | { |
8 | 12 | Bluetooth, |
9 | 13 | BluetoothLE |
10 | 14 | } |
11 | 15 |
|
12 | | - internal sealed class Device |
| 16 | + internal abstract class Device |
13 | 17 | { |
14 | 18 | private readonly Windows.Devices.Enumeration.DeviceInformation info; |
15 | | - private readonly Windows.Devices.Bluetooth.BluetoothDevice bluetoothDevice; |
16 | | - private readonly Windows.Devices.Bluetooth.BluetoothLEDevice bluetoothLeDevice; |
| 19 | + protected abstract bool IsConnected { get; } |
17 | 20 |
|
18 | | - public string Id => info.Id; |
| 21 | + public ConnectionStatus ConnectionStatus => |
| 22 | + IsConnected |
| 23 | + ? ConnectionStatus.Connected |
| 24 | + : PairingInfo.IsPaired |
| 25 | + ? ConnectionStatus.Paired |
| 26 | + : ConnectionStatus.NotPaired; |
19 | 27 | public Windows.Devices.Enumeration.DeviceInformationPairing PairingInfo => info.Pairing; |
20 | | - public bool IsPaired => info.Pairing.IsPaired; |
21 | | - public DeviceMacAddress Mac { get; } |
22 | | - public DeviceType Type { get; } |
| 28 | + public DeviceInfoId Id { get; } |
23 | 29 | public string Name => info.Name; |
24 | | - public bool IsConnected |
25 | | - { |
26 | | - get |
27 | | - { |
28 | | - if (bluetoothDevice != null) |
29 | | - { |
30 | | - return bluetoothDevice.ConnectionStatus == Windows.Devices.Bluetooth.BluetoothConnectionStatus.Connected; |
31 | | - } |
32 | | - else |
33 | | - { |
34 | | - return bluetoothLeDevice.ConnectionStatus == Windows.Devices.Bluetooth.BluetoothConnectionStatus.Connected; |
35 | | - } |
36 | | - } |
37 | | - } |
38 | 30 |
|
39 | | - public Device(Windows.Devices.Enumeration.DeviceInformation info) |
| 31 | + protected Device(Windows.Devices.Enumeration.DeviceInformation info) |
40 | 32 | { |
41 | 33 | this.info = info; |
42 | | - Mac = new DeviceMacAddress(info); |
43 | | - Type = GetDeviceType(info); |
44 | | - if (Type == DeviceType.Bluetooth) |
45 | | - { |
46 | | - bluetoothDevice = Windows.Devices.Bluetooth.BluetoothDevice.FromIdAsync(info.Id).GetAwaiter().GetResult(); |
47 | | - } |
48 | | - else |
49 | | - { |
50 | | - bluetoothLeDevice = Windows.Devices.Bluetooth.BluetoothLEDevice.FromIdAsync(info.Id).GetAwaiter().GetResult(); |
51 | | - } |
| 34 | + Id = new DeviceInfoId(info); |
52 | 35 | } |
53 | 36 |
|
54 | 37 | public override string ToString() |
55 | 38 | { |
56 | | - return $"name:'{Name}' mac:'{Mac}' type:'{Type}' Connected:'{IsConnected}' Paired:'{IsPaired}'"; |
57 | | - } |
58 | | - |
59 | | - private static DeviceType GetDeviceType(Windows.Devices.Enumeration.DeviceInformation device) |
60 | | - { |
61 | | - var match = Regex.Match(device.Id, @"(^\w*)(#)"); |
62 | | - if (!match.Success) |
63 | | - { |
64 | | - throw new Exception($"Failed to extract the device type from the string '{device.Id}'"); |
65 | | - } |
66 | | - |
67 | | - var type = match.Groups[1].Value; |
68 | | - switch (type) |
69 | | - { |
70 | | - case "Bluetooth": |
71 | | - return DeviceType.Bluetooth; |
72 | | - case "BluetoothLE": |
73 | | - return DeviceType.BluetoothLE; |
74 | | - default: |
75 | | - throw new Exception($"Wrong device type '{type}' extracted from '{device.Id}'"); |
76 | | - } |
| 39 | + return $"name:'{Name}' mac:'{Id.DeviceMac}' type:'{Id.DeviceType}' ConnectionStatus:'{ConnectionStatus}'"; |
77 | 40 | } |
78 | 41 | } |
79 | 42 | } |
0 commit comments