‪Black Box
BBMiBandConnect.cs
Go to the documentation of this file.
1 /*
2 * FILE : BBMiBandConnect.cs
3 * PROJECT : Black Box
4 * PROGRAMMER : BRIAN HINDS
5 * FIRST VERSION : 1.0.0.0
6 * DATE : March 20, 2019
7 * DESCRIPTION : This file manages the selected connected device.
8 */
9 
10 using System;
11 using System.Collections.Generic;
12 using System.Windows.Forms;
13 using Windows.Devices.Enumeration;
14 
16 {
17  public partial class ‪BBMiBandConnect : Form
18  {
19  private DeviceWatcher ‪BLEWatcher;
20  private Dictionary<string, string> ‪devices;
22 
24  {
26  }
27 
33  private void ‪ConnectionFrame_Load(object sender, EventArgs e)
34  {
35  ‪devices = new Dictionary<string, string>();
36 
41  ‪BLEWatcher.Start();
42  }
43 
44  private delegate void ‪OnDeviceAddedHandler(DeviceWatcher w, DeviceInformation d);
45 
51  private void ‪OnDeviceAdded(DeviceWatcher w, DeviceInformation d)
52  {
53  if (InvokeRequired)
54  {
56  Invoke(c, new object[] { w, d });
57  }
58  else
59  {
60  try
61  {
62  ‪devices.Add(d.Id, d.Name);
63  ‪deviceList.DataSource = new BindingSource(‪devices, null);
64  ‪deviceList.DisplayMember = "Value";
65  ‪deviceList.ValueMember = "Key";
66  }
67  catch (Exception) { }
68  }
69  }
70 
71  private delegate void ‪OnDeviceRemovedHandler(DeviceWatcher w, DeviceInformationUpdate d);
72 
78  private void ‪OnDeviceRemoved(DeviceWatcher w, DeviceInformationUpdate d)
79  {
80  if (InvokeRequired)
81  {
83  Invoke(c, new object[] { w, d });
84  }
85  else
86  {
87  ‪devices.Remove(d.Id);
88 
89  try
90  {
91  ‪deviceList.DataSource = new BindingSource(‪devices, null);
92  ‪deviceList.DisplayMember = "Value";
93  ‪deviceList.ValueMember = "Key";
94  }
95  catch (Exception) { }
96  }
97  }
98 
99  private void ‪ConnectionFrame_FormClosing(object sender, FormClosingEventArgs e)
100  {
102  {
104  }
105  }
106 
112  private void ‪connectButton_Click(object sender, EventArgs e)
113  {
114  ‪connectButton.Enabled = false;
115 
116  ‪BLEWatcher.Stop();
117 
118  string selectedDeviceId = ‪deviceList.SelectedValue.ToString();
120  }
121 
122  private delegate void ‪OnDeviceConnectedHandler(‪BLEManager con);
123 
129  {
130  if (InvokeRequired)
131  {
133  Invoke(c, new object[] { con });
134  }
135  else
136  {
138  {
139  Hide();
141  ‪controlFrame.Closed += (s, args) => Close();
142  ‪controlFrame.Show();
143  }
144  else
145  {
146  MessageBox.Show("Connection to the device failed !", "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
147  ‪connectButton.Enabled = true;
148  ‪devices.Clear();
149  ‪BLEWatcher.Start();
150  }
151  }
152  }
153  }
154 }
‪void OnDeviceConnected(BLEManager con)
‪Authenticate on device connection
‪void OnDeviceAdded(DeviceWatcher w, DeviceInformation d)
‪Adds the selected device
‪System.Windows.Forms.ComboBox deviceList
‪delegate void OnDeviceRemovedHandler(DeviceWatcher w, DeviceInformationUpdate d)
‪void ConnectionFrame_FormClosing(object sender, FormClosingEventArgs e)
‪DeviceWatcher CreateWatcher()
‪Manages fitness tracker
Definition: BLEManager.cs:38
‪async void Connect(string deviceId, OnDeviceConnected callback=null)
Definition: BLEManager.cs:89
‪static DeviceControl Instance
‪Dictionary< string, string > devices
‪delegate void OnDeviceAddedHandler(DeviceWatcher w, DeviceInformation d)
‪void ConnectionFrame_Load(object sender, EventArgs e)
‪Loads the list of devices
‪delegate void OnDeviceConnectedHandler(BLEManager con)
‪System.Windows.Forms.Button connectButton
‪void connectButton_Click(object sender, EventArgs e)
‪Connects the selected device
‪void InitializeComponent()
‪Méthode requise pour la prise en charge du concepteur - ne modifiez pas le contenu de cette méthode a...
‪void OnDeviceRemoved(DeviceWatcher w, DeviceInformationUpdate d)
‪Removed the selected device