Exemplo n.º 1
0
        public IObservable <BluetoothDeviceStatus> Watch(BluetoothDevice device)
        {
            return(Observable.Create <BluetoothDeviceStatus>(observer =>
            {
                BluetoothDeviceStatus?lastStatus = null;

                return Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(UpdateIntervalInSeconds), TaskPoolScheduler.Default).Subscribe(_ =>
                {
                    try
                    {
                        var discoverDevicesInRange = _bluetoothDeviceProvider.DiscoverDevicesInRange();
                        discoverDevicesInRange.Wait();
                        var devices = discoverDevicesInRange.Result;

                        var newStatus = BluetoothDeviceStatus.None;

                        if (devices.Any(d => d.DeviceName == device.DeviceName))
                        {
                            newStatus = BluetoothDeviceStatus.Available;
                        }

                        if (lastStatus == null || lastStatus.Value != newStatus)
                        {
                            lastStatus = newStatus;
                            observer.OnNext(newStatus);
                        }
                    }
                    catch (Exception e)
                    {
                    }
                });
            }));
        }
Exemplo n.º 2
0
 public async void DiscoverDevices()
 {
     try
     {
         IsLoading = true;
         Devices   = await _bluetoothDeviceProvider.DiscoverDevicesInRange();
     }
     catch (Exception ex)
     {
         MessageBox.Show("Error while discovering nearby devices");
     }
     finally
     {
         IsLoading = false;
     }
 }