public async Task <CharacteristicGattResult> GetNotifyResult(IGattCharacteristic characteristic) { await characteristic.EnableNotifications(); var TXN = await characteristic.RegisterAndNotify(); return(await characteristic.WhenNotificationReceived()); }
async void btnRecord_Clicked(object sender, EventArgs e) { btnTapped((Button)sender); if (device == null || !device.IsConnected()) { return; } if (btnRecord.Text == "Stop Recording") { await write.Write(Encoding.ASCII.GetBytes("0")); cancel = true; btnRecord.Text = "Record ECG"; return; } sendContainer.IsVisible = false; if (dat1.Count != 0) { dat1.Clear(); } if (results != null) { results.Dispose(); } results = notify.RegisterAndNotify().Subscribe(n => { var dataString = Encoding.UTF8.GetString(n.Data); try { list = JsonConvert.DeserializeObject <DataModel>(dataString); dat1.AddRange(list.l1); } catch { Debug.WriteLine("cant convert"); Debug.WriteLine(dataString); } Debug.WriteLine(stopwatchTest.ElapsedMilliseconds + ", " + dat1.Count()); }); await write.Write(Encoding.ASCII.GetBytes(platform)); await write.Write(Encoding.ASCII.GetBytes(mTime.SelectedItem.ToString().Split(' ')[0])); stopwatchTest.Restart(); await Task.Delay(1000); //Debug.WriteLine(dat1.Count); btnRecord.Text = "Stop Recording"; draw(int.Parse(mTime.SelectedItem.ToString().Split(' ')[0]) * sps); }
public IObservable <IRadioPeripheralCharacteristic> WhenNotificationReceived() { return(Observable.Create <IRadioPeripheralCharacteristic>((observer) => { NotificationSubscription = Characteristic.RegisterAndNotify() .Subscribe((_) => observer.OnNext(this)); return Disposable.Create(async() => { NotificationSubscription?.Dispose(); NotificationSubscription = null; }); })); }
/// <summary> /// Authenticates Mi Band 3 devices /// </summary> /// <exception cref="NullReferenceException">Throws exception if AuthCharacteristic could not be found.</exception> /// <exception cref="ConnectionException">Throws exception if authentication went wrong.</exception> public async Task Authenticate() { _authCharacteristic = _miBand3.GetCharacteristic(MiBand3Resource.GuidCharacteristicAuth); if (_authCharacteristic != null) { //Fired when Mi Band 3 is tapped AuthenticationDisposable?.Dispose(); AuthenticationDisposable = _authCharacteristic.RegisterAndNotify().Subscribe(async result => { var data = result.Data; if (data == null) { _miBand3.ConnectionCallback(ConnectionResult.Failed, null); throw new NullReferenceException("No data found in authentication-result."); } //Check if response is valid if (data[0] == MiBand3Resource.AuthResponse && data[2] == MiBand3Resource.AuthSuccess) { if (data[1] == MiBand3Resource.AuthSendKey) { await RequestAuthorizationNumber(); } else if (data[1] == MiBand3Resource.AuthRequestRandomAuthNumber) { await RequestRandomEncryptionKey(data); } else if (data[1] == MiBand3Resource.AuthSendEncryptedAuthNumber) { Trace.WriteLine("Authenticated & Connected!"); _miBand3.Authenticated = true; _miBand3.ConnectionCallback(ConnectionResult.Succeeded, _miBand3.SecretKey); AuthenticationDisposable.Dispose(); return; } } else { _miBand3.Authenticated = false; _miBand3.ConnectionCallback(ConnectionResult.Failed, null); _miBand3.Disconnect(); } }, exception => { _miBand3.ConnectionCallback(ConnectionResult.Failed, null); throw new ConnectionException(exception.Message); }); if (_miBand3.SecretKey == null) { //Triggers vibration on device await TriggerAuthentication(); } else { //Continues session with authorization-number await RequestAuthorizationNumber(); } } else { _miBand3.ConnectionCallback(ConnectionResult.Failed, null); throw new NullReferenceException("AuthCharacteristic is null!"); } }
private void RegisterCharacteristicObservers(IGattCharacteristic characteristic) { /* * Read Once Characteristics */ if (characteristic.Uuid == GattConstants.SYSTEM_ID_UUID) { characteristic.Read().Subscribe(result => { Id = _hexStringConversion.Convert(result.Data); }); } else if (characteristic.Uuid == GattConstants.MANUFACTURER_NAME_UUID) { characteristic.Read().Subscribe(result => { ManufacturerName = _stringConversion.Convert(result.Data); }); } else if (characteristic.Uuid == GattConstants.MODEL_NUMBER_UUID) { characteristic.Read().Subscribe(result => { ModelNumber = _stringConversion.Convert(result.Data); }); } else if (characteristic.Uuid == GattConstants.HARDWARE_REVISION_UUID) { characteristic.Read().Subscribe(result => { HardwareRevision = _stringConversion.Convert(result.Data); }); } else if (characteristic.Uuid == GattConstants.FIRMWARE_REVISION_UUID) { characteristic.Read().Subscribe(result => { FirmwareRevision = _stringConversion.Convert(result.Data).Replace('-', '.'); }); } else if (characteristic.Uuid == GattConstants.SOFTWARE_REVISION_UUID) { characteristic.Read().Subscribe(result => { SoftwareRevision = _stringConversion.Convert(result.Data).Replace('-', '.'); }); } else if (characteristic.Uuid == GattConstants.BATTERY_LEVEL_UUID) { characteristic.Read().Subscribe(result => BatteryLevel = _byteConversion.Convert(result.Data)); } /* * Notify Characteristics */ if (characteristic.Uuid == GattConstants.BATTERY_LEVEL_UUID) { characteristic.RegisterAndNotify().Subscribe(result => BatteryLevel = _byteConversion.Convert(result.Data)); } else if (characteristic.Uuid == GattConstants.HEART_RATE_UUID) { characteristic.RegisterAndNotify().Subscribe(result => HeartRate = _heartRateConversion.Convert(result.Data)); } else if (characteristic.Uuid == GattConstants.SPO2_UUID) { characteristic.RegisterAndNotify().Subscribe(result => { }); } else if (characteristic.Uuid == GattConstants.TEMPERATURE_MEASUREMENT_UUID) { characteristic.RegisterAndNotify(true).Subscribe(result => BodyTemperature = _temperatureConversion.Convert(result.Data)); } else if (characteristic.Uuid == GattConstants.SENSOR_QUALITY_INDEX_UUID) { characteristic.RegisterAndNotify().Subscribe(result => SensorQualityIndex = _byteConversion.Convert(result.Data)); } /* * Write Raw Data Characteristic and Start Notify */ if (characteristic.Uuid == GattConstants.RAW_DATA_UUID) { var rawDataCharacteristic = characteristic; characteristic.Write(RAW_DATA_KEY).Subscribe(result => { rawDataCharacteristic.RegisterAndNotify().Subscribe(notifyResult => { var convertedRawData = _rawConversion.Convert(notifyResult.Data); if (convertedRawData.StepFrequency != null) { this.StepFrequency = convertedRawData.StepFrequency ?? this.StepFrequency; } if (convertedRawData.Accelerometer != null) { this.Accelerometer = convertedRawData.Accelerometer; } }); }); } }