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;
29 private List<DeviceInformation>
devices =
new List<DeviceInformation>();
43 string[] requestedProperties = {
"System.Devices.Aep.DeviceAddress",
"System.Devices.Aep.IsConnected" };
44 watcher = DeviceInformation.CreateWatcher(
45 BluetoothLEDevice.GetDeviceSelectorFromPairingState(
true),
47 DeviceInformationKind.AssociationEndpoint
50 watcher.Added += (DeviceWatcher w, DeviceInformation d) =>
55 watcher.Updated += (DeviceWatcher w, DeviceInformationUpdate d) =>
57 foreach (DeviceInformation device
in devices)
59 if (device.Id == d.Id) { device.Update(d);
break; }
63 watcher.Removed += (DeviceWatcher w, DeviceInformationUpdate d) =>
65 foreach (DeviceInformation device
in devices)
67 if (device.Id == d.Id) {
devices.Remove(device);
break; }
92 callback.Invoke(
this);
107 return (
watcher !=
null &&
watcher.Status == DeviceWatcherStatus.Started);
122 public async
void Write(GattCharacteristic c,
byte[] data)
126 using (DataWriter stream =
new DataWriter())
128 stream.WriteBytes(data);
132 GattCommunicationStatus result = await c.WriteValueAsync(stream.DetachBuffer());
133 if (result != GattCommunicationStatus.Success)
135 Console.WriteLine(
string.Format(
"Write {0} on {1} failed !", BitConverter.ToString(data), c.Uuid));
140 Console.WriteLine(e.Message);
152 GattDeviceServicesResult services = await
connectedDevice.GetGattServicesAsync();
153 if (services.Status == GattCommunicationStatus.Success)
155 foreach (GattDeviceService s
in services.Services)
157 await s.RequestAccessAsync();
158 GattCharacteristicsResult characteristicsResult = await s.GetCharacteristicsAsync();
160 Console.WriteLine(
"Service : " + s.Uuid);
161 if (characteristicsResult.Status == GattCommunicationStatus.Success)
163 foreach (GattCharacteristic c
in characteristicsResult.Characteristics)
165 GattCharacteristicProperties props = c.CharacteristicProperties;
166 Console.WriteLine(
"\t characteristics : " + c.Uuid +
" / " + c.UserDescription);
167 if (props.HasFlag(GattCharacteristicProperties.Read))
169 Console.WriteLine(
"\t\tRead");
171 if (props.HasFlag(GattCharacteristicProperties.Write))
173 Console.WriteLine(
"\t\tWrite");
175 if (props.HasFlag(GattCharacteristicProperties.Notify))
177 Console.WriteLine(
"\t\tNotify");
List< DeviceInformation > devices
BluetoothLEDevice Device
bool HasDeviceConnected()
DeviceWatcher CreateWatcher()
Manages fitness tracker
async void Connect(string deviceId, OnDeviceConnected callback=null)
async void EnumeratingUsages()
Not used but can be useful for debugging
bool IsWatchingForDevice()
async void Write(GattCharacteristic c, byte[] data)
BluetoothLEDevice connectedDevice
delegate void OnDeviceConnected(BLEManager manager)