Skip to content

设备能力 Device

Device API 提供访问设备硬件和系统信息的能力。

在线试用

getSystemInfogetNetworkTypegetLocation
短振长振scanCode

点击按钮后在此展示 JSON 或说明

设备能力 API 试用

系统信息、网络、定位、振动与扫码;振动与部分能力会在右下显示 SDK 日志

导入

ts
import woo from 'mini-sdk'
// 或
import { device } from 'mini-sdk'

设备信息

getSystemInfo

获取系统信息。

ts
const info = await woo.getSystemInfo()

返回值 GetSystemInfoResult

字段类型说明
platformstring操作系统(ios / android / simulator)
systemstring系统版本
brandstring设备品牌
modelstring设备型号
screenWidthnumber屏幕宽度 (px)
screenHeightnumber屏幕高度 (px)
windowWidthnumber可用窗口宽度
windowHeightnumber可用窗口高度
statusBarHeightnumber状态栏高度
languagestring系统语言
versionstring宿主 App 版本
SDKVersionstringSDK 版本

getDeviceId

获取设备唯一标识。

ts
const { deviceId } = await woo.getDeviceId()

getDeviceInfo

获取设备基础信息。

ts
const info = await woo.getDeviceInfo()

getAppInfo

获取当前小程序应用信息。

ts
const info = await woo.getAppInfo()
// { appId, appName, env: 'simulator' | 'production', ... }

位置

getLocation

获取当前地理位置。

ts
const location = await woo.getLocation()
console.log(location.latitude, location.longitude)

权限提示

调用 getLocation 会请求用户的位置权限。如用户拒绝,将抛出 BridgeErrorCode.PermissionDenied 错误。

振动

vibrateShort / vibrateLong

触发设备振动。

ts
await woo.vibrateShort()  // 短振(15ms)
await woo.vibrateLong()   // 长振(400ms)

网络

getNetworkType

获取当前网络类型。

ts
const { networkType } = await woo.getNetworkType()
// 'wifi' | '4g' | '3g' | '2g' | 'none' | 'unknown'

扫码

scanCode

调起扫码界面。

ts
const result = await woo.scanCode({
  scanType: ['qrCode', 'barCode'],
})
console.log(result.result) // 扫码结果
console.log(result.scanType) // 码类型

图片

chooseImage

从相册选择图片。

ts
const result = await woo.chooseImage({
  count: 3,       // 最多选择 3 张
  sourceType: ['album', 'camera'],
})
console.log(result.tempFilePaths) // 临时文件路径数组

takePhoto

拍照。

ts
const result = await woo.takePhoto({
  quality: 'high',
})
console.log(result.tempFilePath) // 照片临时路径

文件上传

uploadFile

上传文件到服务器。

ts
const result = await woo.uploadFile({
  url: 'https://api.example.com/upload',
  filePath: tempFilePath,
  name: 'file',
  header: { Authorization: 'Bearer xxx' },
  formData: { type: 'avatar' },
})

地图

openMap

打开系统地图导航。

ts
await woo.openMap({
  latitude: 39.908860,
  longitude: 116.397390,
  name: '天安门',
  address: '北京市东城区',
})

通信

makePhoneCall

拨打电话。

ts
await woo.makePhoneCall({ phoneNumber: '10086' })

屏幕

setScreenOrientation / getScreenOrientation

设置/获取屏幕方向。

ts
await woo.setScreenOrientation({ orientation: 'landscape' })

const { orientation } = await woo.getScreenOrientation()

onScreenOrientationChange

监听屏幕方向变化。

ts
woo.onScreenOrientationChange((result) => {
  console.log('屏幕方向:', result.orientation)
})

setScreenBrightness / getScreenBrightness

设置/获取屏幕亮度。

ts
await woo.setScreenBrightness({ value: 0.8 }) // 0~1

const { value } = await woo.getScreenBrightness()

keepScreenOn

保持屏幕常亮。

ts
await woo.keepScreenOn({ keepScreenOn: true })

跨应用

跳转到其他小程序。

ts
await woo.navigateToMiniApp({
  appId: 'target-app-id',
  path: '/pages/index/index',
  extraData: { from: 'my-app' },
})

openWallet

打开钱包。

ts
await woo.openWallet({ type: 'payment' })

API 速查表

方法说明签名
getSystemInfo获取系统信息() → Promise<SystemInfo>
getSystemInfoSync同步获取系统信息() → SystemInfo
getNetworkType获取当前网络类型() → Promise<{ networkType }>
onNetworkStatusChange监听网络状态变化(callback) → void
makePhoneCall拨打电话({ phoneNumber }) → Promise<void>
scanCode扫码({ scanType? }) → Promise<{ result, scanType }>
getLocation获取地理位置({ type? }) → Promise<{ latitude, longitude, ... }>
vibrateLong / vibrateShort长/短振动() → Promise<void>
getBatteryInfo获取电池信息() → Promise<{ level, isCharging }>
getScreenBrightness获取屏幕亮度() → Promise<{ value }>
setScreenBrightness设置屏幕亮度({ value }) → Promise<void>
openWallet打开钱包({ type }) → Promise<void>

MiniDev Studio — 小程序开发利器