Skip to main content
Version: v3+

Compass

Start from API_LEVEL 3.0 . Please refer to API_LEVEL.

compass.

info

permission code: device:os.compass

Methods​

start​

Start listening to compass data

start(): void

stop​

Stop listening to compass data

stop(): void

getStatus​

Get the compass calibration status, true means calibrated

getStatus(): boolean

getDirection​

Get the direction of the current watch's 12-point scale, divided into eight directions, refer to direction

getDirection(): string

direction​

ValueTypeDescriptionAPI_LEVEL
NstringNorth3.0
NEstringNortheast3.0
EstringEast3.0
SEstringSoutheast3.0
SstringSouth3.0
SWstringSouthwest3.0
WstringWest3.0
NWstringNorthwest3.0

getDirectionAngle​

Get the current direction angle, the clockwise rotation angle of the watch's 12 o'clock scale direction relative to due north, takes the values 0 - 360, if the compass is not calibrated, returns the INVALID string

getDirectionAngle(): number | 'INVALID'

onChange​

Register the compass direction change event listener callback function

onChange(callback: () => void): void

offChange​

Cancel the compass direction change event listener callback function

offChange(callback: () => void): void

setFreqMode​

Start from API_LEVEL 4.0

Set the mode of trigger frequency, mode value reference frequency mode constant

setFreqMode(mode: number): void

Constants​

Frequency Mode​
ConstantDescriptionAPI_LEVEL
FREQ_MODE_LOWLow power mode with low trigger frequency3.0
FREQ_MODE_NORMALNormal power consumption mode, medium trigger frequency3.0
FREQ_MODE_HIGHHigh power consumption mode with high trigger frequency3.0

getFreqMode​

Start from API_LEVEL 4.0

Get the mode of trigger frequency, result value reference frequency mode constant

getFreqMode(): number

Constants​

Frequency Mode​
ConstantDescriptionAPI_LEVEL
FREQ_MODE_LOWLow power mode with low trigger frequency3.0
FREQ_MODE_NORMALNormal power consumption mode, medium trigger frequency3.0
FREQ_MODE_HIGHHigh power consumption mode with high trigger frequency3.0

Example​

import { Compass } from '@zos/sensor'

const compass = new Compass()

const callback = () => {
if (compass.getStatus()) {
console.log(compass.getDirection())
console.log(compass.getDirectionAngle())
}
}
compass.onChange(callback)
compass.start()

// When not needed for use
compass.offChange()
compass.stop()