微信小程序屏幕亮度、陀螺仪、系统方向、通话、扫码、震动的实例

微信小程序屏幕亮度、陀螺仪、系统方向、通话、扫码、震动的实例

由于这些实例需要使用手机的硬件,所以无法用电脑录屏演示。

wxml

<button bindtap="screenBrightness">屏幕亮度</button>
<button bindtap="setScreenBrightness">设置屏幕亮度</button>
<button bindtap="gyroscope">陀螺仪</button>
<button bindtap="deviceMotion">系统方向</button>
<button bindtap="makePhone">打电话</button>
<button bindtap="scanCode">扫码</button>
<button bindtap="vibrate">震动</button>

js

下面是js中绑定事件

screenBrightness:function(){
      wx.getScreenBrightness({
        success: (option) => {
          console.log(option);
          //0最暗,1最亮
        },
      })
    },
    setscreenBrightness:function(){
      wx.setScreenBrightness({
        value: 0,
      })
    },
    gyroscope:function(){
      wx.startGyroscope({
        interval: "normal",
        success(){
          wx.onGyroscopeChange((result) => {
            console.log("x:"+result.x);
            console.log("y:"+result.y);
            console.log("z:"+result.z);
          })
        }
      })
    },
    deviceMotion:function(){
      wx.startDeviceMotionListening({
        interval: "normal",
        success(){
          wx.onDeviceMotionChange((result) => {
            console.log(result.beta);
            //80左右听筒向上,-80左右听筒向下,正向趋近0听筒向右,负向趋近0听筒向左
          })
        }
      })
    },
    makePhone:function(){
      wx.makePhoneCall({
        phoneNumber: '123456789',
      })
    },
    scanCode:function(){
      wx.scanCode({
        onlyFromCamera: false,
        success(res){
          console.log(res);
        }
      })
    },
    vibrate:function(){
      wx.vibrateLong({
        success: (res) => {
          console.log(res);
        },
      })
    },
posted @ 2021-02-03 20:01  五仁小奶牛  阅读(1257)  评论(0编辑  收藏  举报