Exemple #1
0
        private async void SetupSensors(bool serviceAsParameter)
        {
            pbar.Visibility = Windows.UI.Xaml.Visibility.Visible;
            ClearSensors();
            btnSetup.IsEnabled       = false;
            btnSetupParam.IsEnabled  = false;
            spTestButtons.Visibility = Windows.UI.Xaml.Visibility.Collapsed;

            Exception ex = null;

            try
            {
                if (serviceAsParameter)
                {
                    acc = new Accelerometer();
                    acc.SensorValueChanged += SensorValueChanged;
                    await acc.Initialize((await GattUtils.GetDevicesOfService(acc.SensorServiceUuid))[0]);

                    await acc.EnableSensor();

                    gyro = new Gyroscope();
                    gyro.SensorValueChanged += SensorValueChanged;
                    await gyro.Initialize((await GattUtils.GetDevicesOfService(gyro.SensorServiceUuid))[0]);

                    await gyro.EnableSensor();

                    hum = new HumiditySensor();
                    hum.SensorValueChanged += SensorValueChanged;
                    await hum.Initialize((await GattUtils.GetDevicesOfService(hum.SensorServiceUuid))[0]);

                    await hum.EnableSensor();

                    ks = new SimpleKeyService();
                    ks.SensorValueChanged += SensorValueChanged;
                    await ks.Initialize((await GattUtils.GetDevicesOfService(ks.SensorServiceUuid))[0]);

                    await ks.EnableSensor();

                    mg = new Magnetometer();
                    mg.SensorValueChanged += SensorValueChanged;
                    await mg.Initialize((await GattUtils.GetDevicesOfService(mg.SensorServiceUuid))[0]);

                    await mg.EnableSensor();

                    ps = new PressureSensor();
                    ps.SensorValueChanged += SensorValueChanged;
                    await ps.Initialize((await GattUtils.GetDevicesOfService(ps.SensorServiceUuid))[0]);

                    await ps.EnableSensor();

                    tempSen = new IRTemperatureSensor();
                    tempSen.SensorValueChanged += SensorValueChanged;
                    await tempSen.Initialize((await GattUtils.GetDevicesOfService(tempSen.SensorServiceUuid))[0]);

                    await tempSen.EnableSensor();

                    spTestButtons.Visibility = Windows.UI.Xaml.Visibility.Visible;
                }
                else
                {
                    acc = new Accelerometer();
                    acc.SensorValueChanged += SensorValueChanged;
                    await acc.Initialize();

                    await acc.EnableSensor();

                    gyro = new Gyroscope();
                    gyro.SensorValueChanged += SensorValueChanged;
                    await gyro.Initialize();

                    await gyro.EnableSensor();

                    hum = new HumiditySensor();
                    hum.SensorValueChanged += SensorValueChanged;
                    await hum.Initialize();

                    await hum.EnableSensor();

                    ks = new SimpleKeyService();
                    ks.SensorValueChanged += SensorValueChanged;
                    await ks.Initialize();

                    await ks.EnableSensor();

                    mg = new Magnetometer();
                    mg.SensorValueChanged += SensorValueChanged;
                    await mg.Initialize();

                    await mg.EnableSensor();

                    ps = new PressureSensor();
                    ps.SensorValueChanged += SensorValueChanged;
                    await ps.Initialize();

                    await ps.EnableSensor();

                    tempSen = new IRTemperatureSensor();
                    tempSen.SensorValueChanged += SensorValueChanged;
                    await tempSen.Initialize();

                    await tempSen.EnableSensor();

                    spTestButtons.Visibility = Windows.UI.Xaml.Visibility.Visible;
                }
            }
            catch (Exception exc)
            {
                ex = exc;
            }

            if (ex != null)
            {
                await new MessageDialog(ex.Message).ShowAsync();
            }

            pbar.Visibility         = Windows.UI.Xaml.Visibility.Collapsed;
            btnSetup.IsEnabled      = true;
            btnSetupParam.IsEnabled = true;
        }