开始使用
复制一个包含安装步骤和完整 Markdown 指南的配置提示。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-device-info`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/device-info/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
兼容性
兼容性@capgo/capacitor-device-info 需要 Capacitor 8 或更高版本(@capacitor/core >=8.0.0).
安装
安装您可以使用我们的 AI 助手设置来安装插件。使用以下命令将 Capgo 技能添加到您的 AI 工具中:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins然后使用以下提示:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-device-info` plugin in my project.对于手动设置,请安装包并同步本机项目:
npm install @capgo/capacitor-device-infonpx cap syncimport { DeviceInfo } from '@capgo/capacitor-device-info';读取一个快照
名为“读取一个快照”的部分const snapshot = await DeviceInfo.getInfo();
console.log(snapshot.cpu.cores);console.log(snapshot.memory.usedPercent);console.log(snapshot.storage.freeBytes);console.log(snapshot.sensors?.pressureHpa);cpu.usagePercent 首个本地样本可以省略它;稍后再次调用或使用监控接收第二个样本后的CPU使用率. getInfo() 实时更新
名为“实时更新”的部分
复制到剪贴板const handle = await DeviceInfo.addListener('deviceInfoUpdate', (sample) => { console.log(sample.sequence, sample.elapsedMs); console.log(sample.cpu.usagePercent); console.log(sample.sensors?.readings);});
const session = await DeviceInfo.startMonitoring({ intervalMs: 1000, emitImmediately: true,});
console.log(session.intervalMs, session.startedAt);行为监控
行为监控intervalMs默认值1000毫秒。小于250毫秒的值会被限制到250.emitImmediately默认值true.- 设置
durationMs或sampleCount自动停止监控。 - 在监控期间调用
startMonitoring()会重启会话并使用新的选项。 - 每
deviceInfoUpdate包含一个基于一的sequence,会话的startedAt时间戳和elapsedMs.
停止更新并移除监听器
标题:停止更新并移除监听器await DeviceInfo.stopMonitoring();await handle.remove();
// Or remove every listener registered on this plugin:await DeviceInfo.removeAllListeners();检查监控状态
标题:检查监控状态const state = await DeviceInfo.isMonitoring();
if (state.monitoring) { console.log(state.samplesEmitted);}配置传感器字段
标题:配置传感器字段const { sensors, cpu, gpu } = await DeviceInfo.getInfo();
console.log(cpu.temperatureCelsius);console.log(gpu?.temperatureCelsius);console.log(sensors?.batteryTemperatureCelsius);console.log(sensors?.ambientTemperatureCelsius);console.log(sensors?.relativeHumidityPercent);console.log(sensors?.pressureHpa);console.log(sensors?.illuminanceLux);console.log(sensors?.proximityDistanceCm);console.log(sensors?.availableSensors);传感器字段是可选的。它们仅在设备、操作系统和应用程序沙盒暴露该指标时才会出现。
平台说明
标题为“平台说明”的部分- iOS 不需要权限来暴露指标。它报告了 CoreMotion 传感器可用性,但没有报告原始 CPU 或 GPU 温度。
- Android 不需要权限来暴露指标。CPU 和 GPU 温度是最佳努力的热区读取。
- Web 支持是最佳努力,并报告空的 onboard 传感器数组,因为浏览器不一致地暴露本机设备传感器。
API 参考
标题为“API 参考”的部分| 方法 | 返回 | 注意 |
|---|---|---|
getInfo() | Promise<DeviceInfoSnapshot> | 读取一个快照。 |
startMonitoring(options?) | Promise<StartMonitoringResult> | 启动周期性快照,或者重启一个活跃的会话并使用新的选项。 |
stopMonitoring() | Promise<StopMonitoringResult> | 停止活跃的会话。 |
isMonitoring() | Promise<MonitoringState> | 返回当前会话状态。 |
addListener('deviceInfoUpdate', listener) | Promise<PluginListenerHandle> | 注册一个周期性快照监听器。 |
removeAllListeners() | Promise<void> | 移除该插件上注册的所有监听器。 |
getPluginVersion() | Promise<PluginVersionResult> | 读取本地插件版本。 |
PluginListenerHandle 由 Capacitor 提供,并有 remove() 用于移除该个别监听器。
快照类型
标题:快照类型interface CpuInfo { cores: number; activeCores?: number; architecture?: string; model?: string; usagePercent?: number | null; maxFrequencyHz?: number; temperatureCelsius?: number;}
interface MemoryInfo { totalBytes?: number; freeBytes?: number; usedBytes?: number; usedPercent?: number; appUsedBytes?: number; appLimitBytes?: number; lowMemory?: boolean; pressure?: 'normal' | 'warning' | 'critical' | 'unknown';}
interface StorageInfo { totalBytes?: number; freeBytes?: number; usedBytes?: number; usedPercent?: number;}
interface GpuInfo { api?: 'metal' | 'opengl' | 'webgl' | 'unknown'; vendor?: string; renderer?: string; version?: string; maxTextureSize?: number; temperatureCelsius?: number;}
type ThermalState = 'nominal' | 'fair' | 'serious' | 'critical' | 'unknown';
interface DeviceInfoSnapshot { timestamp: number; platform: 'ios' | 'android' | 'web'; cpu: CpuInfo; memory: MemoryInfo; storage: StorageInfo; gpu?: GpuInfo; thermalState?: ThermalState; lowPowerMode?: boolean; sensors?: OnboardSensorsInfo;}大小以字节为单位, maxFrequencyHz 是赫兹,温度值是摄氏度。 usagePercent 可以 null 直到平台有足够的样本来计算它。
传感器类型
传感器类型interface OnboardSensorDescriptor { type: string; name?: string; vendor?: string; platformType?: number; maximumRange?: number; resolution?: number; powerMilliamp?: number; minDelayMicroseconds?: number; wakeUp?: boolean;}
interface OnboardSensorReading { type: string; unit: string; value: number; name?: string; timestamp?: number;}
interface OnboardSensorsInfo { availableSensors?: OnboardSensorDescriptor[]; readings?: OnboardSensorReading[]; batteryTemperatureCelsius?: number; ambientTemperatureCelsius?: number; relativeHumidityPercent?: number; pressureHpa?: number; illuminanceLux?: number; proximityDistanceCm?: number;}传感器读数的时间戳是Unix时间戳毫秒。常见单位是摄氏度,百分比,百帕,光度,和厘米。
监控类型
监控类型interface MonitoringOptions { intervalMs?: number; durationMs?: number; sampleCount?: number; emitImmediately?: boolean;}
interface StartMonitoringResult { monitoring: boolean; intervalMs: number; startedAt: number;}
interface StopMonitoringResult { monitoring: boolean;}
interface MonitoringState { monitoring: boolean; intervalMs?: number; startedAt?: number; samplesEmitted?: number;}
interface DeviceInfoUpdate extends DeviceInfoSnapshot { sequence: number; startedAt: number; elapsedMs: number;}
interface PluginVersionResult { version: string;}startedAt, timestamp毫秒 elapsedMs 毫秒 sequence 开始于 1 每次监控会话都有。
真实来源
标题:真实来源本参考文档遵循了公共的 API 在插件中。 src/definitions.ts当真实来源发生变化时,请更新本页面。
从 Getting Started 开始
标题:从 Getting Started 开始如果您正在使用 Getting Started 进行设备诊断,请将其连接到 @capgo/capacitor-device-info 概览 使用@capgo/capacitor-device-info 教程页面 @capgo/capacitor-barometer 专注于压力读数 @capgo/capacitor-light-sensor 编辑