@amazon-devices/expo-device提供对有关物理设备的系统信息的访问,例如其制造商和型号。
安装
-
在package.json文件中添加JavaScript库依赖项。
dependencies: { ... "@amazon-devices/expo-device": "~2.0.1", } -
使用
npm install命令重新安装依赖项。 -
运行
vega build命令。
示例
import React, {useEffect, useState} from 'react';
import {StyleSheet, View, Text} from 'react-native';
import * as Device from '@amazon-devices/expo-device';
export const App = () => {
const [deviceType, setDeviceType] = useState<Device.DeviceType>();
const [uptime, setUptime] = useState<number>();
useEffect(() => {
Device.getDeviceTypeAsync().then(setDeviceType);
Device.getUptimeAsync().then(setUptime);
}, []);
return (
<View style={styles.container}>
<Text style={styles.text}>manufacturer: {Device.manufacturer}</Text>
<Text style={styles.text}>modelName: {Device.modelName}</Text>
<Text style={styles.text}>
is TV: {String(deviceType === Device.DeviceType.TV)}
</Text>
<Text style={styles.text}>uptime: {uptime}</Text>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
justifyContent: 'center',
alignItems: 'center',
},
text: {
fontSize: 30,
color: 'black',
},
});
API参考
请查看专门文档页面,了解有关此库和API参考的信息:针对expo-device的官方Expo文档(仅提供英文版)。常量
| 名称 | 类型 | 描述 | 支持 |
|---|---|---|---|
Device.brand | string | null | 产品/硬件的设备品牌和消费者可见品牌。在网页和Vega上,此值始终为null。 | 不在Vega上 |
Device.designName | string | null | 工业设计的配置或名称。代表设备从制造期间的设计到批量生产的名称。在网页和Vega上,此值始终为null。 | 不在Vega上 |
Device.deviceName | string | null | 设备的人类可读名称,可以由设备的用户设置。如果设备名称在网页或Vega上不可用,则此值为null。 | 不在Vega上 |
Device.deviceType | DeviceType | null | 设备类型为DeviceType枚举值。 | ✅ 是 |
Device.deviceYearClass | number | null | 此设备的设备年龄级。在网页和Vega上,此值始终为null。 | 不在Vega上 |
Device.manufacturer | string | null | 产品或硬件的设备制造商。如果无法确定,则此字段的该值可能为null。 | ✅ 是 |
Device.modelName | string | null | 设备型号的人类易记型名称。人们使用此名称来指代设备,而不是编程模型标识符。如果无法确定,则此字段的该值可能为null。 | ✅ 是 |
Device.osBuildFingerprint | string | null | 一种唯一标识当前正在运行的系统操作系统内部版本的字符串。 | ✅ 是 |
Device.osBuildId | string | null | 操作系统的内部版本ID,用于更精确地标识操作系统版本。在网页上,此值始终为null。 | ✅ 是 |
Device.osInternalBuildId | string | null | 设备上运行的操作系统的内部版本ID。在网页上,此值始终为null。 | ✅ 是 |
Device.osName | string | null | 设备上运行的操作系统的名称。 | ✅ 是 |
Device.osVersion | string | null | 人类可读的操作系统版本字符串。版本字符串可能并不总是包含由点分隔的三个数字。 | ✅ 是 |
Device.productName | string | null | 设备实现者选择的设备的产品名称,其中包含设备的开发名称或代号。对应于Build.PRODUCT。在网页上,此值始终为null。 | ✅ 是 |
Device.supportedCpuArchitectures | string[] | null | 支持的处理器架构版本列表。设备需要其运行的二进制文件能够针对其中一种架构进行编译。如果无法确定支持的架构,例如在网页上,则此值为null。 | ✅ 是 |
Device.totalMemory | number | null | 设备的总内存,以字节为单位。内核可访问的总内存,但不一定是单个应用可以访问的总内存。设备的RAM容量,不包括内核以下的固定分配,例如DMA缓冲区或基带CPU的RAM。在网页和Vega上,此值始终为null。 | ✅ 是 |
方法
| 名称 | 类型 | 描述 | 支持 |
|---|---|---|---|
Device.getDeviceTypeAsync() | 将设备类型作为DeviceType枚举值进行检查。 | ✅ 是 | |
Device.getUptimeAsync() | 获取自设备上次重启以来的正常运行时间,以毫秒为单位。 | ✅ 是 | |
| Device.isRootedExperimentalAsync() | 检查运行Expo应用的设备是否已Root (Android) 或越狱 (iOS)。 | 不在Vega上 |
枚举
DeviceType
| 名称 | 值 | 描述 |
|---|---|---|
UNKNOWN | DeviceType.UNKNOWN = 0 | 无法识别的设备类型。 |
PHONE | DeviceType.PHONE = 1 | 移动电话手机,带有触摸屏且由一只手握持 |
TABLET | DeviceType.TABLET = 2 | 平板电脑,所配触摸屏比普通手机大 |
DESKTOP | DeviceType.DESKTOP = 3 | 台式电脑或笔记本电脑,带有键盘和鼠标 |
TV | DeviceType.TV = 4 | 带有基于电视的接口的设备。 |
错误代码
| 代码 | 描述 |
|---|---|
ERR_DEVICE_ROOT_DETECTION | 针对isRootedExperimentalAsync引发的错误代码。如果对某些系统文件没有读取访问权限,则可能会引发错误。 |
已知问题和限制
Device.deviceType和Device.getDeviceTypeAsync()- 返回UNKNOWN或TVDevice.totalMemory- returns null
支持的版本
| 程序包版本 | 基于 | @amazon-devices/react-native-kepler版本 |
|---|---|---|
| 2.0.x | 5.8.0 | 2.0.x |

