-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBleBatteryLevelService.cs
More file actions
119 lines (99 loc) · 3.85 KB
/
Copy pathBleBatteryLevelService.cs
File metadata and controls
119 lines (99 loc) · 3.85 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Enumeration;
using Windows.Storage.Streams;
namespace SensorTag
{
public class BleBatteryLevelService : BleGenericGattService
{
public BleBatteryLevelService()
{
}
public event EventHandler<BatteryLevelMeasurementEventArgs> BatteryLevelMeasurementValueChanged;
public async Task<IEnumerable<BleGattDeviceInfo>> FindMatchingDevices()
{
var devices = await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(
GattCharacteristicUuids.BatteryLevel), new string[] {
CONTAINER_ID_PROPERTY_NAME
});
List<BleGattDeviceInfo> result = new List<BleGattDeviceInfo>();
foreach (DeviceInformation device in devices)
{
result.Add(new BleGattDeviceInfo(device));
}
return result;
}
private void OnBatteryLevelMeasurementValueChanged(BatteryLevelMeasurementEventArgs args)
{
if (BatteryLevelMeasurementValueChanged != null)
{
BatteryLevelMeasurementValueChanged(this, args);
}
}
public async Task<bool> ConnectAsync(string deviceContainerId)
{
bool rc = await this.ConnectAsync(GattServiceUuids.Battery, deviceContainerId);
if (rc)
{
this.RegisterForValueChangeEvents(GattCharacteristicUuids.BatteryLevel);
}
return rc;
}
protected override void OnCharacteristicValueChanged(GattCharacteristic sender, GattValueChangedEventArgs eventArgs)
{
if (sender.Uuid == GattCharacteristicUuids.BatteryLevel)
{
if (BatteryLevelMeasurementValueChanged != null)
{
byte level = 0;
using (DataReader reader = DataReader.FromBuffer(eventArgs.CharacteristicValue))
{
level = reader.ReadByte();
}
OnBatteryLevelMeasurementValueChanged(new BatteryLevelMeasurementEventArgs() { BatteryLevel = level, Timestamp = eventArgs.Timestamp });
}
}
}
public async Task<byte> ReadBatteryLevelAsync(BluetoothCacheMode cacheMode = BluetoothCacheMode.Uncached)
{
GattStatus gattStatus = GattStatus.Unsupported;
var batteryLevelCharacteristic = this.GetCharacteristic(GattCharacteristicUuids.BatteryLevel);
if (batteryLevelCharacteristic == null)
{
gattStatus = GattStatus.UnknownCharacteristic;
}
else
{
GattReadResult readResult = await batteryLevelCharacteristic.ReadValueAsync(cacheMode);
gattStatus = (GattStatus)readResult.Status;
if (gattStatus == GattStatus.Success)
{
using (DataReader reader = DataReader.FromBuffer(readResult.Value))
{
return reader.ReadByte();
}
}
}
OnError("ReadBatteryLevelAsync:" + gattStatus.ToString());
return 0;
}
}
public class BatteryLevelMeasurementEventArgs : EventArgs
{
public byte BatteryLevel
{
get;
set;
}
public DateTimeOffset Timestamp
{
get;
set;
}
}
}