docs/api/get-device-info.md
::: sourceCode
GitCode: https://gitcode.com/dcloud/uni-api/tree/alpha/uni_modules/uni-getDeviceInfo
GitHub: https://github.com/dcloudio/uni-api/tree/alpha/uni_modules/uni-getDeviceInfo
:::
获取设备信息
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|
| 4.0 | 4.41 | 3.9 | 4.11 | 4.61 | 5.0 |
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| options | GetDeviceInfoOptions | 否 | 包含所有字段的过滤对象 | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | [options=包含所有字段的过滤对象]过滤的字段对象, 不传参数默认为获取全部字段。 |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| filter | Array<string> | 是 | - | Web: -; 微信小程序: -; Android: 3.9; iOS: -; HarmonyOS: x | 过滤字段的字符串数组,假如要获取指定字段,传入此数组。 |
| 类型 |
|---|
| GetDeviceInfoResult |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| deviceBrand | string | 否 | - | Web: x; 微信小程序: 4.41; Android: 3.9; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 设备品牌 |
| deviceId | string | 否 | - | Web: 4.0; 微信小程序: 4.41; Android: 3.9; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 设备 id 。由 uni-app 框架生成并存储,清空 Storage 会导致改变 |
| deviceModel | string | 否 | - | Web: 4.0; 微信小程序: 4.41; Android: 3.9; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 设备型号 |
| deviceType | string | 否 | - | Web: 4.0; 微信小程序: 4.41; Android: 3.9; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 设备类型phone、pad、pc |
| deviceOrientation | string | 否 | - | Web: 4.0; 微信小程序: x; Android: 3.9; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 设备方向 竖屏 portrait、横屏 landscape |
| devicePixelRatio | number | 否 | - | Web: 4.0; 微信小程序: x; Android: 3.9; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 设备像素比 |
| system | string | 否 | - | Web: 4.0; 微信小程序: 4.41; Android: 3.9; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 操作系统及版本 |
| platform | string | 否 | - | Web: 4.0; 微信小程序: 4.41; Android: 3.9; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 客户端平台 |
| isRoot | boolean | 否 | - | Web: x; 微信小程序: x; Android: 3.9; iOS: 4.11; HarmonyOS: x | 是否root。iOS 为是否越狱 |
| isUSBDebugging | boolean | 否 | - | Web: x; 微信小程序: x; Android: √; iOS: x; HarmonyOS: x | adb是否开启 |
| osName | string | 否 | - | Web: 4.18; 微信小程序: 4.41; Android: 4.18; iOS: 4.18; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 系统名称 |
| osVersion | string | 否 | - | Web: 4.18; 微信小程序: 4.41; Android: 4.18; iOS: 4.18; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 操作系统版本。如 ios 版本,andriod 版本 |
| osLanguage | string | 否 | - | Web: x; 微信小程序: x; Android: 4.18; iOS: 4.18; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 操作系统语言 |
| osTheme | string | 否 | - | Web: x; 微信小程序: x; Android: 4.18; iOS: 4.18; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 操作系统主题 |
| osAndroidAPILevel | number | 否 | - | Web: x; 微信小程序: x; Android: 4.18; iOS: x; HarmonyOS: x | Android 系统API库的版本。 |
| osHarmonySDKAPIVersion | number | 否 | - | Web: x; 微信小程序: x; Android: √; iOS: x; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 鸿蒙系统软件API版本 |
| osHarmonyDisplayVersion | string | 否 | - | Web: x; 微信小程序: x; Android: √; iOS: x; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 产品版本,关于本机信息内的软件版本 |
| romName | string | 否 | - | Web: x; 微信小程序: x; Android: 4.18; iOS: 4.18; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | rom 名称。Android 部分机型获取不到值。iOS 恒为 ios |
| romVersion | string | 否 | - | Web: x; 微信小程序: x; Android: 4.18; iOS: 4.18; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | rom 版本号。Android 部分机型获取不到值。iOS 为操作系统版本号(同 osVersion)。 |
| abi | string | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 应用(微信APP)二进制接口类型(仅 Android 支持) |
| benchmarkLevel | number | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | |
| cpuType | string | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 需要基础库: 2.29.0 |
设备 CPU 型号(仅 Android 支持)(Tips: GPU 型号可通过 WebGLRenderingContext.getExtension('WEBGL_debug_renderer_info') 来获取)
|
| deviceAbi | string | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 需要基础库: 2.25.1
设备二进制接口类型(仅 Android 支持)
|
| memorySize | string | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 需要基础库: 2.30.0
设备内存大小,单位为 MB
|
| brand | string | 否 | - | Web: x; 微信小程序: 4.41; Android: 3.9; iOS: 4.11; HarmonyOS: x | 设备品牌 已废弃,仅为了向下兼容保留 |
| model | string | 否 | - | Web: 4.0; 微信小程序: 4.41; Android: 3.9; iOS: 4.11; HarmonyOS: x | 设备型号 已废弃,仅为了向下兼容保留 |
| isSimulator | boolean | 否 | - | Web: x; 微信小程序: x; Android: 3.9; iOS: 4.11; HarmonyOS: x | 是否是模拟器
已废弃,由于合规问题在4.51版本后不会采集传感器信息,会影响准确度,建议使用isSimulator()代替。 |
| 合法值 | 兼容性 | 描述 |
|---|---|---|
| phone | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| pad | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| tv | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| watch | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| pc | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| null | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| car | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| vr | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| appliance | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| 合法值 | 兼容性 | 描述 |
|---|---|---|
| ios | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| android | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| harmonyos | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| mac | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| windows | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| linux | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| 合法值 | 兼容性 | 描述 |
|---|---|---|
| ios | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| android | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| harmonyos | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| macos | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| windows | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| linux | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| 合法值 | 兼容性 | 描述 |
|---|---|---|
| light | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| dark | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
::: warning 注意事项
uni.getDeviceInfo({filter:["brand"]})
从4.51+,本api的isSimulator调整为不再获取传感器信息,但判断模拟器的准确度会下降。如需更准确的判断模拟器,需要单独使用uni.isSimulator。注意也需要在同意隐私协议之后获取。 :::
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见 ::: preview https://hellouniappx.dcloud.net.cn/web/#/pages/API/get-device-info/get-device-info
appRedirect https://hellouniappx.dcloud.net.cn/appredirect.html?path=pages/API/get-device-info/get-device-info
示例
<template>
<!-- #ifdef APP -->
<scroll-view class="page-scroll-view">
<!-- #endif -->
<view>
<page-head :title="title"></page-head>
<view class="uni-common-mt">
<view class="uni-list">
<view class="uni-list">
<view class="uni-list-cell" v-for="(item, _) in data.items" style="align-items: center">
<view class="uni-pd">
<view class="uni-label" style="width: 180px">{{
item.label
}}</view>
</view>
<view class="uni-list-cell-db">
<text class="uni-list-cell-db-text">{{
item.value == "" ? "未获取" : item.value
}}</text>
</view>
</view>
</view>
</view>
<view class="uni-padding-wrap">
<view class="uni-btn-v">
<button type="primary" @tap="getDeviceInfo">获取设备信息</button>
</view>
</view>
</view>
</view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script setup lang="uts">
import { setDevicePixelRatio } from '@/store/index.uts'
type Item = {
label : string,
value : string,
}
type DataType = {
items: Item[];
}
const title = ref('getDeviceInfo')
const data = reactive({
items: [] as Item[],
} as DataType)
const getDeviceInfo = () => {
const res = uni.getDeviceInfo();
// 获取像素比, 供截图对比使用
setDevicePixelRatio(res.devicePixelRatio !== null ? res.devicePixelRatio! : 1)
data.items = [] as Item[];
const res_str = JSON.stringify(res);
const res_obj = JSON.parseObject(res_str);
const res_map = res_obj!.toMap();
let keys = [] as string[]
res_map.forEach((_, key) => {
keys.push(key);
});
keys.sort().forEach(key => {
const value = res[key];
if (value != null) {
const item = {
label: key,
value: "" + ((typeof value == "object") ? JSON.stringify(value) : value)
} as Item;
data.items.push(item);
}
});
}
</script>
<style>
.uni-pd {
padding-left: 15px;
}
</style>
:::
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 是 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 错误信息 |