<view class='title'>
<text>mCookie-BLE摇一摇开灯</text>
<view class="line"></view>
</view>
<view class='image-H'>
<image class='userinfo-avatar' src='../bleLED/snm.jpg'></image>
</view>
<view class='wx_frame'>
<text class='wx_text'>放下再抬起触发一次</text>
</view>
.title {
color: #731496;
text-align: center;
margin-top: 20px;
}
.line {
margin-top: 20px;
width: 100%;
height: 1rpx;
background: #731496;
}
.image-H {
text-align: center;
margin-top: 110px;
}
.userinfo-avatar {
width: 166rpx;
height: 166rpx;
margin: 20rpx;
border-radius: 50%;
}
.wx_frame {
margin-top: 156px;
height: 36px;
width: 100%;
background: #fff;
border: 1px solid #dadada;
padding-top:8px;
text-align: center;
}
.wx_text {
font-size: 15px;
color: #555;
}
getAngle: function (x, y, z, dir)
getAngle(res.x, res.y, res.z, 2)
import bleComm from '../utils/bleComm.js';
import angle from '../utils/angle.js';
onLoad: function (options) {
// wx.showLoading({
// title: '靠近连接',
// mask: true
// })
// bleComm.connectDevice().then(res => {
// wx.showToast({
// title: '蓝牙连接成功',
// icon: 'success',
// duration: 300
// })
// });
var nowTime = new Date().getTime();//记录当前时间
if (nowTime - lastTime > 1000) { //时间超过1秒
wx.onAccelerometerChange(function (res) {
var angley = angle.getAngle(res.x, res.y, res.z, 2);
console.log(angley)
})
lastTime = nowTime; //记录当前时间
}
},
onLoad: function (options) {
// wx.showLoading({
// title: '靠近连接',
// mask: true
// })
// bleComm.connectDevice().then(res => {
// wx.showToast({
// title: '蓝牙连接成功',
// icon: 'success',
// duration: 300
// })
// });
var nowTime = new Date().getTime();//记录当前时间
if (nowTime - lastTime > 1000) { //时间超过1秒
wx.onAccelerometerChange(function (res) {
var angley = angle.getAngle(res.x, res.y, res.z, 2);
console.log(angley)
if (angley < -60 && !runSta) {
wx.showModal({//弹出选择框,选择不同消息发送不同指令,控制灯的开关
title: 'Welcome',
content: 'LiBin请选择!',
cancelText: '关灯',
confirmText: '开灯',
success: function (res) {
if (res.confirm) {
console.log('开灯')
bleComm.writeValue("ON");
} else if (res.cancel) {
console.log('关灯')
bleComm.writeValue("OFF");
}
}
})
wx.stopAccelerometer();
runSta = true;
}
else if (angley > -10) {
runSta = false;
wx.stopAccelerometer();
}
})
lastTime = nowTime; //记录当前时间
}
},
var lastTime = 0;//此变量用来记录上次摇动的时间
var runSta;//记录抬起、放下状态
import bleComm from '../utils/bleComm.js';
import angle from '../utils/angle.js';
var msg;
var lastTime = 0;//此变量用来记录上次摇动的时间
var runSta;
Page({
onUnload: function () {
bleComm.disConnect();
},
onHide: function () {
bleComm.disConnect();
},
onLoad: function (options) {
wx.showLoading({
title: '靠近连接',
mask: true
})
bleComm.connectDevice().then(res => {
wx.showToast({
title: '蓝牙连接成功',
icon: 'success',
duration: 300
})
});
var nowTime = new Date().getTime();//记录当前时间
if (nowTime - lastTime > 1000) { //时间超过1秒
wx.onAccelerometerChange(function (res) {
var angley = angle.getAngle(res.x, res.y, res.z, 2);
console.log(angley)
if (angley < -60 && !runSta) {
wx.showModal({
title: 'Welcome',
content: 'LiBin请选择!',
cancelText: '关灯',
confirmText: '开灯',
success: function (res) {
if (res.confirm) {
console.log('开灯')
bleComm.writeValue("1");
} else if (res.cancel) {
console.log('关灯')
bleComm.writeValue("0");
}
}
})
wx.stopAccelerometer();
runSta = true;
}
else if (angley > -10) {
runSta = false;
wx.stopAccelerometer();
}
})
lastTime = nowTime; //记录当前时间
}
},
})
END
杨立斌
美科
解惑
创建时间:2017/12/22
27抬起唤醒