0
点赞
收藏
分享

微信扫一扫

uni-app 设备相关

1. 系统信息  uni.getSystemInfo(OBJECT)

获取系统信息

OBJECT 参数说明:

参数名

类型

必填

说明

success

Function


接口调用成功的回调

fail

Function


接口调用失败的回调函数

complete

Function


接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数

说明

平台支持

brand

手机品牌

微信小程序

model

手机型号

 

pixelRatio

设备像素比

 

screenWidth

屏幕宽度

 

screenHeight

屏幕高德

 

windowWidth

可使用窗口宽度

 

 

uni.getSystemInfo({
success(res) {
console.log(res);
}
});

2.网络状态   uni.getNetworkType(OBJECT)

获取网络类型

OBJECT参数:

参数名

类型

必填

说明

success

Function


接口调用成功,返回网络类型networkType

fail

Function


接口调用失败的回调函数

complete

Function


接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数

说明

networkType

网络类型

networkType有效值

说明

wifi

WiFi网络

2g

2g网络

3g

3g网络

4g

4g网络

none

无网络

unknow

Android下不厂件的网络类型

//获取网络信息
uni.getNetworkType({
success(res) {
this.networkType=res.networkType;
}
});

3. 网络监听  uni.onNetWorkStatusChange(CALLBACK)

监听网络状态变化

CALLBACK返回参数:

参数

类型

说明

isConnected

Boolean

当前是否有网络连接

networkType

String

网络类型

//监听网络状态
uni.onNetworkStatusChange(function(res){
console.log(res);
_self.isConnected=res.isConnected;
res.networkType;
})

4. 加速度  uni.onAccelerometerChange(CALLBACK)

监听加速度数据,频率:5次/秒,接口调用后会自动开始监听,可使用uni.stopAccelerometer停止监听。(可用于步数、移动统计等开发)

CALLBACK返回参数:

参数

类型

说明

x

Number

X轴

y

Number

Y轴

z

Number

X轴

5.罗盘  uni.onCompassChange(CALLBACK)

监听罗盘数据,频率:5次/秒,接口调用后会自动开始监听,可使用uni.stopCompass停止监听。

CALLBACK返回参数:

参数

类型

说明

direction

Number

面对方向度数

//监听罗盘
uni.onCompassChange(function(res){
console.log(res.direction);
});

6. 拨打电话   uni.makePhoneCall(OBJECT)

拨打电话

OBJECT 参数说明:

参数名

类型

必填

说明

phoneNumber

String


需要拨打的电话号码

success

Function


接口调用成功的回调函数

fail

Function


接口调用失败的回调函数

complete

Function


接口调用结束的回调函数(调用成功、失败都会执行)

//拨打电话
uni.makePhoneCall({
phoneNumber:'114'
})

7.扫码  uni.scanCode(OBJECT)

调起客户端扫码界面,扫码成功后返回对应的结果。

OBJECT参数说明:

参数名

类型

必填

说明

onlyFromCamera

Boolean


是否只能从相机扫码,不允许从相册选择图片

scanType

Array


扫码类型,参数类型是数组,二维码是'qrCode',一维码是'barCode','DataMatrix'是'datamatrix',pdf417是'pdf417'。

success

Function


接口调用成功的回调函数

fail

Function


接口调用失败的回调函数

complete

Function


接口调用结束的回调函数(调用成功、失败都会执行)

success 返回参数说明:

参数

说明

平台差异说明

result

所扫码的内容

 

scanType

所扫码的类型

App、微信小程序、百度小程序、QQ小程序

charSet

所扫码的字符集

App、微信小程序、百度小程序、QQ小程序

path

当所扫的码为当前应用的合法二维码时,会返回此字段,内容为二维码携带的 path。

App、微信小程序、百度小程序、QQ小程序

//扫码
scanCode:function(){
uni.scanCode({
success: function(res){
console.log(JSON.stringify(res));
console.log('条码类型:' + res.scanType);
console.log('条码内容:' + res.result);
}
})
}

8. 剪切板 

uni.setClipboardData(OBJECT)

设置系统剪贴版的内容。

OBJECT 参数说明:

参数名

类型

必填

说明

data

String


需要设置的内容

success

Function


接口调用成功的回调

fail

Function


接口调用失败的回调函数

complete

Function


接口调用结束的回调函数(调用成功、失败都会执行)

uni.setClipboardData({
data: 'hello',
success: function () {
console.log('success');
}
});

uni.getClipboardData(OBJECT)

获取系统剪贴板内容。

OBJECT 参数说明:

参数名

类型

必填

说明

success

Function


接口调用成功的回调

fail

Function


接口调用失败的回调函数

complete

Function


接口调用结束的回调函数(调用成功、失败都会执行)

success 返回参数说明:

参数

类型

说明

data

String

剪贴板的内容

uni.getClipboardData({
success: function (res) {
console.log(res.data);
}
});

9.屏幕亮度

uni.setScreenBrightness(OBJECT)

设置屏幕亮度。

平台差异说明

App

H5

微信小程序

支付宝小程序

百度小程序

字节跳动小程序

QQ小程序


x




x


OBJECT 参数说明:

参数名

类型

必填

说明

value

Number


屏幕亮度值,范围 0~1,0 最暗,1 最亮

success

Function


接口调用成功的回调

fail

Function


接口调用失败的回调函数

complete

Function


接口调用结束的回调函数(调用成功、失败都会执行)

uni.setScreenBrightness({
value: 0.5,
success: function () {
console.log('success');
}
});

uni.getScreenBrightness(OBJECT)

获取屏幕亮度

平台差异说明

App

H5

微信小程序

支付宝小程序

百度小程序

字节跳动小程序

QQ小程序


x




x


OBJECT 参数说明:

参数名

类型

必填

说明

success

Function


接口调用成功的回调

fail

Function


接口调用失败的回调函数

complete

Function


接口调用结束的回调函数(调用成功、失败都会执行)

success 返回参数说明

参数

类型

说明

value

Number

屏幕亮度值,范围 0~1,0 最暗,1 最亮。

uni.getScreenBrightness({
success: function (res) {
console.log('屏幕亮度值:' + res.value);
}
});

uni.setKeepScreenOn(OBJECT)

设置是否保持常量状态。仅在当前应用生效,离开应用后设置失效。

平台差异说明

App

H5

微信小程序

支付宝小程序

百度小程序

字节跳动小程序

QQ小程序


x






OBJECT 参数说明:

参数名

类型

必填

说明

keepScreenOn

Boolean


是否保持屏幕常亮

success

Function


接口调用成功的回调

fail

Function


接口调用失败的回调函数

complete

Function


接口调用结束的回调函数(调用成功、失败都会执行)

success 返回参数说明:

参数

类型

说明

errMsg

String

调用结果

// 保持屏幕常亮
uni.setKeepScreenOn({
keepScreenOn: true
});

10.振动

uni.vibrate(OBJECT)

使手机发生振动。

OBJECT 参数说明:

参数名

类型

必填

说明

success

Function


接口调用成功的回调

fail

Function


接口调用失败的回调函数

complete

Function


接口调用结束的回调函数(调用成功、失败都会执行)

uni.vibrate({
success: function () {
console.log('success');
}
});

uni.vibrateLong(OBJECT)

使手机发生长时间的振动(400ms)

OBJECT 参数说明

参数名

类型

必填

说明

success

Function


接口调用成功的回调

fail

Function


接口调用失败的回调函数

complete

Function


接口调用结束的回调函数(调用成功、失败都会执行)

uni.vibrateLong({
success: function () {
console.log('success');
}
});

uni.vibrateShort(OBJECT)

使手机发生较短时间的振动(15ms)

OBJECT 参数说明:

参数名

类型

必填

说明

success

Function


接口调用成功的回调

fail

Function


接口调用失败的回调函数

complete

Function


接口调用结束的回调函数(调用成功、失败都会执行)

uni.vibrateShort({
success: function () {
console.log('success');
}
});

注意

  • iOS上只有长震动,没有短震动
  • iOS上需要手机设置“打开响铃时震动”或“静音时震动”,否则无法震动
  • vibrate只适用于钉钉小程序、支付宝小程序

11.手机联系人

uni.addPhoneContact(OBJECT)

调用后,用户可以选择将该表单以“新增联系人”或“添加到已有联系人”的方式(APP端目前没有选择步骤,将直接写入),写入手机系统通讯录,完成手机通讯录联系人和联系方式的增加。

App平台提供了更多通讯录相关API,包括读取联系人,详见:​​https://www.html5plus.org/doc/zh_cn/contacts.html​​

平台差异说明

App

H5

微信小程序

支付宝小程序

百度小程序

字节跳动小程序

QQ小程序


x




x

x

OBJECT 参数说明:

参数名

类型

必填

说明

photoFilePath

String


头像本地文件路径

nickName

String


昵称

lastName

String


姓氏

middleName

String


中间名

firstName

String


名字

remark

String


备注

mobilePhoneNumber

String


手机号

weChatNumber

String


微信号

addressCountry

String


联系地址国家

addressState

String


联系地址省份

addressCity

String


联系地址城市

addressStreet

String


联系地址街道

addressPostalCode

String


联系地址邮政编码

organization

String


公司

title

String


职位

workFaxNumber

String


工作传真

workPhoneNumber

String


工作电话

hostNumber

String


公司电话

email

String


电子邮件

url

String


网站

workAddressCountry

String


工作地址国家

workAddressState

String


工作地址省份

workAddressCity

String


工作地址城市

workAddressStreet

String


工作地址街道

workAddressPostalCode

String


工作地址邮政编码

homeFaxNumber

String


住宅传真

homePhoneNumber

String


住宅电话

homeAddressCountry

String


住宅地址国家

homeAddressState

String


住宅地址省份

homeAddressCity

String


住宅地址城市

homeAddressStreet

String


住宅地址街道

homeAddressPostalCode

String


住宅地址邮政编码

success

Function


接口调用成功的回调

fail

Function


接口调用失败的回调函数

complete

Function


接口调用结束的回调函数(调用成功、失败都会执行)

回调结果

回调类型

errMsg

说明

success

ok

添加成功

cancel

fail cancel

用户取消操作

fail

fail ${detail}

调用失败,detail 加上详细信息。

uni.addPhoneContact({
nickName: '昵称',
lastName: '姓',
firstName: '名',
remark: '备注',
mobilePhoneNumber: '114',
weChatNumber: 'wx123',
success: function () {
console.log('success');
},
fail: function () {
console.log('fail');
}
});

 



举报

相关推荐

0 条评论