‪Black Box
Public Member Functions | Data Fields | Private Attributes
MiBand_Heartrate.BLEManager Class Reference

Public Member Functions

delegate void OnDeviceConnected (BLEManager manager)
 
DeviceWatcher CreateWatcher ()
 ‪Manages fitness tracker More...
 
void StartWatcher ()
 
void StopWatcher ()
 
async void Connect (string deviceId, OnDeviceConnected callback=null)
 
void Disconnect ()
 
bool IsWatchingForDevice ()
 
bool HasDeviceConnected ()
 
async void Write (GattCharacteristic c, byte[] data)
 
async void EnumeratingUsages ()
 ‪Not used but can be useful for debugging More...
 

Data Fields

BluetoothLEDevice Device => connectedDevice
 

Private Attributes

DeviceWatcher watcher = null
 
List< DeviceInformation > devices = new List<DeviceInformation>()
 
BluetoothLEDevice connectedDevice = null
 

Detailed Description

Definition at line 24 of file BLEManager.cs.

Member Function Documentation

◆ Connect()

async void MiBand_Heartrate.BLEManager.Connect ( string  deviceId,
OnDeviceConnected  callback = null 
)

Parameters
deviceId
callback

Definition at line 89 of file BLEManager.cs.

90  {
91  ‪connectedDevice = await BluetoothLEDevice.FromIdAsync(deviceId);
92  callback.Invoke(this);
93  }
‪BluetoothLEDevice connectedDevice
Definition: BLEManager.cs:30

Referenced by MiBand_Heartrate.BBMiBandConnect.connectButton_Click().

◆ CreateWatcher()

DeviceWatcher MiBand_Heartrate.BLEManager.CreateWatcher ( )

‪Manages fitness tracker

Returns

Definition at line 38 of file BLEManager.cs.

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  }
‪DeviceWatcher watcher
Definition: BLEManager.cs:28
‪List< DeviceInformation > devices
Definition: BLEManager.cs:29

Referenced by MiBand_Heartrate.BBMiBandConnect.ConnectionFrame_Load().

◆ Disconnect()

void MiBand_Heartrate.BLEManager.Disconnect ( )

Definition at line 97 of file BLEManager.cs.

98  {
99  if (‪connectedDevice != null) { ‪connectedDevice.Dispose(); ‪connectedDevice = null; }
100  }
‪BluetoothLEDevice connectedDevice
Definition: BLEManager.cs:30

Referenced by MiBand_Heartrate.ControlFrame.ControlFrame_FormClosing().

◆ EnumeratingUsages()

async void MiBand_Heartrate.BLEManager.EnumeratingUsages ( )

‪Not used but can be useful for debugging

Definition at line 148 of file BLEManager.cs.

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  }
‪BluetoothLEDevice connectedDevice
Definition: BLEManager.cs:30

◆ HasDeviceConnected()

bool MiBand_Heartrate.BLEManager.HasDeviceConnected ( )

◆ IsWatchingForDevice()

bool MiBand_Heartrate.BLEManager.IsWatchingForDevice ( )

Returns

Definition at line 105 of file BLEManager.cs.

106  {
107  return (‪watcher != null && ‪watcher.Status == DeviceWatcherStatus.Started);
108  }
‪DeviceWatcher watcher
Definition: BLEManager.cs:28

Referenced by MiBand_Heartrate.BBMiBandConnect.ConnectionFrame_FormClosing().

◆ OnDeviceConnected()

delegate void MiBand_Heartrate.BLEManager.OnDeviceConnected ( BLEManager  manager)

◆ StartWatcher()

void MiBand_Heartrate.BLEManager.StartWatcher ( )

Definition at line 75 of file BLEManager.cs.

76  {
77  ‪watcher.Start();
78  }
‪DeviceWatcher watcher
Definition: BLEManager.cs:28

◆ StopWatcher()

void MiBand_Heartrate.BLEManager.StopWatcher ( )

Definition at line 80 of file BLEManager.cs.

81  {
82  ‪watcher.Stop();
83  }
‪DeviceWatcher watcher
Definition: BLEManager.cs:28

Referenced by MiBand_Heartrate.BBMiBandConnect.ConnectionFrame_FormClosing().

◆ Write()

async void MiBand_Heartrate.BLEManager.Write ( GattCharacteristic  c,
byte []  data 
)

Parameters
c
data

Definition at line 122 of file BLEManager.cs.

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  }

Referenced by MiBand_Heartrate.MiBand.Authenticate(), MiBand_Heartrate.MiBand.RunPingSensor(), MiBand_Heartrate.MiBand.StartMonitorHeartrate(), and MiBand_Heartrate.MiBand.StopMonitorHeartrate().

Field Documentation

◆ connectedDevice

BluetoothLEDevice MiBand_Heartrate.BLEManager.connectedDevice = null
private

◆ Device

BluetoothLEDevice MiBand_Heartrate.BLEManager.Device => connectedDevice

◆ devices

List<DeviceInformation> MiBand_Heartrate.BLEManager.devices = new List<DeviceInformation>()
private

Definition at line 29 of file BLEManager.cs.

Referenced by MiBand_Heartrate.BLEManager.CreateWatcher().

◆ watcher

DeviceWatcher MiBand_Heartrate.BLEManager.watcher = null
private

The documentation for this class was generated from the following file: