/// <summary>
        /// Parses the given response from the given Bluetooth device and
        /// returns its type.
        /// </summary>
        /// <param name="device">Bluetooth device that received the response.
        /// </param>
        /// <param name="response">Response received.</param>
        /// <returns>The type of the Bluetooth device.</returns>
        private string ParseResponse(BleDevice device, byte[] response)
        {
            try
            {
                // Parse the JSON of the response.
                JObject json = JObject.Parse(Encoding.UTF8.GetString(response));
                if (!JsonConstants.OP_ID.Equals((string)json[JsonConstants.ITEM_OP]) ||
                    !JsonConstants.STATUS_SUCCESS.Equals((string)json[JsonConstants.ITEM_STATUS]))
                {
                    return(null);
                }

                // Get the device type and MAC address.
                string type = (string)json[JsonConstants.ITEM_VALUE];
                string mac  = (string)json[JsonConstants.ITEM_MAC];

                if (type.Equals(BleDevice.TYPE_CONTROLLER))
                {
                    device.Mac = mac;
                }

                return(type);
            }
            catch (Exception)
            {
                // Do nothing.
            }
            return(null);
        }
Esempio n. 2
0
        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return(false);
            }

            BleDevice dev = (BleDevice)obj;

            return(Id == dev.Id);
        }
Esempio n. 3
0
        /// <summary>
        /// Method called when a list item is selected.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">Event args.</param>
        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>
        /// Determines the type of the given irrigation device.
        /// </summary>
        /// <param name="device">Bluetooth device.</param>
        /// <returns>The type of the irrigation device.</returns>
        private string DetermineDeviceType(BleDevice device)
        {
            XBeeBLEDevice xbeeDevice = device.XBeeDevice;
            string        type       = null;

            // Register an event handler for incoming data from the serial interface of the
            // Bluetooth device (XBee Gateway).
            EventHandler <SerialDataReceivedEventArgs> serialHandler = (object sender, SerialDataReceivedEventArgs e) =>
            {
                type = ParseResponse(device, e.Data);
            };

            xbeeDevice.SerialDataReceived += serialHandler;

            // Register an event handler for incoming data from the MicroPython interface of the
            // Bluetooth device (XBee module).
            EventHandler <MicroPythonDataReceivedEventArgs> mpHandler = (object sender, MicroPythonDataReceivedEventArgs e) =>
            {
                type = ParseResponse(device, e.Data);
            };

            xbeeDevice.MicroPythonDataReceived += mpHandler;

            JObject json = JObject.Parse(@"{
				'"                 + JsonConstants.ITEM_OP + "': '" + JsonConstants.OP_ID + "'" +
                                         "}");

            // Send the ID request to both interfaces, only one will respond.
            xbeeDevice.SendSerialData(Encoding.UTF8.GetBytes(json.ToString()));
            xbeeDevice.SendMicroPythonData(Encoding.UTF8.GetBytes(json.ToString()));

            // Wait until the type is received or the timeout elapses.
            long deadline = Environment.TickCount + DEV_INIT_TIMEOUT;

            while (type == null && Environment.TickCount < deadline)
            {
                Task.Delay(100);
            }

            // Unregister the callbacks.
            xbeeDevice.SerialDataReceived      -= serialHandler;
            xbeeDevice.MicroPythonDataReceived -= mpHandler;

            return(type);
        }
        /// <summary>
        /// Class constructor. Instantiates a new <c>ProvisionPage</c> object
        /// with the given parameter.
        /// </summary>
        /// <param name="bleDevice">Bluetooth device.</param>
        public ProvisionPage(BleDevice bleDevice)
        {
            InitializeComponent();
            provisionPageViewModel = new ProvisionPageViewModel(bleDevice);
            BindingContext         = provisionPageViewModel;

            // 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 irrigation device?", "Yes", "No"))
                    {
                        provisionPageViewModel.DisconnectDevice();
                    }
                };
            }

            UpdateLocation();
        }
        /// <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) =>
            {
                // Add only the devices whose name matches the configured prefix.
                if (e.Device == null || e.Device.Name == null || !e.Device.Name.ToLower().StartsWith(BLE_NAME_PREFIX.ToLower()))
                {
                    return;
                }

                BleDevice advertisedDevice = new BleDevice(e.Device);
                // If the device is not discovered, add it to the list. Otherwise update its RSSI value.
                if (!Devices.Contains(advertisedDevice))
                {
                    Devices.Add(advertisedDevice);
                }
                else
                {
                    Devices[Devices.IndexOf(advertisedDevice)].Rssi = advertisedDevice.Rssi;
                }
            };

            // 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();
            };
        }
Esempio n. 7
0
 /// <summary>
 /// Class constructor. Instantiates a new <c>ProvisionPageViewModel</c>
 /// object with the given parameters.
 /// </summary>
 /// <param name="bleDevice">Bluetooth device.</param>
 public ProvisionPageViewModel(BleDevice bleDevice)
 {
     this.bleDevice = bleDevice;
     IsController   = bleDevice.IsController;
     IsValid        = false;
 }