开始使用
复制一个包含安装步骤和完整 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.
安装
安装您可以使用我们的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.如果您更喜欢手动设置,请运行以下命令并按照以下平台特定的说明进行安装:
bun add @capgo/capacitor-device-infobunx 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 首个本地样本可以省略它;稍后再次调用或使用监控接收第二个样本后的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);});
await DeviceInfo.startMonitoring({ intervalMs: 1000, emitImmediately: true,});标题:停止更新
复制到剪贴板await DeviceInfo.stopMonitoring();await handle.remove();您也可以停止自动:
await DeviceInfo.startMonitoring({ intervalMs: 1000, durationMs: 60_000, sampleCount: 60,});检查监控状态
标题:检查监控状态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 支持是最佳努力,并报告空载的传感器数组,因为浏览器不一致地暴露本机设备传感器。
类型参考
类型参考DeviceInfoSnapshot
设备信息快照export interface DeviceInfoSnapshot { timestamp: number; platform: 'ios' | 'android' | 'web'; cpu: CpuInfo; memory: MemoryInfo; storage: StorageInfo; gpu?: GpuInfo; thermalState?: ThermalState; lowPowerMode?: boolean; sensors?: OnboardSensorsInfo;}OnboardSensorsInfo
设备上载传感器信息export interface OnboardSensorsInfo { availableSensors?: OnboardSensorDescriptor[]; readings?: OnboardSensorReading[]; batteryTemperatureCelsius?: number; ambientTemperatureCelsius?: number; relativeHumidityPercent?: number; pressureHpa?: number; illuminanceLux?: number; proximityDistanceCm?: number;}MonitoringOptions
监控选项export interface MonitoringOptions { intervalMs?: number; durationMs?: number; sampleCount?: number; emitImmediately?: boolean;}真实来源
设备信息快照This page is generated from the plugin’s src/definitions.ts. Re-run the sync when the public API changes upstream.
从开始
标题:从开始如果您正在使用 开始 用于设备诊断的 @capgo/capacitor-device-info @__CAPGO_KEEP_0__/__CAPGO_KEEP_1__-device-info Using @capgo/capacitor-device-info 使用@__CAPGO_KEEP_0__/__CAPGO_KEEP_1__-device-info @capgo/capacitor-barometer 为了专注于压力读数,和 @capgo/capacitor-light-sensor 为了专注于光线传感器读数。