UNPKG

rxpoweredup

Version:

A Typescript RxJS-based library for controlling LEGO Powered UP hubs & peripherals.

26 lines (25 loc) 782 B
import { from, map } from 'rxjs'; import { HUB_SERVICE_UUID } from '../constants'; export class HubScanner { hubScannerErrorFactory; bluetoothApi; constructor(hubScannerErrorFactory, bluetoothApi) { this.hubScannerErrorFactory = hubScannerErrorFactory; this.bluetoothApi = bluetoothApi; } discoverHub() { return from(this.bluetoothApi.requestDevice({ filters: [{ services: [HUB_SERVICE_UUID] }], })).pipe(map((device) => { if (this.isDeviceWithGatt(device)) { return device; } else { throw this.hubScannerErrorFactory.createGattUnavailableError(); } })); } isDeviceWithGatt(device) { return !!device.gatt; } }