微信小程序屏幕亮度、陀螺儀、系統方向、通話、掃碼、震動的實例


微信小程序屏幕亮度、陀螺儀、系統方向、通話、掃碼、震動的實例

由於這些實例需要使用手機的硬件,所以無法用電腦錄屏演示。

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);
        },
      })
    },


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM