跳过内容

开始入门

GitHub

您可以使用我们的 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-info
bunx 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,
});

is delta-based. The first native sample can omit it; call again later or use monitoring to receive populated CPU usage after the second sample.

Section titled “停止更新”
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 支持是基于最佳努力的,并报告了空的 onboard 传感器数组,因为浏览器不一致地暴露本机设备传感器。

类型参考

类型参考

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;
}

数据源

数据源

本页面是根据插件生成的 src/definitions.ts当公共API上游发生变化时,请重新同步。

继续从入门

继续从入门

如果您正在使用 入门 进行设备诊断,请将其连接到 @capgo/capacitor-device-info 查看概览 使用 @capgo/capacitor-device-info 为了教程 @capgo/capacitor-barometer 为了专注的压力读数,和 @capgo/capacitor-light-sensor 为了专注的光感读数。