跳到主要内容

设备信息

获取设备型号、系统、屏幕、电量、网络等基础信息。全部同步,不需要 await

基本信息

this.version  = Device.systemVersion()  // "17.5"
this.name = Device.name() // "iPhone 15 Pro"
this.model = Device.model() // "iPhone"
this.language = Device.language() // "zh"
this.darkMode = Device.isdarkmode() // true / false

屏幕

const s = Device.screen()
console.log(s)
// { scale: 3, width: 393, height: 852 }
字段说明
scale像素密度倍数(2x / 3x)
width / height逻辑像素的屏幕宽 / 高

电量

const b = Device.battery()
console.log(b)
// { "state": "unplugged", "level": 0.75 }
state含义
unknown未知
charging充电中
full已充满
unplugged未充电

level 是 0~1 的小数。乘 100 即百分比。

网络状态

const n = Device.network()
console.log(n)
// {
// "wifi": true,
// "cellular": false,
// "online": true,
// "vpn": false
// }
字段说明
wifi是否连 Wi-Fi
cellular是否走蜂窝
online是否联网(任一为真)
vpn是否在 VPN

连接状态(蓝牙 / 热点)

const c = Device.connectivity()
console.log(c)
// { "bluetooth": "on", "hotspot": false }
字段取值 / 类型
bluetooth"on" / "off" / "unsupported" / "unauthorized" / "resetting" / "unknown"
hotspottrue / false
蓝牙权限

读取蓝牙状态需要 App 配置了蓝牙权限。第一次会弹窗。拒绝后返回 "unauthorized"

例:做一个"网络状态"图标

const n = Device.network()
this.netIcon = n.wifi ? "wifi" : (n.cellular ? "antenna.radiowaves.left.and.right" : "wifi.slash")

icon 组件填 {netIcon},根据网络自动切换图标。