Back to Uni App

Get Device Info

docs/api/get-device-info.md

2.3.313.4 KB
Original Source
<!-- ## uni.getDeviceInfo(options?) @getdeviceinfo -->

::: sourceCode

uni.getDeviceInfo(options?) @getdeviceinfo

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

:::

获取设备信息

getDeviceInfo 兼容性

Web微信小程序AndroidiOSHarmonyOSHarmonyOS(Vapor)
4.04.413.94.114.615.0

参数

名称类型必填默认值兼容性描述
optionsGetDeviceInfoOptions包含所有字段的过滤对象Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -[options=包含所有字段的过滤对象]过滤的字段对象, 不传参数默认为获取全部字段。

options 的属性描述

名称类型必备默认值兼容性描述
filterArray<string>-Web: -; 微信小程序: -; Android: 3.9; iOS: -; HarmonyOS: x过滤字段的字符串数组,假如要获取指定字段,传入此数组。

返回值

类型
GetDeviceInfoResult

GetDeviceInfoResult 的属性描述

名称类型必备默认值兼容性描述
deviceBrandstring-Web: x; 微信小程序: 4.41; Android: 3.9; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0设备品牌
deviceIdstring-Web: 4.0; 微信小程序: 4.41; Android: 3.9; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0设备 id 。由 uni-app 框架生成并存储,清空 Storage 会导致改变
deviceModelstring-Web: 4.0; 微信小程序: 4.41; Android: 3.9; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0设备型号
deviceTypestring-Web: 4.0; 微信小程序: 4.41; Android: 3.9; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0设备类型phone、pad、pc
deviceOrientationstring-Web: 4.0; 微信小程序: x; Android: 3.9; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0设备方向 竖屏 portrait、横屏 landscape
devicePixelRationumber-Web: 4.0; 微信小程序: x; Android: 3.9; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0设备像素比
systemstring-Web: 4.0; 微信小程序: 4.41; Android: 3.9; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0操作系统及版本
platformstring-Web: 4.0; 微信小程序: 4.41; Android: 3.9; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0客户端平台
isRootboolean-Web: x; 微信小程序: x; Android: 3.9; iOS: 4.11; HarmonyOS: x是否root。iOS 为是否越狱
isUSBDebuggingboolean-Web: x; 微信小程序: x; Android: √; iOS: x; HarmonyOS: xadb是否开启
osNamestring-Web: 4.18; 微信小程序: 4.41; Android: 4.18; iOS: 4.18; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0系统名称
osVersionstring-Web: 4.18; 微信小程序: 4.41; Android: 4.18; iOS: 4.18; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0操作系统版本。如 ios 版本,andriod 版本
osLanguagestring-Web: x; 微信小程序: x; Android: 4.18; iOS: 4.18; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0操作系统语言
osThemestring-Web: x; 微信小程序: x; Android: 4.18; iOS: 4.18; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0操作系统主题
osAndroidAPILevelnumber-Web: x; 微信小程序: x; Android: 4.18; iOS: x; HarmonyOS: xAndroid 系统API库的版本。
osHarmonySDKAPIVersionnumber-Web: x; 微信小程序: x; Android: √; iOS: x; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0鸿蒙系统软件API版本
osHarmonyDisplayVersionstring-Web: x; 微信小程序: x; Android: √; iOS: x; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0产品版本,关于本机信息内的软件版本
romNamestring-Web: x; 微信小程序: x; Android: 4.18; iOS: 4.18; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0rom 名称。Android 部分机型获取不到值。iOS 恒为 ios
romVersionstring-Web: x; 微信小程序: x; Android: 4.18; iOS: 4.18; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0rom 版本号。Android 部分机型获取不到值。iOS 为操作系统版本号(同 osVersion)。
abistring-Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: -应用(微信APP)二进制接口类型(仅 Android 支持)
benchmarkLevelnumber-Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: -
cpuTypestring-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()代替。 |

deviceType 的属性描述
合法值兼容性描述
phoneWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
padWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
tvWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
watchWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
pcWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
nullWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
carWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
vrWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
applianceWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
platform 的属性描述
合法值兼容性描述
iosWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
androidWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
harmonyosWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
macWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
windowsWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
linuxWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
osName 的属性描述
合法值兼容性描述
iosWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
androidWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
harmonyosWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
macosWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
windowsWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
linuxWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
osTheme 的属性描述
合法值兼容性描述
lightWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
darkWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --

::: warning 注意事项

  • 获取OAID、AndroidID等其他设备信息,见插件市场
  • romName值域候选 详见
  • app平台devicePixelRatio属性,HBuilderX4.22及以下版本类型为string,HBuilderX4.23版本调整为number
  • 在4.51以前,调用本api会获取传感器列表,原因是isSimulator属性判断模拟器时,需要根据传感器信息才能做出更准确的判断。但是,应用商店不允许在同意隐私协议前调用传感器。解决方案有2个:1、在同意隐私协议之后再调用,且隐私协议里声明调用传感器的用途;2、按需加载需要的信息,例如下面的代码,只获取品牌,不获取isSimulator:
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

示例

vue
<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>

:::

参见

通用类型

GeneralCallbackResult

名称类型必备默认值兼容性描述
errMsgstring-Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: -错误信息