Ejemplo n.º 1
0
        public AdapterViewModel(ICentralManager central,
                                INavigationService navigator,
                                IUserDialogs dialogs)
        {
            this.CanControlAdapterState = central.CanControlAdapterState();

            this.WhenAnyValue(x => x.SelectedPeripheral)
            .Skip(1)
            .Where(x => x != null)
            .SubOnMainThread(x => navigator.Navigate("Peripheral", ("Peripheral", x.Peripheral)));

            this.ToggleAdapterState = ReactiveCommand.Create(
                () =>
            {
                var poweredOn = central.Status == AccessState.Available;
                if (!central.TrySetAdapterState(!poweredOn))
                {
                    dialogs.Alert("Cannot change bluetooth adapter state");
                }
            }
                );

            this.ScanToggle = ReactiveCommand.Create(
                () =>
            {
                if (this.IsScanning)
                {
                    this.IsScanning = false;
                    this.scan?.Dispose();
                }
                else
                {
                    this.Peripherals.Clear();

                    this.scan = central
                                .Scan()
                                .Buffer(TimeSpan.FromSeconds(1))
                                .Synchronize()
                                .SubOnMainThread(
                        results =>
                    {
                        var list = new List <PeripheralItemViewModel>();
                        foreach (var result in results)
                        {
                            var peripheral = this.Peripherals.FirstOrDefault(x => x.Equals(result.Peripheral));
                            if (peripheral == null)
                            {
                                peripheral = list.FirstOrDefault(x => x.Equals(result.Peripheral));
                            }

                            if (peripheral != null)
                            {
                                peripheral.Update(result);
                            }
                            else
                            {
                                peripheral = new PeripheralItemViewModel(result.Peripheral);
                                peripheral.Update(result);
                                list.Add(peripheral);
                            }
                        }
                        if (list.Any())
                        {
                            this.Peripherals.AddRange(list);
                        }
                    },
                        ex => dialogs.Alert(ex.ToString(), "ERROR")
                        )
                                .DisposeWith(this.DeactivateWith);

                    this.IsScanning = true;
                }
            }
                );
        }
Ejemplo n.º 2
0
        public AdapterViewModel(INavigationService navigator,
                                IDialogs dialogs,
                                IBleManager?bleManager = null)
        {
            this.IsScanning             = bleManager?.IsScanning ?? false;
            this.CanControlAdapterState = bleManager?.CanControlAdapterState() ?? false;

            this.WhenAnyValue(x => x.SelectedPeripheral)
            .Skip(1)
            .Where(x => x != null)
            .Subscribe(async x => await navigator.Navigate("Peripheral", ("Peripheral", x.Peripheral)));

            this.ToggleAdapterState = ReactiveCommand.CreateFromTask(
                async() =>
            {
                if (bleManager == null)
                {
                    await dialogs.Alert("Platform Not Supported");
                }
                else
                {
                    var poweredOn = bleManager.Status == AccessState.Available;
                    if (!bleManager.TrySetAdapterState(!poweredOn))
                    {
                        await dialogs.Alert("Cannot change bluetooth adapter state");
                    }
                }
            }
                );

            this.ScanToggle = ReactiveCommand.CreateFromTask(
                async() =>
            {
                if (bleManager == null)
                {
                    await dialogs.Alert("Platform Not Supported");
                    return;
                }
                if (this.IsScanning)
                {
                    this.Deactivate();
                }
                else
                {
                    this.IsScanning = true;
                    this.Peripherals.Clear();

                    bleManager
                    .Scan()
                    .Buffer(TimeSpan.FromSeconds(1))
                    .SubOnMainThread(
                        results =>
                    {
                        var list = new List <PeripheralItemViewModel>();
                        foreach (var result in results)
                        {
                            var peripheral = this.Peripherals.FirstOrDefault(x => x.Equals(result.Peripheral));
                            if (peripheral == null)
                            {
                                peripheral = list.FirstOrDefault(x => x.Equals(result.Peripheral));
                            }

                            if (peripheral != null)
                            {
                                peripheral.Update(result);
                            }
                            else
                            {
                                peripheral = new PeripheralItemViewModel(result.Peripheral);
                                peripheral.Update(result);
                                list.Add(peripheral);
                            }
                        }
                        if (list.Any())
                        {
                            // XF is not able to deal with an observablelist/addrange properly
                            foreach (var item in list)
                            {
                                this.Peripherals.Add(item);
                            }
                        }
                    },
                        ex => dialogs.Alert(ex.ToString(), "ERROR")
                        )
                    .DisposeWith(this.DeactivateWith);
                }
            }
                );
        }