/// <summary> /// Event raised when the bluettoth device notify a value change /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void OnCharacteristicValueChangeEvent(object sender, Plugin.BLE.Abstractions.EventArgs.CharacteristicUpdatedEventArgs e) { if (this.eventAggregator == null) { eventAggregator = (IEventAggregator)App.Current.Container.Resolve(typeof(IEventAggregator)); } DialogBehaviourHolder beahaviour = FreeStyleLibreUtils.RespondToPacketBehaviour(e.Characteristic.Value); switch (beahaviour.ResponseType) { case PacketResponseType.Accept: eventAggregator.GetEvent <MeasureChangeEvent>().Publish(beahaviour.ReceivedData); this.MeasureServiceState = MeasureServiceState.RECEIVING_DATA; break; case PacketResponseType.AnswerBack: WriteCharacteristicAsync(MiaoMiaoProtocol.NRF_UART_RX, beahaviour.Response); break; case PacketResponseType.Refuse: this.MeasureServiceState = MeasureServiceState.REFUSED_DATA_THEN_WAIT; break; case PacketResponseType.Ignore: default: this.MeasureServiceState = MeasureServiceState.WAITING_DATA; break; } }
/// <summary> /// Read a characteristic and write an intializes /// </summary> /// <param name="characteristicUUID">The characteristic UUID to write</param> /// /// <param name="values">The values to write</param> /// <returns></returns> public async Task <bool> WriteCharacteristicAsync(String characteristicUUID, List <byte[]> values) { try { Log.Debug(LOG_TAG, "ReadDataAsync : reading characteristic=[" + characteristicUUID + "]"); InitializeBlueTooth(); Guid?characGuid = Utils.AsGuid(characteristicUUID); if (!characGuid.HasValue) { Log.Debug(LOG_TAG, "ReadDataAsync: " + nameof(characGuid) + " is NULL"); return(false); } if (this.UARTService == null) { Log.Debug(LOG_TAG, "SubsrcibeCharacteristicAsync: cannot get characteristic. " + nameof(this.UARTService) + " is NULL"); return(false); } // get RX Characteristic and Write values var rxCharacteristic = await this.UARTService.GetCharacteristicAsync(characGuid.Value); foreach (byte[] value in values) { await rxCharacteristic.WriteAsync(value); } // the state evolves here this.MeasureServiceState = MeasureServiceState.WAITING_DATA; return(true); } catch (Exception e) { DisposeAll(); Log.Error(LOG_TAG, "WriteCharacteristicAsync: " + e); return(false); } }
public override void OnCreate() { base.OnCreate(); this.MeasureServiceState = MeasureServiceState.OFF; this.Ble = CrossBluetoothLE.Current; this.Adapter = CrossBluetoothLE.Current.Adapter; //init des player MP3 playerHypo3 = new MediaPlayer(); playerHypo2 = new MediaPlayer(); playerHypo1 = new MediaPlayer(); playerHyper1 = new MediaPlayer(); playerHyper2 = new MediaPlayer(); playerHyper3 = new MediaPlayer(); // creates the notification channel this.CreateNotificationChannel(); this.CreateAlertChannel(); // Show the Notifcation this.StartService(); IEventAggregator eventAggregator = (IEventAggregator)App.Current.Container.Resolve(typeof(IEventAggregator)); appSettings = (AppSettings)App.Current.Container.Resolve(typeof(AppSettings)); // subscribe to notification measure event (INCREASING, DECREASING, CONSTANT) eventAggregator.GetEvent <NotificationMeasureEvent>().Subscribe((notification) => { this.PushMeasureNotification(notification); }); // subscribe to notification alert event eventAggregator.GetEvent <PushNotificationAlertEvent>().Subscribe((notification) => { this.PushAlertNotification(notification); }); // Subscribe to the EndReadingEvent // Possibly raised by the ReadingProtocol when the reading is over eventAggregator.GetEvent <EndReadingEvent>().Subscribe((value) => { this.MeasureServiceState = MeasureServiceState.WAITING_DATA; }); eventAggregator.GetEvent <InitMeasureServiceEvent>().Publish(""); eventAggregator.GetEvent <ExitApplicationEvent>().Subscribe(() => { RefreshWidget(); }); }