开始
复制一个包含安装步骤和此插件的完整 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 sync导入
导入import { 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 基于差异的。第一次本机样本可以省略它;在第二个样本后调用 getInfo() 再次调用或使用监控接收填充的CPU使用率
实时更新
标题:实时更新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__CAPGO_KEEP_0__true.- 设置
durationMs或sampleCount选择最佳方案 - 停止自动监控。
startMonitoring()正在监控时调用 - 监控正在激活时重启会话将使用新的选项。
deviceInfoUpdate每次sequence包括从 1 开始的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 支持是最佳努力,报告空的设备信息数组,因为浏览器不一致地暴露本机设备传感器。
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 epoch 毫秒为单位。常见单位包括摄氏度、百分比、百帕、光度和厘米。
监控类型
监控类型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 真实来源
This reference follows the public API in the plugin’s src/definitions.ts监控类型
继续从开始
标题:继续从开始如果您正在使用 开始 进行设备诊断的 @capgo/capacitor-device-info 查看概览 @capgo/capacitor-device-info 查看教程 @capgo/capacitor-barometer 获取专注的压力读数 @capgo/capacitor-light-sensor 为了获得聚焦光感应器读数。