Esempio n. 1
0
        /// <summary>
        /// Invoked when the device watcher detects that the proximity sensor was added.
        /// </summary>
        /// <param name="sender">The device watcher.</param>
        /// <param name="device">The device that was added.</param>
        private async void OnProximitySensorAddedAsync(DeviceWatcher sender, DeviceInformation device)
        {
            if (this.proximitySensor == null)
            {
                var addedSensor = ProximitySensor.FromId(device.Id);

                if (addedSensor != null)
                {
                    var minimumDistanceSatisfied = true;

                    //if we care about minimum distance
                    if (this.MinimumDistanceInMillimeters > Int32.MinValue)
                    {
                        if ((this.MinimumDistanceInMillimeters > addedSensor.MaxDistanceInMillimeters) ||
                            (this.MinimumDistanceInMillimeters < addedSensor.MinDistanceInMillimeters))
                        {
                            minimumDistanceSatisfied = false;
                        }
                    }

                    if (minimumDistanceSatisfied)
                    {
                        this.proximitySensor = addedSensor;

                        await SetActiveFromReadingAsync(this.proximitySensor.GetCurrentReading());

                        this.proximitySensor.ReadingChanged += ProximitySensor_ReadingChangedAsync;
                    }
                }
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Invoked when the device watcher finds a proximity sensor
 /// </summary>
 /// <param name="sender">The device watcher</param>
 /// <param name="device">Device information for the proximity sensor that was found</param>
 private void OnProximitySensorAdded(Windows.Devices.Enumeration.DeviceWatcher sender, Windows.Devices.Enumeration.DeviceInformation device)
 {
     if (_sensor == null && ProximitySensor.FromId(device.Id) is ProximitySensor foundSensor)
     {
         _sensor = foundSensor;
     }
 }
Esempio n. 3
0
        public static async Task Initialize()
        {
            DeviceInformationCollection devices;

            CallManager = await CallManager.GetSystemPhoneCallManagerAsync();

            CallStore = await PhoneCallManager.RequestStoreAsync();

            CallHistoryStore = await PhoneCallHistoryManager.RequestStoreAsync(PhoneCallHistoryStoreAccessType.AllEntriesReadWrite);

            devices = await DeviceInformation.FindAllAsync(ProximitySensor.GetDeviceSelector());

            ProximitySensor = devices.Count > 0 ? ProximitySensor.FromId(devices.First().Id) : null;
            VibrationAccessStatus accessStatus = await VibrationDevice.RequestAccessAsync();

            if (accessStatus == VibrationAccessStatus.Allowed)
            {
                VibrationDevice = await VibrationDevice.GetDefaultAsync();
            }
            try
            {
                DefaultLine = await PhoneLine.FromIdAsync(await CallStore.GetDefaultLineAsync());
            }
            catch
            {
            }
            Initialized = true;
        }
Esempio n. 4
0
        /// <summary>
        /// Invoked when the device watcher finds a proximity sensor
        /// </summary>
        /// <param name="sender">The device watcher</param>
        /// <param name="device">Device information for the proximity sensor that was found</param>
        private async void OnProximitySensorAdded(DeviceWatcher sender, DeviceInformation device)
        {
            if (null == sensor)
            {
                ProximitySensor foundSensor = ProximitySensor.FromId(device.Id);
                if (null != foundSensor)
                {
                    if (null != foundSensor.MaxDistanceInMillimeters)
                    {
                        // Check if this is the sensor that matches our ranges.

                        // TODO: Customize these values to your application's needs.
                        // Here, we are looking for a sensor that can detect close objects
                        // up to 3cm away, so we check the upper bound of the detection range.
                        const uint distanceInMillimetersLValue = 30; // 3 cm
                        const uint distanceInMillimetersRValue = 50; // 5 cm

                        if (foundSensor.MaxDistanceInMillimeters >= distanceInMillimetersLValue &&
                            foundSensor.MaxDistanceInMillimeters <= distanceInMillimetersRValue)
                        {
                            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                            {
                                rootPage.NotifyUser("Found a proximity sensor that meets the detection range", NotifyType.StatusMessage);
                            });
                        }
                        else
                        {
                            // We'll use the sensor anyway, to demonstrate how events work.
                            // Your app may decide not to use the sensor.
                            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                            {
                                rootPage.NotifyUser("Proximity sensor does not meet the detection range, using it anyway", NotifyType.StatusMessage);
                            });
                        }
                    }
                    else
                    {
                        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                        {
                            rootPage.NotifyUser("Proximity sensor does not report detection ranges, using it anyway", NotifyType.StatusMessage);
                        });
                    }

                    if (null != foundSensor)
                    {
                        sensor = foundSensor;
                    }
                }
                else
                {
                    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        rootPage.NotifyUser("Could not get a proximity sensor from the device id", NotifyType.ErrorMessage);
                    });
                }
            }
        }
Esempio n. 5
0
        private async Task AttachAsync()
        {
            if (ApiInformation.IsApiContractPresent("Windows.Phone.PhoneContract", 1))
            {
                var devices = await DeviceInformation.FindAllAsync(ProximitySensor.GetDeviceSelector());

                if (devices.Count > 0)
                {
                    _sensor = ProximitySensor.FromId(devices[0].Id);
                    //_sensor.ReadingChanged += OnReadingChanged;

                    _controller = _sensor.CreateDisplayOnOffController();
                }
            }
        }
Esempio n. 6
0
 /// <summary>
 /// Invoked when the device watcher finds a proximity sensor
 /// </summary>
 /// <param name="sender">The device watcher</param>
 /// <param name="device">Device information for the proximity sensor that was found</param>
 private async void OnProximitySensorAdded(DeviceWatcher sender, DeviceInformation device)
 {
     if (null == sensor)
     {
         sensor = ProximitySensor.FromId(device.Id);
         if (null == sensor)
         {
             // failed to find the sensor corresponding to the id
             await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
             {
                 rootPage.NotifyUser("Could not get a proximity sensor from the device id", NotifyType.ErrorMessage);
             });
         }
     }
 }
Esempio n. 7
0
 private void OnProximitySensorAdded(DeviceWatcher sender, DeviceInformation args)
 {
     if (_sensor == null)
     {
         ProximitySensor foundSensor = ProximitySensor.FromId(args.Id);
         if (foundSensor != null)
         {
             _sensor = foundSensor;
         }
         else
         {
             // No proximity sensor found
             Debug.WriteLine("No proximity sensor found");
         }
     }
 }
 /// <summary>
 /// Invoked when the device watcher finds a proximity sensor
 /// </summary>
 /// <param name="sender">The device watcher</param>
 /// <param name="device">Device information for the proximity sensor that was found</param>
 private async void OnProximitySensorAdded(DeviceWatcher sender, DeviceInformation device)
 {
     if (null == sensor)
     {
         ProximitySensor foundSensor = ProximitySensor.FromId(device.Id);
         if (null != foundSensor)
         {
             sensor = foundSensor;
         }
         else
         {
             await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
             {
                 rootPage.NotifyUser("Could not get a proximity sensor from the device id", NotifyType.ErrorMessage);
             });
         }
     }
 }
 /// <summary>
 /// Invoked when the device watcher finds a proximity sensor
 /// </summary>
 /// <param name="sender">The device watcher</param>
 /// <param name="device">Device information for the proximity sensor that was found</param>
 private async void OnProximitySensorAdded(DeviceWatcher sender, DeviceInformation device)
 {
     if (null == sensor)
     {
         ProximitySensor foundSensor = ProximitySensor.FromId(device.Id);
         if (null != foundSensor)
         {
             // Optionally check the ProximitySensor.MaxDistanceInCentimeters/MinDistanceInCentimeters
             // properties here. Refer to Scenario 1 for details.
             sensor = foundSensor;
         }
         else
         {
             await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
             {
                 rootPage.NotifyUser("Could not get a proximity sensor from the device id", NotifyType.ErrorMessage);
             });
         }
     }
 }
Esempio n. 10
0
        async Task GetOrRegisterProximityTaskAsync()
        {
            // This technique also valid for the Pedometer where you can specify
            // a particular step count.
            var deviceInfoList = await DeviceInformation.FindAllAsync(
                ProximitySensor.GetDeviceSelector());

            var deviceInfo = deviceInfoList.FirstOrDefault();

            if (deviceInfo != null)
            {
                var proximtySensor = ProximitySensor.FromId(deviceInfo.Id);
                var threshold      = new ProximitySensorDataThreshold(proximtySensor);
                var trigger        = new SensorDataThresholdTrigger(threshold);

                this.proximityTaskRegistration = FindOrRegisterBackgroundTaskFromLibrary(
                    "Proximity Trigger",
                    trigger);
            }
        }