public void StopReceiving() { if (this.ConnectionState == BikeConnectionState.Connected) { try { this.resistanceSetTimer.Stop(); if (this.bleBike != null) { //this.bleBike.CloseDevice(); this.bleBike.Dispose(); this.bleBike = null; } if (this.bleHeart != null) { //this.bleHeart.CloseDevice(); this.bleHeart.Dispose(); this.bleHeart = null; } } catch { // jammer dan } this.ConnectionState = BikeConnectionState.Disconnected; } }
public void StopReceiving() { if (this.ConnectionState == BikeConnectionState.Connected) { this.sendTimer.Stop(); this.ConnectionState = BikeConnectionState.Disconnected; } }
public void StartReceiving() { if (this.ConnectionState != BikeConnectionState.Connected) { this.sendTimer.Start(); this.ConnectionState = BikeConnectionState.Connected; } }
public async void StartReceiving() { if (this.ConnectionState != BikeConnectionState.Connected) { this.bleBike = new BLE(); this.bleHeart = new BLE(); await Task.Delay(1000); // We need some time to list available devices int errorCode = 0; List <string> bleBikeList = this.bleBike.ListDevices(); if (bleBikeList.Contains(this.bikeName)) { try { errorCode = await this.bleBike.OpenDevice(this.bikeName); if (errorCode != 0) { this.BikeConnectionChanged?.Invoke(this, new BikeConnectionStateChangedEventArgs( BikeConnectionState.Error, new BLEException(errorCode, $"Cannot connect to {this.bikeName}. Make sure it's turned on!"))); return; } errorCode = await this.bleBike.SetService(EspBikeTrainer.BIKE_SERVICE); if (errorCode != 0) { this.BikeConnectionChanged?.Invoke(this, new BikeConnectionStateChangedEventArgs( BikeConnectionState.Error, new BLEException(errorCode, $"Cannot connect to {this.bikeName} bike service."))); return; } this.bleBike.SubscriptionValueChanged += BleBikeValueReceived; errorCode = await this.bleBike.SubscribeToCharacteristic(EspBikeTrainer.BIKE_RECEIVE_CHARACTERISTIC); if (errorCode != 0) { this.BikeConnectionChanged?.Invoke(this, new BikeConnectionStateChangedEventArgs( BikeConnectionState.Error, new BLEException(errorCode, $"Cannot subscribe to {this.bikeName} receive characteristic."))); return; } errorCode = await this.bleHeart.OpenDevice(this.bikeName); if (errorCode != 0) { this.BikeConnectionChanged?.Invoke(this, new BikeConnectionStateChangedEventArgs( BikeConnectionState.Error, new BLEException(errorCode, $"Cannot connect to {this.bikeName}. Make sure it's turned on!"))); return; } errorCode = await this.bleHeart.SetService(EspBikeTrainer.HEART_SERVICE); if (errorCode != 0) { this.BikeConnectionChanged?.Invoke(this, new BikeConnectionStateChangedEventArgs( BikeConnectionState.Error, new BLEException(errorCode, $"Cannot connect to {this.bikeName} heartrate service."))); return; } this.bleHeart.SubscriptionValueChanged += BleHeartValueReceived; errorCode = await this.bleHeart.SubscribeToCharacteristic(EspBikeTrainer.HEART_RATE_CHARACTERISTIC); if (errorCode != 0) { this.BikeConnectionChanged?.Invoke(this, new BikeConnectionStateChangedEventArgs( BikeConnectionState.Error, new BLEException(errorCode, $"Cannot subscribe to {this.bikeName} heartrate characteristic."))); return; } else { this.ConnectionState = BikeConnectionState.Connected; // Reset Resistance this.SetResistance(0); this.resistanceSetTimer.Start(); } } catch { } finally { if (errorCode != 0) { this.resistanceSetTimer.Stop(); this.bleBike.CloseDevice(); this.bleHeart.CloseDevice(); this.bleBike.Dispose(); this.bleHeart.Dispose(); this.ConnectionState = BikeConnectionState.Disconnected; } } } else { this.BikeConnectionChanged?.Invoke(this, new BikeConnectionStateChangedEventArgs( BikeConnectionState.Error, new BLEException(errorCode, $"Cannot find {this.bikeName}. Make sure it's turned on!"))); } } }
public BikeConnectionStateChangedEventArgs(BikeConnectionState state, BLEException exception = null) { this.ConnectionState = state; this.Exception = exception; }