更新记录
1.0.2(2026-03-25)
1.0.1(2026-03-25)
1.0.0(2026-03-25)
- 初始版本发布
- 封装 Android 平台文档声明的所有可用传感器
- 支持每个传感器单独开启/停止
- 支持设备可用性检测
- 提供停止所有传感器的快捷方法
查看更多
平台兼容性
uni-app(4.18)
| Vue2 |
Vue3 |
Chrome |
Safari |
app-vue |
app-vue插件版本 |
app-nvue |
app-nvue插件版本 |
Android |
Android插件版本 |
iOS |
鸿蒙 |
| × |
√ |
× |
× |
√ |
1.0.0 |
√ |
1.0.0 |
5.0 |
1.0.0 |
× |
× |
| 微信小程序 |
支付宝小程序 |
抖音小程序 |
百度小程序 |
快手小程序 |
京东小程序 |
鸿蒙元服务 |
QQ小程序 |
飞书小程序 |
小红书小程序 |
快应用-华为 |
快应用-联盟 |
| × |
× |
× |
× |
× |
× |
× |
× |
× |
× |
× |
× |
uni-app x(4.18)
| Chrome |
Safari |
Android |
Android插件版本 |
iOS |
鸿蒙 |
微信小程序 |
| × |
× |
5.0 |
1.0.0 |
× |
× |
× |
使用前须知
- 本插件封装了 Android 平台[由此查看参考文档]规定的全部非弃用传感器
- 部分传感器(计步器、步测器)需要 Android 10+ 声明
ACTIVITY_RECOGNITION 权限,参见权限声明
插件引入
import * as sensors from '@/uni_modules/cfit-allcensors'
传感器列表
环境传感器
| 传感器 |
方法名 |
回调参数 |
单位 |
| 气压传感器 |
onPressureChange |
pressure |
hPa / mbar |
| 光传感器 |
onLightChange |
lux |
lx |
| 环境温度传感器 |
onAmbientTemperatureChange |
celsius |
°C |
| 相对湿度传感器 |
onHumidityChange |
percent |
% |
运动传感器
| 传感器 |
方法名 |
回调参数 |
单位 |
| 加速度计 |
onAccelerometerChange |
x, y, z |
m/s² |
| 重力传感器 |
onGravityChange |
x, y, z |
m/s² |
| 陀螺仪 |
onGyroscopeChange |
x, y, z |
rad/s |
| 线性加速度 |
onLinearAccelerationChange |
x, y, z |
m/s² |
| 旋转矢量 |
onRotationVectorChange |
x, y, z, w |
- |
| 有效运动 |
onSignificantMotion |
timestamp |
- |
| 计步器 |
onStepCounterChange |
steps |
步数 |
| 步测器 |
onStepDetectorChange |
steps |
步数 |
| 未校准加速度计 |
onUncalibratedAccelerometerChange |
x, y, z, bx, by, bz |
m/s² |
| 未校准陀螺仪 |
onUncalibratedGyroscopeChange |
x, y, z, dx, dy, dz |
rad/s |
| 未校准磁力计 |
onUncalibratedMagneticFieldChange |
x, y, z, bx, by, bz |
μT |
| 游戏旋转矢量 |
onGameRotationVectorChange |
x, y, z, w |
- |
| 地磁旋转矢量 |
onGeomagneticRotationVectorChange |
x, y, z, w |
- |
位置传感器
| 传感器 |
方法名 |
回调参数 |
单位 |
| 磁力计 |
onMagneticFieldChange |
x, y, z |
μT |
| 距离传感器 |
onProximityChange |
distance |
cm |
插件使用
基础使用示例
// 监听气压传感器
sensors.onPressureChange({
handler: (pressure, timestamp, error) => {
console.log('当前气压:', pressure, 'hPa')
}
})
// 监听加速度计
sensors.onAccelerometerChange({
handler: (x, y, z, timestamp, error) => {
console.log(`加速度: X=${x.toFixed(2)}, Y=${y.toFixed(2)}, Z=${z.toFixed(2)}`)
}
})
// 停止指定传感器
sensors.stopPressure()
sensors.stopAccelerometer()
检查传感器可用性
// 检查单个传感器
if (sensors.isPressureAvailable()) {
console.log('设备支持气压传感器')
} else {
uni.showToast({ title: '设备不支持气压传感器', icon: 'none' })
}
// 批量检查
const sensorsList = [
{ name: '气压', available: sensors.isPressureAvailable() },
{ name: '加速度', available: sensors.isAccelerometerAvailable() },
{ name: '陀螺仪', available: sensors.isGyroscopeAvailable() },
{ name: '磁力计', available: sensors.isMagneticFieldAvailable() }
]
停止所有传感器
// 一键停止所有正在运行的传感器
sensors.stopAllSensorUpdates()
可用性检查方法
| 方法 |
返回值 |
说明 |
isPressureAvailable() |
boolean |
气压传感器是否可用 |
isLightAvailable() |
boolean |
光传感器是否可用 |
isAmbientTemperatureAvailable() |
boolean |
环境温度传感器是否可用 |
isHumidityAvailable() |
boolean |
相对湿度传感器是否可用 |
isAccelerometerAvailable() |
boolean |
加速度计是否可用 |
isGravityAvailable() |
boolean |
重力传感器是否可用 |
isGyroscopeAvailable() |
boolean |
陀螺仪是否可用 |
isLinearAccelerationAvailable() |
boolean |
线性加速度传感器是否可用 |
isRotationVectorAvailable() |
boolean |
旋转矢量传感器是否可用 |
isSignificantMotionAvailable() |
boolean |
有效运动传感器是否可用 |
isStepCounterAvailable() |
boolean |
计步器是否可用 |
isStepDetectorAvailable() |
boolean |
步测器是否可用 |
isUncalibratedAccelerometerAvailable() |
boolean |
未校准加速度计是否可用 |
isUncalibratedGyroscopeAvailable() |
boolean |
未校准陀螺仪是否可用 |
isUncalibratedMagneticFieldAvailable() |
boolean |
未校准磁力计是否可用 |
isGameRotationVectorAvailable() |
boolean |
游戏旋转矢量是否可用 |
isGeomagneticRotationVectorAvailable() |
boolean |
地磁旋转矢量是否可用 |
isMagneticFieldAvailable() |
boolean |
磁力计是否可用 |
isProximityAvailable() |
boolean |
距离传感器是否可用 |
停止方法
每个传感器都对应一个停止方法,命名规则为 stop + 传感器名称(首字母大写):
| 停止方法 |
对应监听方法 |
stopPressure() |
onPressureChange |
stopLight() |
onLightChange |
stopAmbientTemperature() |
onAmbientTemperatureChange |
stopHumidity() |
onHumidityChange |
stopAccelerometer() |
onAccelerometerChange |
stopGravity() |
onGravityChange |
stopGyroscope() |
onGyroscopeChange |
stopLinearAcceleration() |
onLinearAccelerationChange |
stopRotationVector() |
onRotationVectorChange |
stopSignificantMotion() |
onSignificantMotion |
stopStepCounter() |
onStepCounterChange |
stopStepDetector() |
onStepDetectorChange |
stopUncalibratedAccelerometer() |
onUncalibratedAccelerometerChange |
stopUncalibratedGyroscope() |
onUncalibratedGyroscopeChange |
stopUncalibratedMagneticField() |
onUncalibratedMagneticFieldChange |
stopGameRotationVector() |
onGameRotationVectorChange |
stopGeomagneticRotationVector() |
onGeomagneticRotationVectorChange |
stopMagneticField() |
onMagneticFieldChange |
stopProximity() |
onProximityChange |
注意事项
-
权限要求
- 计步器和步测器在 Android 10(API 级别 29)及以上需要声明
ACTIVITY_RECOGNITION 权限
-
性能建议
- 传感器监听会持续消耗电量,建议在页面卸载时调用
stopAllSensorUpdates() 停止所有监听
- 无需使用时应及时停止对应传感器,避免不必要的电量消耗
-
有效运动传感器
- 该传感器为单次触发型,检测到有效运动后自动停止,需要重新调用
onSignificantMotion 才能再次监听
-
未校准传感器
- 提供更原始的测量数据,包含偏差/漂移估算,适合高级传感器融合场景
- 测量值 = 未校准值 - 偏差估算
-
传感器可用性
- 不同设备的传感器配置差异较大,使用前建议先检查可用性(参见参考文档)
- 部分传感器(如未校准传感器)仅在 Android 较新版本上可用
示例代码
<template>
<view class="container">
<scroll-view scroll-y class="scroll-area">
<!-- 环境传感器 -->
<view class="sensor-card">
<view class="card-title">🌡️ 气压传感器</view>
<view class="sensor-value">{{ pressureValue }} hPa</view>
<view class="button-group">
<button size="mini" :type="pressureAvailable ? 'primary' : 'default'" @click="startPressure"
:disabled="!pressureAvailable">
{{ pressureAvailable ? '开始监听' : '不可用' }}
</button>
<button size="mini" type="default" @click="stopPressure" :disabled="!pressureActive">停止</button>
</view>
</view>
<view class="sensor-card">
<view class="card-title">💡 光传感器</view>
<view class="sensor-value">{{ lightValue }} lx</view>
<view class="button-group">
<button size="mini" :type="lightAvailable ? 'primary' : 'default'" @click="startLight"
:disabled="!lightAvailable">
{{ lightAvailable ? '开始监听' : '不可用' }}
</button>
<button size="mini" type="default" @click="stopLight" :disabled="!lightActive">停止</button>
</view>
</view>
<view class="sensor-card">
<view class="card-title">🌡️ 环境温度</view>
<view class="sensor-value">{{ ambientTemp }} °C</view>
<view class="button-group">
<button size="mini" :type="ambientTempAvailable ? 'primary' : 'default'" @click="startAmbientTemp"
:disabled="!ambientTempAvailable">
{{ ambientTempAvailable ? '开始监听' : '不可用' }}
</button>
<button size="mini" type="default" @click="stopAmbientTemp"
:disabled="!ambientTempActive">停止</button>
</view>
</view>
<view class="sensor-card">
<view class="card-title">💧 相对湿度</view>
<view class="sensor-value">{{ humidity }} %</view>
<view class="button-group">
<button size="mini" :type="humidityAvailable ? 'primary' : 'default'" @click="startHumidity"
:disabled="!humidityAvailable">
{{ humidityAvailable ? '开始监听' : '不可用' }}
</button>
<button size="mini" type="default" @click="stopHumidity" :disabled="!humidityActive">停止</button>
</view>
</view>
<!-- 运动传感器 -->
<view class="sensor-card">
<view class="card-title">📱 加速度计</view>
<view class="sensor-value">
X: {{ accelX.toFixed(2) }}<br />
Y: {{ accelY.toFixed(2) }}<br />
Z: {{ accelZ.toFixed(2) }}
</view>
<view class="button-group">
<button size="mini" :type="accelerometerAvailable ? 'primary' : 'default'"
@click="startAccelerometer" :disabled="!accelerometerAvailable">
{{ accelerometerAvailable ? '开始监听' : '不可用' }}
</button>
<button size="mini" type="default" @click="stopAccelerometer"
:disabled="!accelerometerActive">停止</button>
</view>
</view>
<view class="sensor-card">
<view class="card-title">🌍 重力传感器</view>
<view class="sensor-value">
X: {{ gravityX.toFixed(2) }}<br />
Y: {{ gravityY.toFixed(2) }}<br />
Z: {{ gravityZ.toFixed(2) }}
</view>
<view class="button-group">
<button size="mini" :type="gravityAvailable ? 'primary' : 'default'" @click="startGravity"
:disabled="!gravityAvailable">
{{ gravityAvailable ? '开始监听' : '不可用' }}
</button>
<button size="mini" type="default" @click="stopGravity" :disabled="!gravityActive">停止</button>
</view>
</view>
<view class="sensor-card">
<view class="card-title">🔄 陀螺仪</view>
<view class="sensor-value">
X: {{ gyroX.toFixed(2) }}<br />
Y: {{ gyroY.toFixed(2) }}<br />
Z: {{ gyroZ.toFixed(2) }}
</view>
<view class="button-group">
<button size="mini" :type="gyroscopeAvailable ? 'primary' : 'default'" @click="startGyroscope"
:disabled="!gyroscopeAvailable">
{{ gyroscopeAvailable ? '开始监听' : '不可用' }}
</button>
<button size="mini" type="default" @click="stopGyroscope" :disabled="!gyroscopeActive">停止</button>
</view>
</view>
<view class="sensor-card">
<view class="card-title">📊 线性加速度</view>
<view class="sensor-value">
X: {{ linearAccX.toFixed(2) }}<br />
Y: {{ linearAccY.toFixed(2) }}<br />
Z: {{ linearAccZ.toFixed(2) }}
</view>
<view class="button-group">
<button size="mini" :type="linearAccAvailable ? 'primary' : 'default'"
@click="startLinearAcceleration" :disabled="!linearAccAvailable">
{{ linearAccAvailable ? '开始监听' : '不可用' }}
</button>
<button size="mini" type="default" @click="stopLinearAcceleration"
:disabled="!linearAccActive">停止</button>
</view>
</view>
<view class="sensor-card">
<view class="card-title">🧭 旋转矢量</view>
<view class="sensor-value">
X: {{ rotVecX.toFixed(3) }}, Y: {{ rotVecY.toFixed(3) }}<br />
Z: {{ rotVecZ.toFixed(3) }}, W: {{ rotVecW.toFixed(3) }}
</view>
<view class="button-group">
<button size="mini" :type="rotationVectorAvailable ? 'primary' : 'default'"
@click="startRotationVector" :disabled="!rotationVectorAvailable">
{{ rotationVectorAvailable ? '开始监听' : '不可用' }}
</button>
<button size="mini" type="default" @click="stopRotationVector"
:disabled="!rotationVectorActive">停止</button>
</view>
</view>
<view class="sensor-card">
<view class="card-title">🚶 有效运动</view>
<view class="sensor-value">{{ significantMotionTime || '未触发' }}</view>
<view class="button-group">
<button size="mini" :type="significantMotionAvailable ? 'primary' : 'default'"
@click="startSignificantMotion" :disabled="!significantMotionAvailable">
{{ significantMotionAvailable ? '开始监听' : '不可用' }}
</button>
<button size="mini" type="default" @click="stopSignificantMotion"
:disabled="!significantMotionActive">停止</button>
</view>
</view>
<view class="sensor-card">
<view class="card-title">👣 计步器</view>
<view class="sensor-value">{{ stepCount }} 步</view>
<view class="button-group">
<button size="mini" :type="stepCounterAvailable ? 'primary' : 'default'" @click="startStepCounter"
:disabled="!stepCounterAvailable">
{{ stepCounterAvailable ? '开始监听' : '不可用' }}
</button>
<button size="mini" type="default" @click="stopStepCounter"
:disabled="!stepCounterActive">停止</button>
</view>
</view>
<view class="sensor-card">
<view class="card-title">🚶♂️ 步测器</view>
<view class="sensor-value">最近一步: {{ stepDetectorCount || '未检测' }}</view>
<view class="button-group">
<button size="mini" :type="stepDetectorAvailable ? 'primary' : 'default'" @click="startStepDetector"
:disabled="!stepDetectorAvailable">
{{ stepDetectorAvailable ? '开始监听' : '不可用' }}
</button>
<button size="mini" type="default" @click="stopStepDetector"
:disabled="!stepDetectorActive">停止</button>
</view>
</view>
<view class="sensor-card">
<view class="card-title">📈 未校准加速度计</view>
<view class="sensor-value">
测量: X={{ uncalibAccX.toFixed(2) }}, Y={{ uncalibAccY.toFixed(2) }},
Z={{ uncalibAccZ.toFixed(2) }}<br />
偏差: X={{ uncalibAccBiasX.toFixed(2) }}, Y={{ uncalibAccBiasY.toFixed(2) }},
Z={{ uncalibAccBiasZ.toFixed(2) }}
</view>
<view class="button-group">
<button size="mini" :type="uncalibAccAvailable ? 'primary' : 'default'"
@click="startUncalibratedAccelerometer" :disabled="!uncalibAccAvailable">
{{ uncalibAccAvailable ? '开始监听' : '不可用' }}
</button>
<button size="mini" type="default" @click="stopUncalibratedAccelerometer"
:disabled="!uncalibAccActive">停止</button>
</view>
</view>
<view class="sensor-card">
<view class="card-title">📉 未校准陀螺仪</view>
<view class="sensor-value">
测量: X={{ uncalibGyroX.toFixed(2) }}, Y={{ uncalibGyroY.toFixed(2) }},
Z={{ uncalibGyroZ.toFixed(2) }}<br />
漂移: X={{ uncalibGyroDriftX.toFixed(2) }}, Y={{ uncalibGyroDriftY.toFixed(2) }},
Z={{ uncalibGyroDriftZ.toFixed(2) }}
</view>
<view class="button-group">
<button size="mini" :type="uncalibGyroAvailable ? 'primary' : 'default'"
@click="startUncalibratedGyroscope" :disabled="!uncalibGyroAvailable">
{{ uncalibGyroAvailable ? '开始监听' : '不可用' }}
</button>
<button size="mini" type="default" @click="stopUncalibratedGyroscope"
:disabled="!uncalibGyroActive">停止</button>
</view>
</view>
<view class="sensor-card">
<view class="card-title">🧲 未校准磁力计</view>
<view class="sensor-value">
测量: X={{ uncalibMagX.toFixed(2) }}, Y={{ uncalibMagY.toFixed(2) }}, Z={{ uncalibMagZ.toFixed(2) }}
μT<br />
偏差: X={{ uncalibMagBiasX.toFixed(2) }}, Y={{ uncalibMagBiasY.toFixed(2) }},
Z={{ uncalibMagBiasZ.toFixed(2) }}
</view>
<view class="button-group">
<button size="mini" :type="uncalibMagAvailable ? 'primary' : 'default'"
@click="startUncalibratedMagneticField" :disabled="!uncalibMagAvailable">
{{ uncalibMagAvailable ? '开始监听' : '不可用' }}
</button>
<button size="mini" type="default" @click="stopUncalibratedMagneticField"
:disabled="!uncalibMagActive">停止</button>
</view>
</view>
<view class="sensor-card">
<view class="card-title">🎮 游戏旋转矢量</view>
<view class="sensor-value">
X: {{ gameRotVecX.toFixed(3) }}, Y: {{ gameRotVecY.toFixed(3) }}<br />
Z: {{ gameRotVecZ.toFixed(3) }}, W: {{ gameRotVecW.toFixed(3) }}
</view>
<view class="button-group">
<button size="mini" :type="gameRotVecAvailable ? 'primary' : 'default'"
@click="startGameRotationVector" :disabled="!gameRotVecAvailable">
{{ gameRotVecAvailable ? '开始监听' : '不可用' }}
</button>
<button size="mini" type="default" @click="stopGameRotationVector"
:disabled="!gameRotVecActive">停止</button>
</view>
</view>
<view class="sensor-card">
<view class="card-title">🧭 地磁旋转矢量</view>
<view class="sensor-value">
X: {{ geomagRotVecX.toFixed(3) }}, Y: {{ geomagRotVecY.toFixed(3) }}<br />
Z: {{ geomagRotVecZ.toFixed(3) }}, W: {{ geomagRotVecW.toFixed(3) }}
</view>
<view class="button-group">
<button size="mini" :type="geomagRotVecAvailable ? 'primary' : 'default'"
@click="startGeomagneticRotationVector" :disabled="!geomagRotVecAvailable">
{{ geomagRotVecAvailable ? '开始监听' : '不可用' }}
</button>
<button size="mini" type="default" @click="stopGeomagneticRotationVector"
:disabled="!geomagRotVecActive">停止</button>
</view>
</view>
<!-- 位置传感器 -->
<view class="sensor-card">
<view class="card-title">🧲 磁力计</view>
<view class="sensor-value">
X: {{ magneticX.toFixed(2) }}<br />
Y: {{ magneticY.toFixed(2) }}<br />
Z: {{ magneticZ.toFixed(2) }}
</view>
<view class="button-group">
<button size="mini" :type="magneticAvailable ? 'primary' : 'default'" @click="startMagneticField"
:disabled="!magneticAvailable">
{{ magneticAvailable ? '开始监听' : '不可用' }}
</button>
<button size="mini" type="default" @click="stopMagneticField"
:disabled="!magneticActive">停止</button>
</view>
</view>
<view class="sensor-card">
<view class="card-title">📏 距离传感器</view>
<view class="sensor-value">{{ proximityValue }} cm</view>
<view class="button-group">
<button size="mini" :type="proximityAvailable ? 'primary' : 'default'" @click="startProximity"
:disabled="!proximityAvailable">
{{ proximityAvailable ? '开始监听' : '不可用' }}
</button>
<button size="mini" type="default" @click="stopProximity" :disabled="!proximityActive">停止</button>
</view>
</view>
<!-- 停止所有按钮 -->
<view class="stop-btn">
<button type="warn" @click="stopAll">停止所有传感器监听</button>
</view>
</scroll-view>
</view>
</template>
<script>
import * as cfitsensor from '@/uni_modules/cfit-allcensors'
export default {
data() {
return {
// 环境
pressureValue: '--',
lightValue: '--',
ambientTemp: '--',
humidity: '--',
// 运动
accelX: 0,
accelY: 0,
accelZ: 0,
gravityX: 0,
gravityY: 0,
gravityZ: 0,
gyroX: 0,
gyroY: 0,
gyroZ: 0,
linearAccX: 0,
linearAccY: 0,
linearAccZ: 0,
rotVecX: 0,
rotVecY: 0,
rotVecZ: 0,
rotVecW: 0,
significantMotionTime: '',
stepCount: 0,
stepDetectorCount: 0,
uncalibAccX: 0,
uncalibAccY: 0,
uncalibAccZ: 0,
uncalibAccBiasX: 0,
uncalibAccBiasY: 0,
uncalibAccBiasZ: 0,
uncalibGyroX: 0,
uncalibGyroY: 0,
uncalibGyroZ: 0,
uncalibGyroDriftX: 0,
uncalibGyroDriftY: 0,
uncalibGyroDriftZ: 0,
uncalibMagX: 0,
uncalibMagY: 0,
uncalibMagZ: 0,
uncalibMagBiasX: 0,
uncalibMagBiasY: 0,
uncalibMagBiasZ: 0,
gameRotVecX: 0,
gameRotVecY: 0,
gameRotVecZ: 0,
gameRotVecW: 0,
geomagRotVecX: 0,
geomagRotVecY: 0,
geomagRotVecZ: 0,
geomagRotVecW: 0,
// 位置
magneticX: 0,
magneticY: 0,
magneticZ: 0,
proximityValue: '--',
// 监听状态
pressureActive: false,
lightActive: false,
ambientTempActive: false,
humidityActive: false,
accelerometerActive: false,
gravityActive: false,
gyroscopeActive: false,
linearAccActive: false,
rotationVectorActive: false,
significantMotionActive: false,
stepCounterActive: false,
stepDetectorActive: false,
uncalibAccActive: false,
uncalibGyroActive: false,
uncalibMagActive: false,
gameRotVecActive: false,
geomagRotVecActive: false,
magneticActive: false,
proximityActive: false,
// 可用性标志
pressureAvailable: false,
lightAvailable: false,
ambientTempAvailable: false,
humidityAvailable: false,
accelerometerAvailable: false,
gravityAvailable: false,
gyroscopeAvailable: false,
linearAccAvailable: false,
rotationVectorAvailable: false,
significantMotionAvailable: false,
stepCounterAvailable: false,
stepDetectorAvailable: false,
uncalibAccAvailable: false,
uncalibGyroAvailable: false,
uncalibMagAvailable: false,
gameRotVecAvailable: false,
geomagRotVecAvailable: false,
magneticAvailable: false,
proximityAvailable: false
}
},
onLoad() {
this.checkSensorAvailability()
},
onUnload() {
this.stopAll()
},
methods: {
checkSensorAvailability() {
this.pressureAvailable = cfitsensor.isPressureAvailable()
this.lightAvailable = cfitsensor.isLightAvailable()
this.ambientTempAvailable = cfitsensor.isAmbientTemperatureAvailable()
this.humidityAvailable = cfitsensor.isHumidityAvailable()
this.accelerometerAvailable = cfitsensor.isAccelerometerAvailable()
this.gravityAvailable = cfitsensor.isGravityAvailable()
this.gyroscopeAvailable = cfitsensor.isGyroscopeAvailable()
this.linearAccAvailable = cfitsensor.isLinearAccelerationAvailable()
this.rotationVectorAvailable = cfitsensor.isRotationVectorAvailable()
this.significantMotionAvailable = cfitsensor.isSignificantMotionAvailable()
this.stepCounterAvailable = cfitsensor.isStepCounterAvailable()
this.stepDetectorAvailable = cfitsensor.isStepDetectorAvailable()
this.uncalibAccAvailable = cfitsensor.isUncalibratedAccelerometerAvailable()
this.uncalibGyroAvailable = cfitsensor.isUncalibratedGyroscopeAvailable()
this.uncalibMagAvailable = cfitsensor.isUncalibratedMagneticFieldAvailable()
this.gameRotVecAvailable = cfitsensor.isGameRotationVectorAvailable()
this.geomagRotVecAvailable = cfitsensor.isGeomagneticRotationVectorAvailable()
this.magneticAvailable = cfitsensor.isMagneticFieldAvailable()
this.proximityAvailable = cfitsensor.isProximityAvailable()
console.log('气压可用:', this.pressureAvailable)
console.log('光可用:', this.lightAvailable)
console.log('环境温度可用:', this.ambientTempAvailable)
console.log('湿度可用:', this.humidityAvailable)
console.log('加速度可用:', this.accelerometerAvailable)
console.log('重力可用:', this.gravityAvailable)
console.log('陀螺仪可用:', this.gyroscopeAvailable)
console.log('线性加速度可用:', this.linearAccAvailable)
console.log('旋转矢量可用:', this.rotationVectorAvailable)
console.log('有效运动可用:', this.significantMotionAvailable)
console.log('计步器可用:', this.stepCounterAvailable)
console.log('步测器可用:', this.stepDetectorAvailable)
console.log('未校准加速度可用:', this.uncalibAccAvailable)
console.log('未校准陀螺仪可用:', this.uncalibGyroAvailable)
console.log('未校准磁力计可用:', this.uncalibMagAvailable)
console.log('游戏旋转矢量可用:', this.gameRotVecAvailable)
console.log('地磁旋转矢量可用:', this.geomagRotVecAvailable)
console.log('磁力计可用:', this.magneticAvailable)
console.log('距离可用:', this.proximityAvailable)
},
// 每个传感器的启动方法
startPressure() {
if (this.pressureActive) return;
if (!cfitsensor.isPressureAvailable()) {
uni.showToast({
title: '设备不支持气压传感器',
icon: 'none'
});
return
}
cfitsensor.onPressureChange({
handler: (v, t, e) => {
this.pressureValue = v.toFixed(2);
this.pressureActive = true;
}
});
uni.showToast({
title: '开始监听气压',
icon: 'success'
});
},
stopPressure() {
cfitsensor.stopPressure();
this.pressureActive = false;
uni.showToast({
title: '已停止气压监听',
icon: 'none'
});
},
// 光传感器
startLight() {
if (this.lightActive) return;
if (!cfitsensor.isLightAvailable()) {
uni.showToast({
title: '设备不支持光传感器',
icon: 'none'
});
return
}
cfitsensor.onLightChange({
handler: (v, t, e) => {
this.lightValue = v.toFixed(2);
this.lightActive = true;
}
});
uni.showToast({
title: '开始监听光照',
icon: 'success'
});
},
stopLight() {
cfitsensor.stopLight();
this.lightActive = false;
uni.showToast({
title: '已停止光照监听',
icon: 'none'
});
},
// 环境温度
startAmbientTemp() {
if (this.ambientTempActive) return;
if (!cfitsensor.isAmbientTemperatureAvailable()) {
uni.showToast({
title: '设备不支持环境温度',
icon: 'none'
});
return
}
cfitsensor.onAmbientTemperatureChange({
handler: (v, t, e) => {
this.ambientTemp = v.toFixed(2);
this.ambientTempActive = true;
}
});
uni.showToast({
title: '开始监听环境温度',
icon: 'success'
});
},
stopAmbientTemp() {
cfitsensor.stopAmbientTemperature();
this.ambientTempActive = false;
uni.showToast({
title: '已停止环境温度监听',
icon: 'none'
});
},
// 湿度
startHumidity() {
if (this.humidityActive) return;
if (!cfitsensor.isHumidityAvailable()) {
uni.showToast({
title: '设备不支持湿度',
icon: 'none'
});
return
}
cfitsensor.onHumidityChange({
handler: (v, t, e) => {
this.humidity = v.toFixed(2);
this.humidityActive = true;
}
});
uni.showToast({
title: '开始监听湿度',
icon: 'success'
});
},
stopHumidity() {
cfitsensor.stopHumidity();
this.humidityActive = false;
uni.showToast({
title: '已停止湿度监听',
icon: 'none'
});
},
// 加速度计
startAccelerometer() {
if (this.accelerometerActive) return;
if (!cfitsensor.isAccelerometerAvailable()) {
uni.showToast({
title: '设备不支持加速度',
icon: 'none'
});
return
}
cfitsensor.onAccelerometerChange({
handler: (x, y, z, t, e) => {
this.accelX = x;
this.accelY = y;
this.accelZ = z;
this.accelerometerActive = true;
}
});
uni.showToast({
title: '开始监听加速度',
icon: 'success'
});
},
stopAccelerometer() {
cfitsensor.stopAccelerometer();
this.accelerometerActive = false;
uni.showToast({
title: '已停止加速度监听',
icon: 'none'
});
},
// 重力
startGravity() {
if (this.gravityActive) return;
if (!cfitsensor.isGravityAvailable()) {
uni.showToast({
title: '设备不支持重力',
icon: 'none'
});
return
}
cfitsensor.onGravityChange({
handler: (x, y, z, t, e) => {
this.gravityX = x;
this.gravityY = y;
this.gravityZ = z;
this.gravityActive = true;
}
});
uni.showToast({
title: '开始监听重力',
icon: 'success'
});
},
stopGravity() {
cfitsensor.stopGravity();
this.gravityActive = false;
uni.showToast({
title: '已停止重力监听',
icon: 'none'
});
},
// 陀螺仪
startGyroscope() {
if (this.gyroscopeActive) return;
if (!cfitsensor.isGyroscopeAvailable()) {
uni.showToast({
title: '设备不支持陀螺仪',
icon: 'none'
});
return
}
cfitsensor.onGyroscopeChange({
handler: (x, y, z, t, e) => {
this.gyroX = x;
this.gyroY = y;
this.gyroZ = z;
this.gyroscopeActive = true;
}
});
uni.showToast({
title: '开始监听陀螺仪',
icon: 'success'
});
},
stopGyroscope() {
cfitsensor.stopGyroscope();
this.gyroscopeActive = false;
uni.showToast({
title: '已停止陀螺仪监听',
icon: 'none'
});
},
// 线性加速度
startLinearAcceleration() {
if (this.linearAccActive) return;
if (!cfitsensor.isLinearAccelerationAvailable()) {
uni.showToast({
title: '设备不支持线性加速度',
icon: 'none'
});
return
}
cfitsensor.onLinearAccelerationChange({
handler: (x, y, z, t, e) => {
this.linearAccX = x;
this.linearAccY = y;
this.linearAccZ = z;
this.linearAccActive = true;
}
});
uni.showToast({
title: '开始监听线性加速度',
icon: 'success'
});
},
stopLinearAcceleration() {
cfitsensor.stopLinearAcceleration();
this.linearAccActive = false;
uni.showToast({
title: '已停止线性加速度监听',
icon: 'none'
});
},
// 旋转矢量
startRotationVector() {
if (this.rotationVectorActive) return;
if (!cfitsensor.isRotationVectorAvailable()) {
uni.showToast({
title: '设备不支持旋转矢量',
icon: 'none'
});
return
}
cfitsensor.onRotationVectorChange({
handler: (x, y, z, w, t, e) => {
this.rotVecX = x;
this.rotVecY = y;
this.rotVecZ = z;
this.rotVecW = w;
this.rotationVectorActive = true;
}
});
uni.showToast({
title: '开始监听旋转矢量',
icon: 'success'
});
},
stopRotationVector() {
cfitsensor.stopRotationVector();
this.rotationVectorActive = false;
uni.showToast({
title: '已停止旋转矢量监听',
icon: 'none'
});
},
// 有效运动
startSignificantMotion() {
if (this.significantMotionActive) return;
if (!cfitsensor.isSignificantMotionAvailable()) {
uni.showToast({
title: '设备不支持有效运动',
icon: 'none'
});
return
}
cfitsensor.onSignificantMotion({
handler: (t, e) => {
this.significantMotionTime = t.toLocaleTimeString();
this.significantMotionActive = true;
}
});
uni.showToast({
title: '开始监听有效运动',
icon: 'success'
});
},
stopSignificantMotion() {
cfitsensor.stopSignificantMotion();
this.significantMotionActive = false;
uni.showToast({
title: '已停止有效运动监听',
icon: 'none'
});
},
// 计步器
startStepCounter() {
if (this.stepCounterActive) return;
if (!cfitsensor.isStepCounterAvailable()) {
uni.showToast({
title: '设备不支持计步器',
icon: 'none'
});
return
}
cfitsensor.onStepCounterChange({
handler: (s, t, e) => {
this.stepCount = s;
this.stepCounterActive = true;
}
});
uni.showToast({
title: '开始监听计步器',
icon: 'success'
});
},
stopStepCounter() {
cfitsensor.stopStepCounter();
this.stepCounterActive = false;
uni.showToast({
title: '已停止计步器监听',
icon: 'none'
});
},
// 步测器
startStepDetector() {
if (this.stepDetectorActive) return;
if (!cfitsensor.isStepDetectorAvailable()) {
uni.showToast({
title: '设备不支持步测器',
icon: 'none'
});
return
}
cfitsensor.onStepDetectorChange({
handler: (s, t, e) => {
this.stepDetectorCount = s;
this.stepDetectorActive = true;
}
});
uni.showToast({
title: '开始监听步测器',
icon: 'success'
});
},
stopStepDetector() {
cfitsensor.stopStepDetector();
this.stepDetectorActive = false;
uni.showToast({
title: '已停止步测器监听',
icon: 'none'
});
},
// 未校准加速度计
startUncalibratedAccelerometer() {
if (this.uncalibAccActive) return;
if (!cfitsensor.isUncalibratedAccelerometerAvailable()) {
uni.showToast({
title: '设备不支持未校准加速度',
icon: 'none'
});
return
}
cfitsensor.onUncalibratedAccelerometerChange({
handler: (x, y, z, bx, by, bz, t, e) => {
this.uncalibAccX = x;
this.uncalibAccY = y;
this.uncalibAccZ = z;
this.uncalibAccBiasX = bx;
this.uncalibAccBiasY = by;
this.uncalibAccBiasZ = bz;
this.uncalibAccActive = true;
}
});
uni.showToast({
title: '开始监听未校准加速度',
icon: 'success'
});
},
stopUncalibratedAccelerometer() {
cfitsensor.stopUncalibratedAccelerometer();
this.uncalibAccActive = false;
uni.showToast({
title: '已停止未校准加速度监听',
icon: 'none'
});
},
// 未校准陀螺仪
startUncalibratedGyroscope() {
if (this.uncalibGyroActive) return;
if (!cfitsensor.isUncalibratedGyroscopeAvailable()) {
uni.showToast({
title: '设备不支持未校准陀螺仪',
icon: 'none'
});
return
}
cfitsensor.onUncalibratedGyroscopeChange({
handler: (x, y, z, dx, dy, dz, t, e) => {
this.uncalibGyroX = x;
this.uncalibGyroY = y;
this.uncalibGyroZ = z;
this.uncalibGyroDriftX = dx;
this.uncalibGyroDriftY = dy;
this.uncalibGyroDriftZ = dz;
this.uncalibGyroActive = true;
}
});
uni.showToast({
title: '开始监听未校准陀螺仪',
icon: 'success'
});
},
stopUncalibratedGyroscope() {
cfitsensor.stopUncalibratedGyroscope();
this.uncalibGyroActive = false;
uni.showToast({
title: '已停止未校准陀螺仪监听',
icon: 'none'
});
},
// 未校准磁力计
startUncalibratedMagneticField() {
if (this.uncalibMagActive) return;
if (!cfitsensor.isUncalibratedMagneticFieldAvailable()) {
uni.showToast({
title: '设备不支持未校准磁力计',
icon: 'none'
});
return
}
cfitsensor.onUncalibratedMagneticFieldChange({
handler: (x, y, z, bx, by, bz, t, e) => {
this.uncalibMagX = x;
this.uncalibMagY = y;
this.uncalibMagZ = z;
this.uncalibMagBiasX = bx;
this.uncalibMagBiasY = by;
this.uncalibMagBiasZ = bz;
this.uncalibMagActive = true;
}
});
uni.showToast({
title: '开始监听未校准磁场',
icon: 'success'
});
},
stopUncalibratedMagneticField() {
cfitsensor.stopUncalibratedMagneticField();
this.uncalibMagActive = false;
uni.showToast({
title: '已停止未校准磁场监听',
icon: 'none'
});
},
// 游戏旋转矢量
startGameRotationVector() {
if (this.gameRotVecActive) return;
if (!cfitsensor.isGameRotationVectorAvailable()) {
uni.showToast({
title: '设备不支持游戏旋转矢量',
icon: 'none'
});
return
}
cfitsensor.onGameRotationVectorChange({
handler: (x, y, z, w, t, e) => {
this.gameRotVecX = x;
this.gameRotVecY = y;
this.gameRotVecZ = z;
this.gameRotVecW = w;
this.gameRotVecActive = true;
}
});
uni.showToast({
title: '开始监听游戏旋转矢量',
icon: 'success'
});
},
stopGameRotationVector() {
cfitsensor.stopGameRotationVector();
this.gameRotVecActive = false;
uni.showToast({
title: '已停止游戏旋转矢量监听',
icon: 'none'
});
},
// 地磁旋转矢量
startGeomagneticRotationVector() {
if (this.geomagRotVecActive) return;
if (!cfitsensor.isGeomagneticRotationVectorAvailable()) {
uni.showToast({
title: '设备不支持地磁旋转矢量',
icon: 'none'
});
return
}
cfitsensor.onGeomagneticRotationVectorChange({
handler: (x, y, z, w, t, e) => {
this.geomagRotVecX = x;
this.geomagRotVecY = y;
this.geomagRotVecZ = z;
this.geomagRotVecW = w;
this.geomagRotVecActive = true;
}
});
uni.showToast({
title: '开始监听地磁旋转矢量',
icon: 'success'
});
},
stopGeomagneticRotationVector() {
cfitsensor.stopGeomagneticRotationVector();
this.geomagRotVecActive = false;
uni.showToast({
title: '已停止地磁旋转矢量监听',
icon: 'none'
});
},
// 磁力计
startMagneticField() {
if (this.magneticActive) return;
if (!cfitsensor.isMagneticFieldAvailable()) {
uni.showToast({
title: '设备不支持磁力计',
icon: 'none'
});
return
}
cfitsensor.onMagneticFieldChange({
handler: (x, y, z, t, e) => {
this.magneticX = x;
this.magneticY = y;
this.magneticZ = z;
this.magneticActive = true;
}
});
uni.showToast({
title: '开始监听磁场',
icon: 'success'
});
},
stopMagneticField() {
cfitsensor.stopMagneticField();
this.magneticActive = false;
uni.showToast({
title: '已停止磁场监听',
icon: 'none'
});
},
// 距离传感器
startProximity() {
if (this.proximityActive) return;
if (!cfitsensor.isProximityAvailable()) {
uni.showToast({
title: '设备不支持距离传感器',
icon: 'none'
});
return
}
cfitsensor.onProximityChange({
handler: (d, t, e) => {
this.proximityValue = d.toFixed(2);
this.proximityActive = true;
}
});
uni.showToast({
title: '开始监听距离',
icon: 'success'
});
},
stopProximity() {
cfitsensor.stopProximity();
this.proximityActive = false;
uni.showToast({
title: '已停止距离监听',
icon: 'none'
});
},
// 停止所有传感器
stopAll() {
cfitsensor.stopAllSensorUpdates();
// 重置所有状态
this.pressureActive = false;
this.lightActive = false;
this.ambientTempActive = false;
this.humidityActive = false;
this.accelerometerActive = false;
this.gravityActive = false;
this.gyroscopeActive = false;
this.linearAccActive = false;
this.rotationVectorActive = false;
this.significantMotionActive = false;
this.stepCounterActive = false;
this.stepDetectorActive = false;
this.uncalibAccActive = false;
this.uncalibGyroActive = false;
this.uncalibMagActive = false;
this.gameRotVecActive = false;
this.geomagRotVecActive = false;
this.magneticActive = false;
this.proximityActive = false;
uni.showToast({
title: '已停止所有传感器',
icon: 'none'
});
}
}
}
</script>
<style>
.container {
padding: 20rpx;
background-color: #f5f5f5;
min-height: 100vh;
}
.scroll-area {
height: calc(100vh - 100rpx);
}
.sensor-card {
background-color: #fff;
border-radius: 20rpx;
padding: 30rpx;
margin-bottom: 20rpx;
box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}
.card-title {
font-size: 32rpx;
font-weight: bold;
color: #333;
margin-bottom: 20rpx;
border-left: 6rpx solid #007aff;
padding-left: 20rpx;
}
.sensor-value {
font-size: 28rpx;
color: #666;
margin-bottom: 20rpx;
background-color: #f9f9f9;
padding: 20rpx;
border-radius: 12rpx;
line-height: 1.5;
}
.button-group {
display: flex;
gap: 20rpx;
justify-content: space-between;
}
.stop-btn {
margin-top: 30rpx;
margin-bottom: 50rpx;
padding: 0 20rpx;
}
</style>