Exemple #1
0
        public static async void RegisterAndRunAsync()
        {
            var taskName = typeof(CheckHeartRateInBackgroundTask).Name;
            IBackgroundTaskRegistration checkHeartRateInBackground = BackgroundTaskRegistration.AllTasks.Values.FirstOrDefault(t => t.Name == taskName);

            if (band.IsConnected() && band.Identity.IsAuthenticated())
            {
                if (checkHeartRateInBackground != null)
                {
                    checkHeartRateInBackground.Unregister(true);
                }

                var deviceTrigger = new DeviceUseTrigger();
                var deviceInfo    = await band.Identity.GetPairedBand();

                var taskBuilder = new BackgroundTaskBuilder
                {
                    Name               = taskName,
                    TaskEntryPoint     = typeof(CheckHeartRateInBackgroundTask).ToString(),
                    IsNetworkRequested = false
                };

                taskBuilder.SetTrigger(deviceTrigger);
                BackgroundTaskRegistration task = taskBuilder.Register();

                await deviceTrigger.RequestAsync(deviceInfo.Id);
            }
        }
        public static async void RegisterAndRunAsync(DeviceInformation devInfo)
        {
            var taskName = typeof(CheckHeartRateInBackgroundTask).ToString();

            IBackgroundTaskRegistration checkHeartRateInBackground = BackgroundTaskRegistration.AllTasks.Values.FirstOrDefault(t => t.Name == taskName);

            if (band.IsConnected() && band.Identity.IsAuthenticated())
            {
                if (checkHeartRateInBackground != null)
                {
                    // if the task is already executing, unregister it
                    checkHeartRateInBackground.Unregister(true);
                }

                var devTrigger  = new DeviceUseTrigger();
                var taskBuilder = new BackgroundTaskBuilder {
                    Name               = taskName,
                    TaskEntryPoint     = typeof(CheckHeartRateInBackgroundTask).ToString(),
                    IsNetworkRequested = false
                };

                taskBuilder.SetTrigger(devTrigger);

                BackgroundTaskRegistration task = taskBuilder.Register();
                task.Completed += Task_Completed;
                task.Progress  += Task_Progress;
                await devTrigger.RequestAsync(devInfo.Id);
            }
        }
 private async void Button_Click(object sender, RoutedEventArgs e)
 {
     btnConn.IsEnabled = false;
     band = new MiBand2SDK.MiBand2();
     // Check if already authentified
     if (band.IsConnected())
     {
         txtStat.Text = "Connected";
     }
     else
     {
         // Trying to connect to band and authenticate first time.
         // While authentication process started, you need to touch you band, when you see the message.
         if (await band.ConnectAsync() && await band.Identity.AuthenticateAsync())
         {
             txtStat.Text = "Connected";
         }
         else//didnt connect :(
         {
             btnConn.IsEnabled = true;
         }
     }
 }