Skip to main content
Version: v3.x

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

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()