# Sensor

# Interface Declaration

{ "name": "system.sensor" }

# Import Module

import sensor from '@system.sensor' 
// or 
const sensor = require('@system.sensor')

# Interface Definition

# Methods

# sensor.subscribePressure(OBJECT)

Listens for pressure sensor data. If called multiple times, only the last call takes effect.

# Parameters:

Parameter Type Required Description
callback Function Yes Callback triggered each time the pressure information changes.

# Callback Return Value:

Parameter Type Description
pressure Number Pressure, in hPa (hectopascals), floating-point number.

# Example:

sensor.subscribePressure({
  callback: function(ret) {
    console.log(`handling callback, pressure = ${ret.pressure}`)
  }
})

# sensor.unsubscribePressure()

Cancels listening for pressure sensor data.

# Parameters:

None

# Example:

sensor.unsubscribePressure()

# sensor.subscribeAccelerometer(OBJECT)

Listens for accelerometer sensor data.

# Parameters:

Parameter Type Required Description
interval String No Frequency of callback execution for accelerometer data listening. Default is 'normal'.
callback Function Yes Callback triggered when gravity sensor data changes.
fail Function No Callback for subscription errors.

# Valid Values for interval:

Value Description
game Suitable for game update callback frequency, around 20ms per call.
ui Suitable for UI update callback frequency, around 60ms per call.
normal Normal callback frequency, around 200ms per call.

# Callback Return Value:

Parameter Type Description
x Number X-axis coordinate.
y Number Y-axis coordinate.
z Number Z-axis coordinate.

# Example:

sensor.subscribeAccelerometer({
  callback: function(ret) {
    console.log(`handling callback, x = ${ret.x}, y = ${ret.y}, z = ${ret.z}`)
  },
  fail: function(msg, code) {
    console.log(`handling callback, fail:`, msg, code)
  }
})

# sensor.unsubscribeAccelerometer()

Cancels listening for accelerometer sensor data.

# Parameters:

None

# Example:

sensor.unsubscribeAccelerometer()

# sensor.subscribeCompass(OBJECT)

Listens for compass data. If called multiple times, only the last call takes effect.

# Parameters:

Parameter Type Required Description
callback Function Yes Callback triggered when compass data changes.
fail Function No Callback for subscription failure.

# Callback Return Value:

Parameter Type Description
direction Number Represents the angle between the device's y-axis and the Earth's magnetic north pole. When facing north, the angle is 0; facing south, it is π; facing east, it is π/2; facing west, it is -π/2.
accuracy Number Accuracy, see compass accuracy description for details.

# Error Codes for fail:

Error Code Description
1000 The current device does not support the compass sensor.

# Example:

sensor.subscribeCompass({
  callback: function (res) {
    console.log(`handling subscribeCompass callback, direction = ${res.direction}, accuracy = ${res.accuracy}`)
  },
  fail: function (data, code) {
    console.log(`handling subscribeCompass fail, code = ${code}`)
  }
})

# sensor.unsubscribeCompass()

Cancels listening for compass data.

# Parameters:

None

# Example:

sensor.unsubscribeCompass()

# Compass Accuracy Description:

Value Description
3 High accuracy.
2 Medium accuracy.
1 Low accuracy.
-1 Unreliable, sensor disconnected.
0 Unreliable, reason unknown.

# Support Details

Interface Supported Device Products Unsupported Device Products
subscribePressure Xiaomi Watch S3, Xiaomi Band 9 Pro, Xiaomi Band 10, Xiaomi Watch S4 Xiaomi S1 Pro Sports Health Watch, Xiaomi Band 8 Pro, Xiaomi Band 9, Redmi Watch 4, Xiaomi Watch H1, REDMI Watch 5
unsubscribePressure Xiaomi Watch S3, Xiaomi Band 9 Pro, Xiaomi Band 10, Xiaomi Watch S4 Xiaomi S1 Pro Sports Health Watch, Xiaomi Band 8 Pro, Xiaomi Band 9, Redmi Watch 4, Xiaomi Watch H1, REDMI Watch 5
subscribeAccelerometer Xiaomi Band 9 / 9 Pro, Xiaomi Band 10 Xiaomi Watch S3, Xiaomi S1 Pro Sports Health Watch, Xiaomi Band 8 Pro, Redmi Watch 4, Xiaomi Watch H1, Xiaomi Watch S4, REDMI Watch 5
unsubscribeAccelerometer Xiaomi Band 9 / 9 Pro, Xiaomi Band 10 Xiaomi Watch S3, Xiaomi S1 Pro Sports Health Watch, Xiaomi Band 8 Pro, Redmi Watch 4, Xiaomi Watch H1, Xiaomi Watch S4, REDMI Watch 5
subscribeCompass / unsubscribeCompass Xiaomi Watch S4, REDMI Watch 5 Other Xiaomi band and watch devices