private async Task DiscoverDevices() { var devices = await CargoClient.GetConnectedDevicesAsync(); if (devices.Count() > 0) { // must create on the UI thread for various Binding reasons _deviceInfo = devices[0]; Application.Current.Dispatcher.BeginInvoke(new Action(async() => { // TODO: support more than one device? InitializeCargoLogging(); CargoClient = await CargoClient.CreateAsync(_deviceInfo); IsConnected = true; //TODO: call an "OnConnected" function Properties = new BandProperties(CargoClient); Theme = new BandTheme(CargoClient); Sensors = new BandSensors(CargoClient); Tiles = new BandTiles(CargoClient); })); } }
public static void Start() { if (Instance == null) { Create(); } // While I don't love the whole Timer(1) thing, it simply means that we trigger the first run immediately // TODO: Since this could potentially cause race conditions if someone really wanted to garauntee order // of execution we could split out object creation from Start(). Timer timer = new Timer(1); timer.Elapsed += async(sender, e) => { if (!Instance.IsConnected) { await Instance.DiscoverDevices(); } else { // make sure we still have devices var devices = await CargoClient.GetConnectedDevicesAsync(); if (!(devices.Count() > 0 && devices[0].Id == Instance._deviceInfo.Id)) { Instance.IsConnected = false; } } // only reset once we finished processing timer.Interval = 1000; timer.Start(); }; timer.AutoReset = false; timer.Start(); }