npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-native-device-secure-info

v1.1.0

Published

React Native native module to get device secure information including device info, memory, CPU, and battery status. Supports both Old Architecture (Bridge) and New Architecture (TurboModule).

Downloads

195

Readme

react-native-device-secure-info

一个 React Native 原生模块,用于获取设备安全信息,包括设备基础信息、内存、CPU 和电池状态。同时支持新架构(TurboModule)旧架构(Bridge)

截图

功能特性

  • 📱 基础设备信息 — 设备名称、系统版本、品牌、制造商、设备型号、设备 ID、运营商、SDK 版本
  • 💾 内存信息 — 总内存、可用内存、已用内存、内存使用率
  • ⚙️ CPU 信息 — CPU 架构、核心数、使用率
  • 🔋 电池信息 — 电量百分比、充电状态、是否正在充电
  • 🚀 双架构支持 — 自动适配新架构(TurboModule)和旧架构(Bridge)
  • 📦 TypeScript 支持 — 完整的类型定义

平台支持

| 平台 | 支持 | | --- | --- | | iOS | ✅ | | Android | ✅ |

环境要求

  • React Native >= 0.71.0
  • React >= 18.0.0
  • iOS >= 13.4
  • Android minSdkVersion >= 24
  • Android Gradle Plugin >= 7.0(本库使用 namespace 而不是 manifest 的 package 属性)

安装

npm install react-native-device-secure-info
# 或
yarn add react-native-device-secure-info

iOS

cd ios && pod install

Android

无需额外配置,React Native CLI 的 autolinking 会自动完成链接。

使用方式

import { getDeviceInfo } from 'react-native-device-secure-info';

async function fetchInfo() {
  const info = await getDeviceInfo();

  // 基础设备信息
  console.log('设备名称:', info.deviceName);
  console.log('系统:', info.systemName, info.systemVersion);
  console.log('品牌:', info.brand);
  console.log('制造商:', info.manufacturer);
  console.log('设备型号:', info.deviceModel);
  console.log('设备ID:', info.deviceId);
  console.log('运营商:', info.carrierName);

  // 内存信息
  console.log('总内存:', info.totalMemory);
  console.log('可用内存:', info.availableMemory);
  console.log('内存使用率:', info.memoryUsagePercent + '%');

  // CPU 信息
  console.log('CPU架构:', info.cpuArchitecture);
  console.log('CPU核心数:', info.cpuCores);
  console.log('CPU使用率:', info.cpuUsagePercent + '%');

  // 电池信息
  console.log('电量:', info.batteryLevel + '%');
  console.log('充电状态:', info.batteryState);
  console.log('正在充电:', info.isCharging);
}

API

getDeviceInfo(): Promise<DeviceSecureInfo>

获取设备安全信息,返回一个 Promise,resolve 后得到 DeviceSecureInfo 对象。

DeviceSecureInfo 类型定义

interface DeviceSecureInfo {
  // ---- 基础设备信息 ----
  deviceName: string;        // 设备名称(如 "iPhone 17 Pro")
  systemVersion: string;     // 系统版本号(如 "26.2" / "14")
  brand: string;             // 设备品牌(如 "Apple" / "Samsung")
  manufacturer: string;      // 制造商(如 "Apple" / "Xiaomi")
  deviceId: string;          // 设备唯一标识
  carrierName: string;       // 运营商名称(如 "中国移动")
  sdkVersion: number;        // SDK 版本号
  deviceModel: string;       // 设备型号(如 "iPhone14,2" / "arm64")
  systemName: string;        // 系统名称(如 "iOS" / "Android")

  // ---- 内存信息 ----
  totalMemory: number;       // 总内存(字节)
  availableMemory: number;   // 可用内存(字节)
  usedMemory: number;        // 已使用内存(字节)
  memoryUsagePercent: number; // 内存使用百分比

  // ---- CPU 信息 ----
  cpuArchitecture: string;   // CPU 架构(如 "arm64")
  cpuCores: number;          // CPU 核心数
  cpuUsagePercent: number;   // CPU 使用率百分比

  // ---- 电池信息 ----
  batteryLevel: number;      // 电池电量百分比(0-100,-1 表示未知)
  batteryState: string;      // 电池状态("charging" / "discharging" / "full" / "unknown")
  isCharging: boolean;       // 是否正在充电
}

架构说明

本模块同时支持 React Native 新架构和旧架构:

| 架构 | 实现方式 | 说明 | | --- | --- | --- | | 新架构 | TurboModule + Codegen | 通过 TurboModuleRegistry 自动注册,性能更优 | | 旧架构 | NativeModules (Bridge) | 通过 NativeModules.DeviceSecureInfoModule 获取 |

模块入口(src/index.ts)会自动检测当前架构并选择对应的实现,使用者无需关心底层差异。

项目结构(标准 RN 库结构)

react-native-device-secure-info/
├── src/                                        # JS/TS 库源码
│   ├── index.ts                                # JS 入口,自动适配新旧架构
│   └── NativeDeviceSecureInfo.ts               # TurboModule Spec 定义(Codegen)
├── ios/                                        # iOS 库源码(仅 .h/.m/.mm/.swift)
│   ├── DeviceSecureInfoModule.h
│   ├── DeviceSecureInfoModule.m                # 旧架构(Bridge)
│   ├── DeviceSecureInfoTurboModule.h
│   └── DeviceSecureInfoTurboModule.mm          # 新架构(TurboModule)
├── android/                                    # Android 库 (com.android.library)
│   ├── build.gradle                            # 声明了 namespace = com.reactnativedevicesecureinfo
│   ├── gradle.properties                       # 可被消费方 rootProject.ext 覆盖
│   └── src/main/
│       ├── AndroidManifest.xml                 # 空 manifest,不含 package 属性
│       └── java/com/reactnativedevicesecureinfo/
│           ├── DeviceSecureInfoModule.kt       # 旧架构模块
│           ├── DeviceSecureInfoPackage.kt      # 旧架构 ReactPackage
│           ├── DeviceSecureInfoTurboModule.kt  # 新架构 TurboModule
│           └── DeviceSecureInfoTurboPackage.kt # 新架构 TurboReactPackage
├── example/                                    # 用于本地开发调试的示例 App
│   ├── android/                                # 完整的 RN App 项目
│   ├── ios/                                    # 完整的 RN App 项目
│   ├── src/…
│   ├── App.tsx
│   ├── package.json
│   ├── react-native.config.js                  # 声明 dependencies[<pkg.name>].root = ..
│   └── metro.config.js                         # watchFolders + extraNodeModules 指向上级库
├── react-native-device-secure-info.podspec     # iOS CocoaPods 配置
├── react-native.config.js                      # 消费方 autolink 配置
└── package.json

改造说明:早期版本的 android/ 目录是通过 react-native init 生成的完整 App 项目 (android/app/build.gradle 使用了 com.android.application),导致消费方运行 npx @react-native-community/cli config 时抛出:

error Failed to build the app: No package name found.
We couldn't parse the namespace from neither your build.gradle[.kts] file at null
nor your package in the AndroidManifest at ...

现已改造为标准库结构:根 android/ 使用 com.android.library 插件并显式声明 namespace,AndroidManifest 保持空 <manifest> 即可通过新版 AGP 的校验。

本地开发

依赖安装(分别在根仓库与 example 中安装,避免 hoist 引起的重复副本):

# 在仓库根安装库自身依赖
npm install

# 再进入 example 安装示例 App 依赖
cd example
npm install

运行示例 App

# 在仓库根目录执行
npm run example:start          # 启动 Metro(等同于 cd example && npm run start)
npm run example:android        # cd example && npm run android
npm run example:ios            # cd example && npm run ios(需要先在 example/ios 下 pod install)

iOS Pods

cd example/ios && pod install

发布到 npm

一、发布策略

| 类型 | 是否需要打包 | 说明 | | --- | --- | --- | | 原生代码(ios/*.h/.m/.mmandroid/**/*.ktbuild.gradleAndroidManifest.xml) | ❌ 不打包 | 直接源码发布。消费方 Xcode / Gradle 会在自己工程里编译。 | | JS/TS 代码(src/) | ✅ 打包 | 用 react-native-builder-bob 编译成 lib/commonjslib/modulelib/typescript,覆盖 CommonJS / ESM / TypeScript 声明。 |

package.json 里的 main / module / types 指向编译产物,而 react-native 字段指向 src/index.ts,让 Metro 直接读源码(保留调试便利),其它环境读编译产物。

二、发布前检查产物

# 只做打包演练,不真的发布,输出即将进入 tarball 的文件列表
npm run pack:check

# 或者生成实际 tarball 到本地
npm pack
# 会产出 react-native-device-secure-info-<version>.tgz

产物应当只包含:src/lib/android/build.gradleandroid/gradle.propertiesandroid/src/ios/podspecreact-native.config.jsREADME.mdLICENSECHANGELOG.md

绝对不该出现example/android/build/android/.gradle/ios/Pods/ios/build/node_modules/、任何测试/mock 文件。

三、发布流程

# 1) 更新版本号(会自动 git commit + 打 tag)
npm run version:patch     # 1.0.0 → 1.0.1
# 或
npm run version:minor     # 1.0.0 → 1.1.0
npm run version:major     # 1.0.0 → 2.0.0

# 2) 推送 tag 到远程
git push --follow-tags

# 3) 发布到 npm(触发 prepublishOnly:clean + typecheck + lint + test + bob build)
npm run release
# 首次发布公开包:需要保证已经 npm login,并且包名可用

四、脚本一览

| 脚本 | 作用 | | --- | --- | | npm run clean | 清理 lib/android/build/ 等构建产物 | | npm run typecheck | 只做 TS 类型检查(不产出文件) | | npm test | 跑 Jest 单测 | | npm run lint | ESLint 检查 | | npm run prepare | 由 npm 生命周期自动触发(安装时/发布时),执行 bob build 产出 lib/ | | npm run prepublishOnly | 发布前钩子:clean → typecheck → lint → test → bob build | | npm run pack:check | npm pack --dry-run,检查即将发布的文件列表 | | npm run version:{patch,minor,major} | 版本号 bump + 自动 git commit + tag | | npm run release | npm publish --access public |

五、files 白名单 vs .npmignore

  • 采用 files 白名单作为主控(package.json 中)
  • 同时保留 .npmignore 作为兜底黑名单,防止意外把 example/android/build/ios/Pods/ 等打进 tarball
  • 白名单优先级高于 .npmignore;两者一起用是常见的双保险实践

License

MIT