import { Component , OnInit } from '@angular/core' ; import { BleManagerService } from '../../services/bleManager/ble-manager.service' ; import { UIAlert } from '../../services/uiAlert' ; @ Component ( { selector : 'scale-teset-page' , templateUrl : './scale-test.page.html' , styleUrls : [ './scale-test.page.scss' ] , } ) export class ScaleTestPage implements OnInit { private interval : any = undefined ; constructor ( private readonly bleManager , private readonly uiAlert ) { } public ngOnInit ( ) { } public async findAndConnectScale ( ) { const hasLocationPermission = await this . bleManager . hasLocationPermission ( ) ; if ( ! hasLocationPermission ) { await this . uiAlert . showMessage ( 'SCALE.REQUEST_PERMISSION.LOCATION' , undefined , undefined , true ) ; await this . bleManager . requestLocationPermissions ( ) ; } const hasBluetoothPermission = await this . bleManager . hasBluetoothPermission ( ) ; if ( ! hasBluetoothPermission ) { await this . uiAlert . showMessage ( 'SCALE.REQUEST_PERMISSION.BLUETOOTH' , undefined , undefined , true ) ; await this . bleManager . requestBluetoothPermissions ( ) ; } const bleEnabled = await this . bleManager . isBleEnabled ( ) ; if ( bleEnabled === false ) { await this . uiAlert . showMessage ( 'SCALE.BLUETOOTH_NOT_ENABLED' , undefined , undefined , true ) ; return ; } await this . uiAlert . showLoadingSpinner ( ) ; const device = await this . bleManager . tryToFindScale ( ) ; if ( device ) { await this . uiAlert . hideLoadingSpinner ( ) ; this . bleManager . autoConnectScale ( device . type , device . id , false ) ; } else { await this . uiAlert . hideLoadingSpinner ( ) ; this . uiAlert . showMessage ( 'SCALE.CONNECTION_NOT_ESTABLISHED' , undefined , undefined , true ) ; } } public sendLEDCommand ( ) { const scale = this . bleManager . getScale ( ) ; scale . connect ( ) . then ( ( ) => { } ) ; if ( this . interval === undefined ) { let weightOn = false ; let timerOn = false ; this . interval = setInterval ( ( ) => { weightOn = ! weightOn ; timerOn = ! timerOn ; scale . setLed ( weightOn , timerOn ) ; } , 1000 ) ; } } public stopLEDCommand ( ) { if ( this . interval !== undefined ) { clearInterval ( this . interval ) ; this . interval = undefined ; } } }