‪Black Box
BLEManager.cs
Go to the documentation of this file.
1 /*
2 * FILE : BLEManager.cs
3 * PROJECT : Black Box
4 * PROGRAMMER : BRIAN HINDS
5 * FIRST VERSION : 1.0.0.0
6 * DATE : March 20, 2019
7 * DESCRIPTION : Used for manage and discover Bluetooth LE device
8 */
9 
10 using System;
11 using System.Collections.Generic;
12 using Windows.Devices.Bluetooth;
13 using Windows.Devices.Bluetooth.GenericAttributeProfile;
14 using Windows.Devices.Enumeration;
15 using Windows.Storage.Streams;
16 
17 namespace ‪MiBand_Heartrate
18 {
19  /* BLEManager
20  * Used for manage and discover Bluetooth LE device
21  * see https://docs.microsoft.com/en-us/windows/uwp/devices-sensors/gatt-client
22  */
23 
24  internal class ‪BLEManager
25  {
26  public delegate void ‪OnDeviceConnected(‪BLEManager manager);
27 
28  private DeviceWatcher ‪watcher = null;
29  private List<DeviceInformation> ‪devices = new List<DeviceInformation>();
30  private BluetoothLEDevice ‪connectedDevice = null;
31 
32  public BluetoothLEDevice ‪Device => ‪connectedDevice;
33 
38  public DeviceWatcher ‪CreateWatcher()
39  {
40  if (‪watcher == null)
41  {
42  ‪devices.Clear();
43  string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected" };
44  ‪watcher = DeviceInformation.CreateWatcher(
45  BluetoothLEDevice.GetDeviceSelectorFromPairingState(true),
46  requestedProperties,
47  DeviceInformationKind.AssociationEndpoint
48  );
49 
50  ‪watcher.Added += (DeviceWatcher w, DeviceInformation d) =>
51  {
52  ‪devices.Add(d);
53  };
54 
55  ‪watcher.Updated += (DeviceWatcher w, DeviceInformationUpdate d) =>
56  {
57  foreach (DeviceInformation device in ‪devices)
58  {
59  if (device.Id == d.Id) { device.Update(d); break; }
60  }
61  };
62 
63  ‪watcher.Removed += (DeviceWatcher w, DeviceInformationUpdate d) =>
64  {
65  foreach (DeviceInformation device in ‪devices)
66  {
67  if (device.Id == d.Id) { ‪devices.Remove(device); break; }
68  }
69  };
70  }
71 
72  return ‪watcher;
73  }
74 
75  public void ‪StartWatcher()
76  {
77  ‪watcher.Start();
78  }
79 
80  public void ‪StopWatcher()
81  {
82  ‪watcher.Stop();
83  }
84 
89  public async void ‪Connect(string deviceId, ‪OnDeviceConnected callback = null)
90  {
91  ‪connectedDevice = await BluetoothLEDevice.FromIdAsync(deviceId);
92  callback.Invoke(this);
93  }
94 
97  public void ‪Disconnect()
98  {
99  if (‪connectedDevice != null) { ‪connectedDevice.Dispose(); ‪connectedDevice = null; }
100  }
101 
106  {
107  return (‪watcher != null && ‪watcher.Status == DeviceWatcherStatus.Started);
108  }
109 
113  public bool ‪HasDeviceConnected()
114  {
115  return (‪connectedDevice != null);
116  }
117 
122  public async void ‪Write(GattCharacteristic c, byte[] data)
123  {
124  if (!‪HasDeviceConnected()) { return; }
125 
126  using (DataWriter stream = new DataWriter())
127  {
128  stream.WriteBytes(data);
129 
130  try
131  {
132  GattCommunicationStatus result = await c.WriteValueAsync(stream.DetachBuffer());
133  if (result != GattCommunicationStatus.Success)
134  {
135  Console.WriteLine(string.Format("Write {0} on {1} failed !", BitConverter.ToString(data), c.Uuid));
136  }
137  }
138  catch (Exception e)
139  {
140  Console.WriteLine(e.Message);
141  }
142  }
143  }
144 
148  public async void ‪EnumeratingUsages()
149  {
150  if (!‪HasDeviceConnected()) { return; }
151 
152  GattDeviceServicesResult services = await ‪connectedDevice.GetGattServicesAsync();
153  if (services.Status == GattCommunicationStatus.Success)
154  {
155  foreach (GattDeviceService s in services.Services)
156  {
157  await s.RequestAccessAsync();
158  GattCharacteristicsResult characteristicsResult = await s.GetCharacteristicsAsync();
159 
160  Console.WriteLine("Service : " + s.Uuid);
161  if (characteristicsResult.Status == GattCommunicationStatus.Success)
162  {
163  foreach (GattCharacteristic c in characteristicsResult.Characteristics)
164  {
165  GattCharacteristicProperties props = c.CharacteristicProperties;
166  Console.WriteLine("\t characteristics : " + c.Uuid + " / " + c.UserDescription);
167  if (props.HasFlag(GattCharacteristicProperties.Read))
168  {
169  Console.WriteLine("\t\tRead");
170  }
171  if (props.HasFlag(GattCharacteristicProperties.Write))
172  {
173  Console.WriteLine("\t\tWrite");
174  }
175  if (props.HasFlag(GattCharacteristicProperties.Notify))
176  {
177  Console.WriteLine("\t\tNotify");
178  }
179  }
180  }
181 
182  s.Dispose();
183  }
184  }
185  }
186  }
187 }
‪DeviceWatcher watcher
Definition: BLEManager.cs:28
‪List< DeviceInformation > devices
Definition: BLEManager.cs:29
‪BluetoothLEDevice Device
Definition: BLEManager.cs:32
‪DeviceWatcher CreateWatcher()
‪Manages fitness tracker
Definition: BLEManager.cs:38
‪async void Connect(string deviceId, OnDeviceConnected callback=null)
Definition: BLEManager.cs:89
‪async void EnumeratingUsages()
‪Not used but can be useful for debugging
Definition: BLEManager.cs:148
‪async void Write(GattCharacteristic c, byte[] data)
Definition: BLEManager.cs:122
‪BluetoothLEDevice connectedDevice
Definition: BLEManager.cs:30
‪delegate void OnDeviceConnected(BLEManager manager)