-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainPage.xaml.cs
218 lines (184 loc) · 9.71 KB
/
MainPage.xaml.cs
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
Copyright (c) Microsoft Corporation All rights reserved.
MIT License:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
using Microsoft.Band;
using Microsoft.Band.Sensors;
using Microsoft.Band.Tiles;
using Microsoft.Band.Notifications;
namespace MSBandTestWindowsPhone
{
public sealed partial class MainPage : Page
{
private const string tileId = "{87281FA7-4576-4533-A3E9-8D7BEF7CEEE4}";
private IBandClient bandClientCopy;
private int tilesRemaining;
public IBandHeartRateReading heartReading;
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
/// <summary>
/// Connect to Microsoft Band and read Accelerometer data.
/// </summary>
public async void GetTemp(object sender, RoutedEventArgs e)
{
try
{
// Get the list of Microsoft Bands paired to the phone.
IBandInfo[] pairedBands = await BandClientManager.Instance.GetBandsAsync();
if (pairedBands.Length < 1)
{
this.warnings.Text = "This sample app requires a Microsoft Band paired to your phone. Also make sure that you have the latest firmware installed on your Band, as provided by the latest Microsoft Health app.";
return;
}
using (IBandClient bandClient = await BandClientManager.Instance.ConnectAsync(pairedBands[0]))
{
// normally wouldn't do it this way obviously
bandClientCopy = bandClient;
tilesRemaining = await bandClient.TileManager.GetRemainingTileCapacityAsync();
if (tilesRemaining > 0)
{
Guid myTileId = new Guid(tileId);
BandTile myTile = new BandTile(myTileId)
{
Name = "My Tile",
IsBadgingEnabled = true,
TileIcon = await LoadIcon("ms-appx:///Assets/SampleTileIconLarge.png"),
SmallIcon = await LoadIcon("ms-appx:///Assets/SampleTileIconSmall.png")
};
//await bandClient.TileManager.AddTileAsync(myTile);
}
bandClient.SensorManager.SkinTemperature.ReadingChanged += SkinTemperature_ReadingChanged;
await bandClient.SensorManager.SkinTemperature.StartReadingsAsync();
await Task.Delay(TimeSpan.FromMinutes(1));
await bandClient.SensorManager.SkinTemperature.StopReadingsAsync();
}
}
catch (Exception ex)
{
this.warnings.Text = ex.ToString();
}
}
public async void GetHr(object sender, RoutedEventArgs e)
{
try
{
// Get the list of Microsoft Bands paired to the phone.
IBandInfo[] pairedBands = await BandClientManager.Instance.GetBandsAsync();
if (pairedBands.Length < 1)
{
this.warnings.Text = "This sample app requires a Microsoft Band paired to your phone. Also make sure that you have the latest firmware installed on your Band, as provided by the latest Microsoft Health app.";
return;
}
using (IBandClient bandClient = await BandClientManager.Instance.ConnectAsync(pairedBands[0]))
{
// normally wouldn't do it this way obviously
bandClientCopy = bandClient;
tilesRemaining = await bandClient.TileManager.GetRemainingTileCapacityAsync();
if (tilesRemaining > 0)
{
Guid myTileId = new Guid(tileId);
BandTile myTile = new BandTile(myTileId)
{
Name = "My Tile",
IsBadgingEnabled = true,
TileIcon = await LoadIcon("ms-appx:///Assets/SampleTileIconLarge.png"),
SmallIcon = await LoadIcon("ms-appx:///Assets/SampleTileIconSmall.png")
};
//await bandClient.TileManager.AddTileAsync(myTile);
}
IEnumerable<TimeSpan> supportedHeartBeatReportingIntervals = bandClient.SensorManager.HeartRate.SupportedReportingIntervals;
//foreach (var ri in supportedHeartBeatReportingIntervals)
//{
// Debug.WriteLine(ri.ToString());
//}
bandClient.SensorManager.HeartRate.ReportingInterval = supportedHeartBeatReportingIntervals.First<TimeSpan>();
await bandClient.SensorManager.HeartRate.RequestUserConsentAsync();
bandClient.SensorManager.HeartRate.ReadingChanged += HeartRate_ReadingChanged;
bandClient.SensorManager.HeartRate.ReadingChanged += async (senderino, args) =>
{
heartReading = args.SensorReading;
await warnings.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () =>
{
this.warnings.Text = heartReading.HeartRate.ToString();
});
};
try
{
await bandClient.SensorManager.HeartRate.StartReadingsAsync();
}
catch (BandException ex)
{
throw ex;
}
await Task.Delay(TimeSpan.FromMinutes(1));
await bandClient.SensorManager.HeartRate.StopReadingsAsync();
}
}
catch (Exception ex)
{
this.warnings.Text = ex.ToString();
}
}
private async void HeartRate_ReadingChanged(object sender, BandSensorReadingEventArgs<IBandHeartRateReading> e)
{
IBandHeartRateReading heartRateReading = e.SensorReading;
string text = string.Format("HR: {0}bpm", heartRateReading.HeartRate);
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { this.heartRate.Text = text; }).AsTask();
}
private async void SkinTemperature_ReadingChanged(object sender, BandSensorReadingEventArgs<IBandSkinTemperatureReading> e)
{
IBandSkinTemperatureReading temperatureReading = e.SensorReading;
string text = string.Format("Temperature: {0}C", temperatureReading.Temperature);
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { this.textBlock.Text = text; }).AsTask();
//if (tilesRemaining > 0)
// await bandClientCopy.NotificationManager.SendMessageAsync(Guid.Parse(tileId), text, "Your temperature reading has been taken.", DateTimeOffset.Now, MessageFlags.ShowDialog);
}
private async void Accelerometer_ReadingChanged(object sender, BandSensorReadingEventArgs<IBandAccelerometerReading> e)
{
IBandAccelerometerReading accel = e.SensorReading;
string text = string.Format("X = {0}\nY = {1}\nZ = {2}", accel.AccelerationX, accel.AccelerationY, accel.AccelerationZ);
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { this.textBlock.Text = text; }).AsTask();
}
private async void RemoveTileButton_Click(object sender, RoutedEventArgs e)
{
await bandClientCopy.TileManager.RemoveTileAsync(Guid.Parse(tileId));
}
private async Task<BandIcon> LoadIcon(string uri)
{
StorageFile imageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(uri));
using (IRandomAccessStream fileStream = await imageFile.OpenAsync(FileAccessMode.Read))
{
WriteableBitmap bitmap = new WriteableBitmap(1, 1);
await bitmap.SetSourceAsync(fileStream);
return bitmap.ToBandIcon();
}
}
private void textBlock_SelectionChanged(object sender, RoutedEventArgs e)
{
}
}
}