更新记录
1.0.0(2026-08-26)
- 设备信息、电量信息,字段对齐 HarmonyOS
- App 端支持蓝牙状态查询与开关
- 覆盖 Android / iOS / HarmonyOS / 小程序 / 元服务
- 按 API 拆分模块,支持摇树
平台兼容性
uni-app(4.83)
| Vue2 |
Vue3 |
Chrome |
Safari |
app-vue |
app-nvue |
Android |
iOS |
鸿蒙 |
| √ |
√ |
√ |
√ |
√ |
√ |
√ |
√ |
√ |
| 微信小程序 |
支付宝小程序 |
抖音小程序 |
百度小程序 |
快手小程序 |
京东小程序 |
鸿蒙元服务 |
QQ小程序 |
飞书小程序 |
小红书小程序 |
快应用-华为 |
快应用-联盟 |
| √ |
√ |
√ |
√ |
√ |
√ |
√ |
√ |
√ |
√ |
√ |
√ |
uni-app x(4.83)
| Chrome |
Safari |
Android |
iOS |
鸿蒙 |
微信小程序 |
| √ |
√ |
√ |
√ |
√ |
√ |
umi-device-kit
基于 UTS 的多端设备与电量信息插件,字段分别对齐:
支持平台
| 平台 |
目录 |
设备信息 |
电量信息 |
| Android |
app-android |
原生 API |
原生 API |
| iOS |
app-ios |
原生 API |
原生 API |
| HarmonyOS App |
app-harmony |
@kit.BasicServicesKit |
@kit.BasicServicesKit |
| 微信小程序 |
mp-weixin |
uni API |
uni API |
| 支付宝小程序 |
mp-alipay |
uni API |
uni API |
| 百度小程序 |
mp-baidu |
uni API |
uni API |
| 抖音小程序 |
mp-toutiao |
uni API |
uni API |
| QQ 小程序 |
mp-qq |
uni API |
uni API |
| 飞书小程序 |
mp-lark |
uni API |
uni API |
| 快手小程序 |
mp-kuaishou |
uni API |
uni API |
| 京东小程序 |
mp-jd |
uni API |
uni API |
| 鸿蒙元服务 (ASCF) |
mp-harmony |
uni API |
uni API |
ASCF 元服务不支持 UTS 原生鸿蒙 API,请通过 mp-harmony 目录下的 uni API 实现。
使用方法
导入
import {
getDeviceInfo,
getDeviceInfoSync,
getBatteryInfo,
getBatteryInfoSync,
GetDeviceInfoOptions,
GetBatteryInfoOptions
} from '@/uni_modules/umi-device-kit'
获取设备信息
同步
const deviceInfo = getDeviceInfoSync()
console.log(deviceInfo.platform) // android | ios | harmony | mp-weixin ...
console.log(deviceInfo.brand) // 品牌
console.log(deviceInfo.productModel) // 认证型号
console.log(deviceInfo.osFullName) // 系统版本全称
console.log(deviceInfo.deviceType) // phone | pad | pc ...
异步
getDeviceInfo({
success(res) {
console.log(res.errMsg) // getDeviceInfo:ok
console.log(res.deviceInfo)
},
fail(err) {
console.error(err.errCode, err.errMsg) // 9010001
},
complete(res) {
console.log('complete', res)
}
} as GetDeviceInfoOptions)
获取电量信息
同步
const batteryInfo = getBatteryInfoSync()
console.log(batteryInfo.level) // 0-100
console.log(batteryInfo.isCharging) // true | false
console.log(batteryInfo.chargingStatus) // none | enable | disable | full
console.log(batteryInfo.voltage) // 微伏,无法获取为 -1
console.log(batteryInfo.batteryTemperature) // 摄氏度,无法获取为 -1
异步
getBatteryInfo({
success(res) {
console.log(res.errMsg) // getBatteryInfo:ok
console.log(res.batteryInfo.level, res.batteryInfo.isCharging)
},
fail(err) {
console.error(err.errCode, err.errMsg) // 9010002
},
complete(res) {
console.log('complete', res)
}
} as GetBatteryInfoOptions)
uni-app x 页面示例
<template>
<view class="page">
<text>品牌:{{ device.brand }}</text>
<text>型号:{{ device.productModel }}</text>
<text>系统:{{ device.osFullName }}</text>
<text>电量:{{ battery.level }}%</text>
<text>充电:{{ battery.isCharging ? '是' : '否' }}</text>
<button @click="refresh">刷新</button>
</view>
</template>
<script setup lang="uts">
import {
getDeviceInfoSync,
getBatteryInfoSync,
DeviceInfo,
BatteryInfo
} from '@/uni_modules/umi-device-kit'
const device = ref<DeviceInfo>(getDeviceInfoSync())
const battery = ref<BatteryInfo>(getBatteryInfoSync())
function refresh() {
device.value = getDeviceInfoSync()
battery.value = getBatteryInfoSync()
}
</script>
错误码
| 错误码 |
说明 |
| 9010001 |
获取设备信息失败 |
| 9010002 |
获取电量信息失败 |
DeviceInfo 字段说明
| 字段 |
类型 |
说明 |
HarmonyOS 对应 |
| platform |
string |
运行平台标识,如 android、ios、harmony、mp-weixin、mp-harmony |
— |
| deviceType |
string |
设备类型,如 phone、pad、pc、wearable |
deviceType |
| manufacture |
string |
设备厂家名称 |
manufacture |
| brand |
string |
设备品牌 |
brand |
| marketName |
string |
外部产品系列 / 市场名称 |
marketName |
| productSeries |
string |
产品系列 |
productSeries |
| productModel |
string |
认证型号 |
productModel |
| productModelAlias |
string |
认证型号别名 |
productModelAlias |
| softwareModel |
string |
内部软件子型号 |
softwareModel |
| hardwareModel |
string |
硬件版本号 |
hardwareModel |
| osFullName |
string |
系统版本全称,如 OpenHarmony-5.0.0.1、Android-14 |
osFullName |
| osReleaseType |
string |
系统发布类型:Canary / Beta / Release |
osReleaseType |
| displayVersion |
string |
产品版本 |
displayVersion |
| sdkApiVersion |
number |
SDK API 版本,无法获取为 -1 |
sdkApiVersion |
| securityPatchTag |
string |
安全补丁级别 |
securityPatchTag |
| abiList |
string |
应用二进制接口(ABI)列表 |
abiList |
| majorVersion |
number |
主版本号,无法获取为 -1 |
majorVersion |
| seniorVersion |
number |
Senior 版本号,无法获取为 -1 |
seniorVersion |
| featureVersion |
number |
Feature 版本号,无法获取为 -1 |
featureVersion |
| buildVersion |
number |
Build 版本号,无法获取为 -1 |
buildVersion |
| distributionOSName |
string |
发行版系统名称(HarmonyOS) |
distributionOSName |
| distributionOSVersion |
string |
发行版系统版本号(HarmonyOS) |
distributionOSVersion |
| distributionOSApiVersion |
number |
发行版系统 API 版本(HarmonyOS),无法获取为 -1 |
distributionOSApiVersion |
| model |
string |
设备型号(兼容字段,等同 productModel) |
— |
| system |
string |
操作系统及版本(兼容字段) |
— |
GetDeviceInfoSuccess(异步成功回调)
| 字段 |
类型 |
说明 |
| errMsg |
string |
固定为 getDeviceInfo:ok |
| deviceInfo |
DeviceInfo |
设备信息对象 |
BatteryInfo 字段说明
| 字段 |
类型 |
说明 |
HarmonyOS 对应 |
| level |
number |
剩余电量百分比 0-100 |
batterySOC |
| isCharging |
boolean |
是否正在充电 |
chargingStatus(派生) |
| chargingStatus |
string |
充电状态:none / enable / disable / full / unknown |
BatteryChargeState |
| healthStatus |
string |
电池健康状态,如 good、overheat、unknown |
healthStatus |
| pluggedType |
string |
充电器连接类型:none / ac / usb / wireless / unknown |
pluggedType |
| voltage |
number |
电压,单位微伏,无法获取为 -1 |
voltage |
| technology |
string |
电池技术型号 |
technology |
| batteryTemperature |
number |
电池温度,单位摄氏度,无法获取为 -1 |
batteryTemperature |
| isBatteryPresent |
boolean |
是否支持电池或电池在位 |
isBatteryPresent |
| nowCurrent |
number |
当前电流,单位毫安,无法获取为 -1 |
nowCurrent |
GetBatteryInfoSuccess(异步成功回调)
| 字段 |
类型 |
说明 |
| errMsg |
string |
固定为 getBatteryInfo:ok |
| batteryInfo |
BatteryInfo |
电量信息对象 |
平台字段支持差异
| 能力 |
HarmonyOS App |
Android |
iOS |
小程序 / ASCF |
| DeviceInfo 完整字段 |
✅ |
部分 |
部分 |
基础字段 |
| BatteryInfo 完整字段 |
✅ |
部分 |
基础 |
基础 |
| voltage / temperature |
✅ |
✅ |
❌ |
❌ |
| nowCurrent |
✅ |
✅ |
❌ |
❌ |
无法获取的字符串字段返回 '',数值字段返回 -1,布尔字段返回 false。
摇树优化(Tree Shaking)
本插件支持摇树优化:业务代码中实际 import 的 API 及其依赖才会被打包,未使用的 API 不会增加包体积。
原理
- 按 API 拆分模块:设备信息与电量信息分别独立文件,互不引用
utssdk/
├── app-android/
│ ├── device-info.uts # getDeviceInfo / getDeviceInfoSync
│ ├── battery-info.uts # getBatteryInfo / getBatteryInfoSync
│ └── index.uts # 聚合导出
├── common/
│ ├── device-info-mp.uts
│ └── battery-info-mp.uts
└── ...
- 按需 import:使用具名导入,只引入需要的 API
// ✅ 仅打包设备信息相关代码(不含 BatteryManager 等电量依赖)
import { getDeviceInfoSync } from '@/uni_modules/umi-device-kit'
// ✅ 仅打包电量信息相关代码
import { getBatteryInfoSync } from '@/uni_modules/umi-device-kit'
// ⚠️ 同时 import 两个 API 则两者均会打包
import { getDeviceInfoSync, getBatteryInfoSync } from '@/uni_modules/umi-device-kit'
- package.json 配置:已开启各平台摇树
"treeShaking": {
"app": { "android": true, "ios": true, "harmony": true },
"mp-weixin": true,
"mp-alipay": true
}
注意事项
- 打自定义基座时,若代码中尚未 import 或调用插件 API,编译时会被摇掉,导致基座不包含该插件。请先在业务代码中引用后再打包。
- 修改 import 后建议清理
unpackage/cache 再重新编译。
- 请勿使用
import * as Kit from '@/uni_modules/umi-device-kit' 方式导入,会导致全部 API 被打包。
注意事项
- 字段对齐:所有平台统一返回
DeviceInfo / BatteryInfo 结构,HarmonyOS App 端字段最完整。
- 隐私合规:未暴露
serial、udid 等需系统权限的敏感字段。
- ASCF 元服务:不可直接调用
@kit.BasicServicesKit,请使用 mp-harmony 实现。
- 导入规范:请从插件根目录
@/uni_modules/umi-device-kit 导入,不要直接导入 utssdk 子目录文件。
- 类型提示:可在
.uvue 中导入 DeviceInfo、BatteryInfo、GetDeviceInfoOptions、GetBatteryInfoOptions 获得类型提示。