Exemple #1
0
        void OnListViewDeviceSelected(object sender, SelectedItemChangedEventArgs e)
        {
            iDevice selectedDevise = e.SelectedItem as iDevice;

            selectedDevise.pair();
            selectedDevise.connect();
            Navigation.PopToRootAsync();
        }
        public DeviseListViewModel()
        {
            adapter = CrossBleAdapter.Current;
            Devices = devices;

            OpenSettings = ReactiveCommand.Create(() =>
            {
                if (this.adapter.Features.HasFlag(AdapterFeatures.OpenSettings))
                {
                    this.adapter.OpenSettings();
                }
                else
                {
                    this.dialogs.Alert("Cannot open bluetooth settings");
                }
            });

            ToggleAdapterState = ReactiveCommand.Create(() =>
            {
                if (adapter.CanControlAdapterState())
                {
                    var poweredOn = adapter.Status == AdapterStatus.PoweredOn;
                    adapter.SetAdapterState(!poweredOn);
                }
                else
                {
                    dialogs.Alert("Cannot change bluetooth adapter state");
                }
            });

            ScanToggle = ReactiveCommand.Create(() =>
            {
                if (IsScanning)
                {
                    scan.Dispose();
                    IsScanning = false;
                }
                else
                {
                    Devices.Clear();
                    IsScanning = true;
                    scan       = adapter.Scan().Subscribe(scanResult =>
                    {
                        var ad     = scanResult.AdvertisementData;
                        var record = new iDevice
                        {
                            Name          = scanResult.Device.Name,
                            Rssi          = scanResult.Rssi,
                            Uuid          = scanResult.Device.Uuid,
                            ServiceCount  = ad.ServiceUuids?.Length ?? 0,
                            IsConnectable = ad.IsConnectable,
                            LocalName     = ad.LocalName,
                            TxPower       = ad.TxPower,
                            BtDevice      = scanResult.Device
                        };

                        if (!Devices.Any(device => device.Uuid == record.Uuid))
                        {
                            Devices.Add(record);
                        }
                    }).DisposeWith(this.DeactivateWith);
                }
            }
                                                );
        }