Example #1
0
        public TemperaturePage(BleDevice device)
        {
            InitializeComponent();
            temperaturePageViewModel = new TemperaturePageViewModel(device);
            BindingContext           = temperaturePageViewModel;

            // Initialize the refresh rate picker.
            List <string> rates = new List <string>
            {
                "1 second",
                "2 seconds",
                "5 seconds",
                "10 seconds",
                "30 seconds",
                "60 seconds"
            };

            ratePicker.ItemsSource   = rates;
            ratePicker.SelectedIndex = 2;

            // Register the back button action.
            if (EnableBackButtonOverride)
            {
                CustomBackButtonAction = async() =>
                {
                    // Ask the user if wants to close the connection.
                    if (await DisplayAlert("Disconnect device", "Do you want to disconnect the XBee device?", "Yes", "No"))
                    {
                        temperaturePageViewModel.DisconnectDevice();
                    }
                };
            }
        }
        /// <summary>
        /// Class constructor. Instantiates a new <c>TemperaturePageViewModel</c> object with the
        /// given Bluetooth device.
        /// </summary>
        /// <param name="device">Bluetooth device.</param>
        public TemperaturePageViewModel(BleDevice device)
        {
            BleDevice = device;
            IsRunning = false;

            TemperatureValue = "-";
            HumidityValue    = "-";
            ValuesColor      = COLOR_DEFAULT;
        }
Example #3
0
        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return(false);
            }

            BleDevice dev = (BleDevice)obj;

            return(Id == dev.Id);
        }
        public void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            BleDevice selectedDevice = e.SelectedItem as BleDevice;

            // Clear selection.
            ((ListView)sender).SelectedItem = null;

            if (selectedDevice == null)
            {
                return;
            }

            mainPageViewModel.StopScan();
            mainPageViewModel.SelectedDevice = selectedDevice;
            mainPageViewModel.AskForPassword(false);
        }
        /// <summary>
        /// Class constructor. Instantiates a new <c>MainPageViewModel</c> object.
        /// </summary>
        public MainPageViewModel()
        {
            devices = new ObservableCollection <BleDevice>();

            // Initialize Bluetooth stuff.
            adapter = CrossBluetoothLE.Current.Adapter;

            // Subscribe to device advertisements.
            adapter.DeviceAdvertised += (object sender, DeviceEventArgs e) =>
            {
                BleDevice advertisedDevice = new BleDevice(e.Device);
                // If the device is not discovered, add it to the list.
                if (!Devices.Contains(advertisedDevice))
                {
                    Devices.Add(advertisedDevice);
                }
            };

            // Subscribe to device connection lost.
            adapter.DeviceConnectionLost += (object sender, DeviceErrorEventArgs e) =>
            {
                if (SelectedDevice == null || CrossBluetoothLE.Current.State != BluetoothState.On)
                {
                    return;
                }

                // Close the connection with the device.
                SelectedDevice.XBeeDevice.Close();

                // Return to main page and prompt the disconnection error.
                Device.BeginInvokeOnMainThread(async() =>
                {
                    await Application.Current.MainPage.Navigation.PopToRootAsync();
                    ShowErrorDialog("Device disconnected", "Bluetooth connection lost with the device.");
                });
            };
        }