コード例 #1
0
        /// <summary>
        /// This methods executes the pairing actions
        /// </summary>
        private async Task <bool> PairDeviceAsync(Models.Device device)
        {
            ComputeScanningPropertiesIndicators(ScanningWizardState.Pairing);
            availableDevices.Clear();

            await Task.Delay(1500);

            this.HasConnected = await MeasureService.RegisterDeviceAsync(device);

            await Task.Delay(500);

            if (HasConnected)
            {
                this.HasInitializedDevice = await MeasureService.InitializeBLEServicesAsync(device, MiaoMiaoProtocol.UART_SERVICE_ID);
            }
            await Task.Delay(500);

            if (this.HasInitializedDevice)
            {
                this.HasSubscribed = await MeasureService.SubsrcibeCharacteristicAsync(MiaoMiaoProtocol.NRF_UART_TX);
            }
            await Task.Delay(500);

            if (this.HasSubscribed)
            {
                // MIAOMIAO PROTOCOL
                List <byte[]> resetPacket = new List <byte[]>();
                resetPacket.Add(FreeStyleLibreUtils.GenerateResetPacket());
                this.HasReadFirstMeasure = await MeasureService.WriteCharacteristicAsync(MiaoMiaoProtocol.NRF_UART_RX, resetPacket);
            }
            await Task.Delay(1500);

            bool HasReadCorrectData = true;

            if (this.HasReadFirstMeasure)
            {
                HasReadCorrectData = MeasureService.CurrentState() != Models.Enums.MeasureServiceState.REFUSED_DATA_THEN_WAIT;
            }



            return(this.HasConnected && this.HasInitializedDevice && this.HasSubscribed && this.HasReadFirstMeasure && HasReadCorrectData);
        }
コード例 #2
0
        /// <summary>
        /// This method is in charge of awake the device and connects to it
        /// NOTE : If an error is raised during its execution, this method calls itself (the called is post delayed)
        /// </summary>
        /// <returns></returns>
        private async Task WakeUpMeasureServiceAsync()
        {
            Log.Debug(LOG_TAG, GetType() + ".WakeUpMeasureServiceAsync: called");

            bool needToPostDelayExecution = false;

            User currentUser = this.userRepository.GetCurrentUser();

            if (!currentUser.DeviceIsBounded)
            {
                Log.Debug(LOG_TAG, GetType() + ".WakeUpMeasureServiceAsync: no device bounded. No need to awake the mesure service");
                return;
            }

            Models.Device device = new Models.Device(currentUser.DeviceId, currentUser.DeviceName);

            try
            {
                bool IsDeviceConnected = true;
                bool IsInitialized     = true;
                bool IsSuscribed       = true;
                bool IsWritten         = true;
                bool IsRead            = true;

                // step 1 : register device
                IsDeviceConnected = await this.measureService.RegisterDeviceAsync(device);

                // step 2 : initializes ble services
                if (IsDeviceConnected)
                {
                    IsInitialized = await this.measureService.InitializeBLEServicesAsync(device, MiaoMiaoProtocol.UART_SERVICE_ID);
                }

                // step 3 : subscription
                if (IsInitialized)
                {
                    IsSuscribed = await this.measureService.SubsrcibeCharacteristicAsync(MiaoMiaoProtocol.NRF_UART_TX);
                }

                // step 4 : reset
                // MIAOMIAO PROTOCOL
                if (IsSuscribed)
                {
                    List <byte[]> resetPacket = new List <byte[]>();
                    resetPacket.Add(FreeStyleLibreUtils.GenerateResetPacket());
                    IsWritten = await this.measureService.WriteCharacteristicAsync(MiaoMiaoProtocol.NRF_UART_RX, resetPacket);
                }

                // step 5 : the state of the service
                if (IsWritten)
                {
                    IsRead = this.measureService.CurrentState() != Models.Enums.MeasureServiceState.REFUSED_DATA_THEN_WAIT;
                }


                // if there was an error in of the steps, we start over
                if (!IsDeviceConnected || !IsInitialized || !IsSuscribed || !IsWritten || !IsRead)
                {
                    needToPostDelayExecution = true;

                    this.measureService.UnregisterDevice(device);
                    Log.Debug(LOG_TAG, GetType() + ".WakeUpMeasureServiceAsync: awakening is incomplete");
                }
                else
                {
                    Log.Debug(LOG_TAG, GetType() + ".WakeUpMeasureServiceAsync: awakening is complete");
                }
            }
            catch (CharacteristicReadException exception)
            {
                Log.Debug(LOG_TAG, GetType() + ".WakeUpMeasureServiceAsync: " + exception);
                if (this.measureService != null && device != null)
                {
                    this.measureService.UnregisterDevice(device);
                }

                needToPostDelayExecution = true;
            }
            finally
            {
                // post delay the execution of this method in X minutes because something went wrong
                if (needToPostDelayExecution)
                {
                    await Task.Delay(this.appSettings.MEASURE_SERVICE_RETRY_DEFAULT_TIME * 1000 * 60).ContinueWith(t =>
                                                                                                                   Xamarin.Forms.Device.BeginInvokeOnMainThread(() => { WakeUpMeasureServiceAsync(); })
                                                                                                                   );
                }
            }
        }