Ejemplo n.º 1
0
        private async void readBLEPeriodically()
        {
            bool rsp;

            while (true)
            {
                await Task.Delay(1000);

                if (this.bleSerial != null && this.SerialInitialized)
                {
                    try
                    {
                        do
                        {
                            await bglib.SendCommandAsync(this.bleSerial, bglib.BLECommandSystemReset(0));

                            rsp = await this.bleSemaphore.WaitAsync(2000);
                        } while (!rsp);

                        if (rsp)
                        {
                            await bglib.SendCommandAsync(this.bleSerial, bglib.BLECommandSystemHello());

                            rsp = await this.bleSemaphore.WaitAsync(2000);
                        }
                        else
                        {
                            this.ErrorMessage.Text = "Semaphore timeout";
                        }

                        if (rsp)
                        {
                            byte[] bleAddress = new byte[] { SENSORTAG_ADDRESS_0, SENSORTAG_ADDRESS_1, SENSORTAG_ADDRESS_2, SENSORTAG_ADDRESS_3, SENSORTAG_ADDRESS_4, SENSORTAG_ADDRESS_5 };
                            await bglib.SendCommandAsync(this.bleSerial, bglib.BLECommandGAPConnectDirect(bleAddress, 0, BLECONNINTERVALMIN, BLECINNINTERVANMAX, BLECONNTIMEOUT, BLECONNSLAVELATENCY));

                            rsp = await this.bleSemaphore.WaitAsync(2000);
                        }
                        else
                        {
                            this.ErrorMessage.Text = "Semaphore timeout";
                        }

                        // Config temperature sensor period
                        if (rsp)
                        {
                            byte[] temperaturePeriod = new byte[] { 100 };
                            await bglib.SendCommandAsync(this.bleSerial, bglib.BLECommandATTClientAttributeWrite(this.bleConnectionHndl, BLETEMPPERIODHNDL, temperaturePeriod));

                            rsp = await this.bleSemaphore.WaitAsync(2000);
                        }
                        else
                        {
                            this.ErrorMessage.Text = "Semaphore timeout";
                        }

                        // Config light sensor period
                        if (rsp)
                        {
                            byte[] lightPeriod = new byte[] { 100 };
                            await bglib.SendCommandAsync(this.bleSerial, bglib.BLECommandATTClientAttributeWrite(this.bleConnectionHndl, BLELIGHTPERIODHNDL, lightPeriod));

                            rsp = await this.bleSemaphore.WaitAsync(2000);
                        }
                        else
                        {
                            this.ErrorMessage.Text = "Semaphore timeout";
                        }

                        // Start temperature measurement
                        if (rsp)
                        {
                            byte[] temperatureConfig = new byte[] { 0x01 };
                            await bglib.SendCommandAsync(this.bleSerial, bglib.BLECommandATTClientAttributeWrite(this.bleConnectionHndl, BLETEMPCONFIGHNDL, temperatureConfig));

                            rsp = await this.bleSemaphore.WaitAsync(2000);
                        }
                        else
                        {
                            this.ErrorMessage.Text = "Semaphore timeout";
                        }

                        // Start light measurement
                        if (rsp)
                        {
                            byte[] lightConfig = new byte[] { 0x01 };
                            await bglib.SendCommandAsync(this.bleSerial, bglib.BLECommandATTClientAttributeWrite(this.bleConnectionHndl, BLELIGHTCONFIGHNDL, lightConfig));

                            rsp = await this.bleSemaphore.WaitAsync(2000);
                        }
                        else
                        {
                            this.ErrorMessage.Text = "Semaphore timeout";
                        }

                        while (rsp)
                        {
                            await Task.Delay(1000);

                            // Read temperature data
                            if (rsp)
                            {
                                await bglib.SendCommandAsync(this.bleSerial, bglib.BLECommandATTClientReadByHandle(this.bleConnectionHndl, BLETEMPDATAHNDL));

                                rsp = await this.bleSemaphore.WaitAsync(2000);
                            }
                            else
                            {
                                this.ErrorMessage.Text = "Semaphore timeout";
                            }

                            // Read light data
                            if (rsp)
                            {
                                await bglib.SendCommandAsync(this.bleSerial, bglib.BLECommandATTClientReadByHandle(this.bleConnectionHndl, BLELIGHTDATAHNDL));

                                rsp = await this.bleSemaphore.WaitAsync(2000);
                            }
                            else
                            {
                                this.ErrorMessage.Text = "Semaphore timeout";
                            }
                        }

                        await bglib.SendCommandAsync(this.bleSerial, bglib.BLECommandConnectionDisconnect(this.bleConnectionHndl));
                    }
                    catch (Exception ex)
                    {
                        this.ErrorValue.Text   = "BGLib error!";
                        this.ErrorMessage.Text = ex.Message;
                    }
                }
            }
        }