屏幕
小程序还提供了一些api来操作屏幕的参数,主要是屏幕的亮度
官方描述
https://developers.weixin.qq.com/miniprogram/dev/api/device/screen/wx.setScreenBrightness.html
<view>
<button bindtap="screen">屏幕亮度</button>
<button bindtap="setScreen">设置屏幕亮度</button>
</view>
screen:function(){
wx.getScreenBrightness({
success(res){
console.log(res)
}
})
},
setScreen:function(){
wx.setScreenBrightness({
value: 1,
success(){
console.log("设置成功")
}
})
},
真机调试结果如下
陀螺仪
官方描述
https://developers.weixin.qq.com/miniprogram/dev/api/device/gyroscope/wx.stopGyroscope.html
陀螺仪和加速器有所不同,它是用来测量角速度相关的参数的
<view>
<button bindtap="gyroscope">陀螺仪</button>
</view>
gyroscope:function(){
// 打开陀螺仪
wx.startGyroscope({
success(){
console.log("陀螺仪已打开")
wx.onGyroscopeChange((res) => {
console.log("X:"+res.x)
console.log("Y:"+res.y)
console.log("Z:"+res.z)
})
},
})
},
真机调试结果如下
设备方向
小程序还提供了api可以让我们知道设备的方向,或者说方位
官方描述
https://developers.weixin.qq.com/miniprogram/dev/api/device/motion/wx.stopDeviceMotionListening.html
<view>
<button bindtap="deviceMotion">系统方向设定</button>
</view>
deviceMotion:function(){
wx.startDeviceMotionListening({
interval:'normal',
success:function(){
console.log("开启设备方向监控");
wx.onDeviceMotionChange((result) => {
console.log(result)
})
}
})
},
真机调试结果如下
拨打电话
就是拨打电话的api,同样是借助微信实现的调用设备底层的api
官方描述
https://developers.weixin.qq.com/miniprogram/dev/api/device/phone/wx.makePhoneCall.html
<view>
<button bindtap="makePhone">拨打电话</button>
</view>
makePhone:function(){
wx.makePhoneCall({
phoneNumber: '18942935836'
})
},
扫码
官方描述
https://developers.weixin.qq.com/miniprogram/dev/api/device/scan/wx.scanCode.html
<view>
<button bindtap="scanCode">扫码</button>
</view>
/**
* 扫码
*/
scanCode:function(){
wx.scanCode({
onlyFromCamera:false,
success(res){
console.log(res)
}
})
},
真机调试结果
振动
调用api实现手机的振动
官方描述
https://developers.weixin.qq.com/miniprogram/dev/api/device/vibrate/wx.vibrateShort.html
<view>
<button bindtap="vibrate">振动</button>
</view>
/**
* 振动
*/
vibrate:function(){
wx.vibrateLong({
success: (res) => {
console.log(res)
},
})
},
振动演示如下
来源:CSDN
作者:ww0peo
链接:https://blog.csdn.net/qq_35262405/article/details/104065775