-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.cs
90 lines (71 loc) · 3.96 KB
/
App.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
namespace bandview
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Android.Content;
using FakeBand.Fakes;
using Microsoft.Band;
using Microsoft.Band.Sensors;
using Microsoft.Practices.Unity;
public static class App
{
private const int ConnectTimeout = 10000;
public static UnityContainer Container { get; } = new UnityContainer();
// no thread-safety guarantees here, call only from UI thread
public static async Task<bool> ConnectToBand(Context ctx, Action<string> progressReporter = null)
{
FakeBandClientManager.Configure(new FakeBandClientManagerOptions
{
Bands = new List<IBandInfo>
{
new FakeBandInfo(BandConnectionType.Bluetooth, "Fake Band"),
}
});
var bandClientManager = FakeBand.Fakes.FakeBandClientManager.Instance;
var pairedBands = await bandClientManager.GetBandsAsync();
var bandInfo = pairedBands.FirstOrDefault();
if (bandInfo == null)
{
progressReporter?.Invoke(ctx.GetString((Resource.String.status_notfound)));
return false;
}
progressReporter?.Invoke($"{ctx.GetString(Resource.String.status_connecting)} {bandInfo.Name}");
var connectTask = bandClientManager.ConnectAsync(bandInfo);
if (await Task.WhenAny(connectTask, Task.Delay(ConnectTimeout)) == connectTask)
{
var bandClient = await connectTask;
if (bandClient == null)
{
progressReporter?.Invoke($"{ctx.GetString(Resource.String.status_failed)}");
return false;
}
progressReporter?.Invoke($"{ctx.GetString(Resource.String.status_connected)} {bandInfo.Name}");
BindSensors(bandClient);
return true;
}
else
{
progressReporter?.Invoke($"{ctx.GetString(Resource.String.status_failed)}");
return false;
}
}
private static void BindSensors(IBandClient bandClient)
{
Container.RegisterInstance(typeof(IBandSensor<IBandAccelerometerReading>), string.Empty, bandClient.SensorManager.Accelerometer, new ContainerControlledLifetimeManager());
Container.RegisterInstance(typeof(IBandSensor<IBandHeartRateReading>), string.Empty, bandClient.SensorManager.HeartRate, new ContainerControlledLifetimeManager());
Container.RegisterInstance(typeof(IBandSensor<IBandPedometerReading>), string.Empty, bandClient.SensorManager.Pedometer, new ContainerControlledLifetimeManager());
Container.RegisterInstance(typeof(IBandSensor<IBandRRIntervalReading>), string.Empty, bandClient.SensorManager.RRInterval, new ContainerControlledLifetimeManager());
Container.RegisterInstance(typeof(IBandSensor<IBandSkinTemperatureReading>), string.Empty, bandClient.SensorManager.SkinTemperature, new ContainerControlledLifetimeManager());
Container.RegisterInstance(typeof(IBandSensor<IBandUVReading>), string.Empty, bandClient.SensorManager.UV, new ContainerControlledLifetimeManager());
Container.RegisterInstance(typeof(IBandSensor<IBandGsrReading>), string.Empty, bandClient.SensorManager.Gsr, new ContainerControlledLifetimeManager());
Container.RegisterInstance(typeof(IBandSensor<IBandAltimeterReading>), string.Empty, bandClient.SensorManager.Altimeter, new ContainerControlledLifetimeManager());
Container.RegisterInstance(typeof(IBandSensor<IBandAmbientLightReading>), string.Empty, bandClient.SensorManager.AmbientLight, new ContainerControlledLifetimeManager());
Container.RegisterInstance(typeof(IBandSensor<IBandBarometerReading>), string.Empty, bandClient.SensorManager.Barometer, new ContainerControlledLifetimeManager());
Container.RegisterInstance(typeof(IBandSensor<IBandCaloriesReading>), string.Empty, bandClient.SensorManager.Calories, new ContainerControlledLifetimeManager());
Container.RegisterInstance(typeof(IBandSensor<IBandGyroscopeReading>), string.Empty, bandClient.SensorManager.Gyroscope, new ContainerControlledLifetimeManager());
Container.RegisterInstance(typeof(IBandSensor<IBandDistanceReading>), string.Empty, bandClient.SensorManager.Distance, new ContainerControlledLifetimeManager());
}
}
}