-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBluetoothController.cs
More file actions
184 lines (167 loc) · 7.46 KB
/
BluetoothController.cs
File metadata and controls
184 lines (167 loc) · 7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.Advertisement;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Enumeration;
using Windows.Storage.Streams;
namespace Microsoft.Samples.Kinect.InfraredBasics
{
public class BluetoothController
{
private Guid SERVICE_UUID = new Guid("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
private Guid CHARACTERISTIC_UUID = new Guid("12381d6f-57cd-4845-a22b-39382199a992");
private List<string> badDeviceIds = new List<string>();
private Dictionary<Spell, BluetoothLEDevice> spellDevices;
private Dictionary<Spell, GattCharacteristic> spellTriggers;
public delegate void SpellListChangeEvent(Spell[] spells);
public event SpellListChangeEvent OnSpellListChanged;
//private GattServiceProvider serviceProvider;
//private GattLocalCharacteristic actionCharacteristic;
//private GattLocalCharacteristic nameCharacteristic;
public BluetoothController()
{
spellDevices = new Dictionary<Spell, BluetoothLEDevice>();
spellTriggers = new Dictionary<Spell, GattCharacteristic>();
}
public void Initialize()
{
// Query for extra properties you want returned
string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected" };
DeviceWatcher deviceWatcher =
DeviceInformation.CreateWatcher("System.ItemNameDisplay:~~\"Potter: \"",
requestedProperties,
DeviceInformationKind.AssociationEndpoint);
// Register event handlers before starting the watcher.
// Added, Updated and Removed are required to get all nearby devices
deviceWatcher.Added += DeviceWatcher_Added;
deviceWatcher.Updated += DeviceWatcher_Updated;
deviceWatcher.Removed += DeviceWatcher_Removed;
// Start the watcher.
deviceWatcher.Start();
SpellListChanged();
//var advertisementWatcher = new BluetoothLEAdvertisementWatcher();
//advertisementWatcher.ScanningMode = BluetoothLEScanningMode.Active;
////advertisementWatcher.SignalStrengthFilter = new BluetoothSignalStrengthFilter
//{
// InRangeThresholdInDBm = -75,
// OutOfRangeThresholdInDBm = -76,
// OutOfRangeTimeout = TimeSpan.FromSeconds(2),
// SamplingInterval = TimeSpan.FromSeconds(2)
//};
//advertisementWatcher.AdvertisementFilter =
// new BluetoothLEAdvertisementFilter
// {
// Advertisement =
// new BluetoothLEAdvertisement
// {
// ServiceUuids =
// {
// SERVICE_UUID
// }
// }
// };
//advertisementWatcher.Received += AdvertisementRecieved;
//advertisementWatcher.Start();
}
//private void AdvertisementRecieved(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
//{
// //BluetoothLEDevice bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(args.BluetoothAddress);
// Console.WriteLine(args.Advertisement.LocalName + ":" + String.Join(", ", args.Advertisement.ServiceUuids));
//}
private async void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation args)
{
if (badDeviceIds.Contains(args.Id))
{
return;
}
Console.WriteLine($"Added: {args.Id} ({args.Kind})");
try
{
BluetoothLEDevice device = BluetoothLEDevice.FromIdAsync(args.Id).AsTask().Result;
if (device == null) return;
Spell result = await Connect(args, device);
device.ConnectionStatusChanged += async (dev, obj) =>
{
if (dev.ConnectionStatus == BluetoothConnectionStatus.Connected)
{
await Connect(args, dev);
}
else
{
spellDevices.Remove(result);
spellTriggers.Remove(result);
}
SpellListChanged();
};
SpellListChanged();
}
catch (Exception e)
{
object friendlyNameObj;
string friendlyName = "<no_name>";
if (args.Properties.TryGetValue("System.ItemNameDisplay", out friendlyNameObj))
{
friendlyName = (string)friendlyNameObj;
}
Console.WriteLine($"Failed to connect to {args.Id} ({friendlyName}): {e.Message}");
}
}
private async Task<Spell> Connect(DeviceInformation args, BluetoothLEDevice device)
{
string spellName = device.Name.Split(new string[] { ": " }, StringSplitOptions.None)[1];
Spell result;
Action<string> markBadDevice = (msg) =>
{
Console.WriteLine(msg);
badDeviceIds.Add(args.Id);
device.Dispose();
return;
};
if (!Enum.TryParse<Spell>(spellName, out result))
{
markBadDevice("Spell device: " + device.Name + " has an invalid spell name!");
}
var serviceResult = await device.GetGattServicesForUuidAsync(SERVICE_UUID);
if (serviceResult.Services.Count != 1)
{
markBadDevice("Spell device" + device.Name + " does not have a spell service!");
}
var characteristicsResult = await serviceResult.Services[0].GetCharacteristicsForUuidAsync(CHARACTERISTIC_UUID);
if (characteristicsResult.Characteristics.Count != 1)
{
markBadDevice("Spell device " + device.Name + " has an invalid number of trigger characteristics!");
}
spellDevices[result] = device;
spellTriggers[result] = characteristicsResult.Characteristics[0];
return result;
}
private void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate args)
{
}
private void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate args)
{
// We currently don't address this, when a device is manually disconnected.
}
public async Task TriggerSpell(Spell spell)
{
if (spellTriggers.ContainsKey(spell))
{
var writer = new DataWriter();
writer.WriteString("Trigger");
await spellTriggers[spell].WriteValueAsync(writer.DetachBuffer());
}
else
{
Console.WriteLine("No attached device exists for spell: " + spell.ToString());
}
}
private void SpellListChanged()
{
OnSpellListChanged.Invoke(spellDevices.Keys.ToArray());
}
}
}