# uni.getDeviceInfo(options?)

获取设备信息

# getDeviceInfo 兼容性

Web 微信小程序 Android iOS HarmonyOS
4.0 4.41 3.9 4.11 4.61

# 参数

名称 类型 必填 默认值 兼容性 描述
options GetDeviceInfoOptions 包含所有字段的过滤对象 [options=包含所有字段的过滤对象]过滤的字段对象, 不传参数默认为获取全部字段。
名称 类型 必备 默认值 兼容性 描述
filter Array<string>
过滤字段的字符串数组,假如要获取指定字段,传入此数组。

# 返回值

类型
GetDeviceInfoResult
名称 类型 必备 默认值 兼容性 描述
deviceBrand string
设备品牌
deviceId string
设备 id 。由 uni-app 框架生成并存储,清空 Storage 会导致改变
deviceModel string
设备型号
deviceType string
设备类型phone、pad、pc
合法值 描述
phone
pad
tv
watch
pc
null
car
vr
appliance
deviceOrientation string
设备方向 竖屏 portrait、横屏 landscape
devicePixelRatio number
设备像素比
system string
操作系统及版本
platform string
客户端平台
合法值 描述
ios
android
harmonyos
mac
windows
linux
isRoot boolean
是否root。iOS 为是否越狱
isUSBDebugging boolean
adb是否开启
osName string
系统名称
合法值 描述
ios
android
harmonyos
macos
windows
linux
osVersion string
操作系统版本。如 ios 版本,andriod 版本
osLanguage string
操作系统语言
osTheme string
操作系统主题
合法值 描述
light
dark
osAndroidAPILevel number
Android 系统API库的版本。
osHarmonySDKAPIVersion number
鸿蒙系统软件API版本
osHarmonyDisplayVersion string
产品版本,关于本机信息内的软件版本
romName string
rom 名称。Android 部分机型获取不到值。iOS 恒为 ios
romVersion string
rom 版本号。Android 部分机型获取不到值。iOS 为操作系统版本号(同 osVersion)。
abi string
应用(微信APP)二进制接口类型(仅 Android 支持)
benchmarkLevel number
cpuType string
需要基础库: 2.29.0

设备 CPU 型号(仅 Android 支持)(Tips: GPU 型号可通过 WebGLRenderingContext.getExtension('WEBGL_debug_renderer_info') 来获取)
deviceAbi string
需要基础库: 2.25.1

设备二进制接口类型(仅 Android 支持)
memorySize string
需要基础库: 2.30.0

设备内存大小,单位为 MB
brand string
设备品牌 已废弃,仅为了向下兼容保留
model string
设备型号 已废弃,仅为了向下兼容保留
isSimulator boolean
是否是模拟器
已废弃,由于合规问题在4.51版本后不会采集传感器信息,会影响准确度,建议使用isSimulator()代替。

注意事项

  • 获取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。注意也需要在同意隐私协议之后获取。
  • 自 5.11 起:
    • web 平台在 macOS 上 platform 正确的返回 mac
    • web 平台在 macOS 上获取到的 osVersion 固定为 10.15.7,这和系统版本不一致,这不是 bug,是浏览器 ua 的限制(从 macOS 11 开始,Apple 和 Google、Mozilla 等浏览器厂商达成共识,减少通过 UA 暴露精确的系统版本)
    • web 平台运行在鸿蒙 PC 上时 osName 仅返回 harmonyos(不会返回 ohos_pc),可通过 deviceType 为 pc 区分
    • 小程序平台统一标准:鸿蒙系统 osName、platform 为 harmonyos,手机、Tablet 和 PC 通过 deviceType 区分

# 示例

示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见

扫码体验(手机浏览器跳转到App直达页)

示例

<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

名称 类型 必备 默认值 兼容性 描述
errMsg string
错误信息