Exemplo n.º 1
0
        //public ICommand cmdConnect { get; }
        //public ICommand cmdDisconnect { get; }

        public BLEPage(IBluetoothLowEnergyAdapter adapter, BlePeripheralConnectionRequest connection)
        {
            Adapter    = adapter;
            Connection = connection;

            // i command così non si bindano... Devono essere messi all'interno di una INotifyPropertyChanged...
            //cmdConnect = new Command(async () => await Connect(true));
            //cmdDisconnect = new Command(async () => await Connect(false));

            InitializeComponent();
            oldColor = lblTitolo.BackgroundColor;


            Debug.WriteLine($"Connesso a {connection.GattServer.DeviceId} {connection.GattServer.Advertisement.DeviceName}");
            GattServer = connection.GattServer;

            try
            {
                notifyHandler = GattServer.NotifyCharacteristicValue(
                    serviceGuid,
                    buttonGuid,
                    bytes =>
                {
                    // Attento. Qui può arrivarti un solo byte o due o quattro a seconda del tipo
                    // di dato che hai devinito lato ESP32...
                    // Ora lato ESP32 ho usato un uint16_t
                    var val = BitConverter.ToUInt16(bytes, 0);
                    Debug.WriteLine($"{bytes.Count()} byte ({val}) da {buttonGuid}");

                    swESP32.IsToggled = val == 1;
                }
                    );

                Device.StartTimer(TimeSpan.FromSeconds(0.2), () =>
                {
                    AggiornaStato();
                    return(true); // True = Repeat again, False = Stop the timer
                });
            }
            catch (GattException ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }
Exemplo n.º 2
0
        public BLEPage(IBluetoothLowEnergyAdapter adapter, BlePeripheralConnectionRequest connection)
        {
            Adapter    = adapter;
            Connection = connection;

            // i command così non si bindano... Devono essere messi all'interno di una INotifyPropertyChanged...
            //cmdConnect = new Command(async () => await Connect(true));
            //cmdDisconnect = new Command(async () => await Connect(false));

            InitializeComponent();
            oldColor = lblTitolo.BackgroundColor;

            Valori = new Models.AdcValues();

            Debug.WriteLine($"Connesso a {connection.GattServer.DeviceId} {connection.GattServer.Advertisement.DeviceName}");
            GattServer = connection.GattServer;

            try
            {
                // Mi registro per ricevere le notifiche del bottone lato ESP32
                buttonNotifyHandler = GattServer.NotifyCharacteristicValue(
                    serviceGuid,
                    buttonGuid,
                    bytes =>
                {
                    // Attento. Qui può arrivarti un solo byte o due o quattro a seconda del tipo
                    // di dato che hai definito lato ESP32...
                    // Ora lato ESP32 ho usato un uint16_t
                    var valuleFromESP32 = BitConverter.ToUInt16(bytes, 0);
                    Debug.WriteLine($"{bytes.Count()} byte ({valuleFromESP32}) da {buttonGuid}");

                    swESP32.IsToggled = valuleFromESP32 == 1;
                }
                    );

                // Mi registro per ricevere le notifiche dell ADC lato ESP32
                adcNotifyHandler = GattServer.NotifyCharacteristicValue(
                    serviceGuid,
                    adcGuid,
                    bytes =>
                {
                    // Attento. Qui può arrivarti un solo byte o due o quattro a seconda del tipo
                    // di dato che hai definito lato ESP32...
                    // Ora lato ESP32 ho usato un int (signed)
                    var valuleFromESP32 = BitConverter.ToInt32(bytes, 0);
                    Debug.WriteLine($"{bytes.Count()} byte ({valuleFromESP32}) da {adcGuid}");

                    lblADCVal.Text = valuleFromESP32.ToString();
                    Valori.Add(new Models.AdcValue {
                        Time = DateTime.Now, Value = valuleFromESP32
                    });
                    if (Valori.Count > 100)
                    {
                        Valori.RemoveAt(0);
                    }

                    chart.ItemsSource = null;
                    chart.ItemsSource = Valori;
                }
                    );

                Device.StartTimer(TimeSpan.FromSeconds(0.2), () =>
                {
                    AggiornaStato();
                    return(true); // True = Repeat again, False = Stop the timer
                });
            }
            catch (GattException ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }