Last active
December 19, 2017 17:33
-
-
Save xlozinguez/93fefabefd4dd1e4b3d4b1662e84fe96 to your computer and use it in GitHub Desktop.
GATT flow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { Observable } from 'rxjs/Observable'; | |
import { BluetoothCore } from '@manekinekko/angular-web-bluetooth'; | |
@Injectable() | |
export class HeartRateService { | |
static GATT_CHARACTERISTIC_HR_MEASUREMENT = 'heart_rate_measurement'; | |
static GATT_PRIMARY_SERVICE = 'heart_rate'; | |
constructor( | |
public ble: BluetoothCore | |
) {} | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public getHeartRate() { | |
try { | |
return this.ble | |
.discover$({ | |
acceptAllDevices: true, | |
optionalServices: [HeartRateService.GATT_PRIMARY_SERVICE], | |
} as RequestDeviceOptions) | |
.mergeMap( (gatt: BluetoothRemoteGATTServer) => { | |
return this.ble.getPrimaryService$(gatt, HeartRateService.GATT_PRIMARY_SERVICE); | |
}) | |
.mergeMap( (primaryService: BluetoothRemoteGATTService) => { | |
return this.ble.getCharacteristic$(primaryService, HeartRateService.GATT_CHARACTERISTIC_HR_MEASUREMENT); | |
}) | |
.mergeMap( (characteristic: BluetoothRemoteGATTCharacteristic) => { | |
return this.ble.readValue$(characteristic); | |
}) | |
.map( (value: DataView) => { | |
return this.parseHeartRate(value); | |
}); | |
} catch (e) { | |
console.error('Oops! can not read value from %s'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment