Skip to content

Instantly share code, notes, and snippets.

@xlozinguez
Last active December 19, 2017 17:33
Show Gist options
  • Save xlozinguez/93fefabefd4dd1e4b3d4b1662e84fe96 to your computer and use it in GitHub Desktop.
Save xlozinguez/93fefabefd4dd1e4b3d4b1662e84fe96 to your computer and use it in GitHub Desktop.
GATT flow
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
) {}
...
}
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