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
Value | Type | Description | API_LEVEL |
---|---|---|---|
N | string | North | 3.0 |
NE | string | Northeast | 3.0 |
E | string | East | 3.0 |
SE | string | Southeast | 3.0 |
S | string | South | 3.0 |
SW | string | Southwest | 3.0 |
W | string | West | 3.0 |
NW | string | Northwest | 3.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()