<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>
<button class='weui-btn' hover-class='other-button-hover' bindtap='sendData'>开始</button>
</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%;
}
.weui-btn {
margin-top: 128px;
width: 156rpx;
height: 156rpx;
border-radius: 50px;
padding-top: 15px;
text-align: center;
background-color: #8d1cb7;
color: #fff;
}
.other-button-hover {
background-color: #731496;
}
import bleComm from '../utils/bleComm.js';
var msg;
var lastTime = 0;//此变量用来记录上次摇动的时间
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
})
});
},
sendData: function () {
msg = !msg; //状态切换一次,用于开关灯
var x = 0,
y = 0,
z = 0,
lastX = 0,
lastY = 0,
lastZ = 0;//此组变量分别记录对应x、y、z三轴的数值和上次的数值
wx.onAccelerometerChange(function (acceleration) {
var nowTime = new Date().getTime();//记录当前时间,单位毫秒
//如果这次摇的时间距离上次摇的时间有一定间隔 才执行
if (nowTime - lastTime > 100) {
var diffTime = nowTime - lastTime;//记录时间段
lastTime = nowTime;//记录本次摇动时间,为下次计算摇动时间做准备
x = acceleration.x;//获取x轴数值
y = acceleration.y;//获取y轴数值
z = acceleration.z;//获取z轴数值
//加速度运行一定时间,即为我们想要的速度 v=at
var speed = Math.abs(x + y + z - lastX - lastY - lastZ) * 10 * 1000 / diffTime;
console.log(speed)//调试结果
lastX = x;//赋值,为下一次计算做准备
lastY = y;//赋值,为下一次计算做准备
lastZ = z;//赋值,为下一次计算做准备
if (speed > 110) {//如果计算出来的速度超过了阈值,就算作成功摇一摇
console.log(msg)
if (msg)
bleComm.writeValue('1');//蓝牙发送指令
else
bleComm.writeValue('0');//蓝牙发送指令
wx.stopAccelerometer(); //停止获取加速度
wx.vibrateLong();//手机震动
wx.showToast({ //弹框提示
title: '摇一摇OK',
icon: 'success',
duration: 500, //持续时间500毫秒
})
}
else {
wx.hideToast();//隐藏弹框
}
}
})
},
})
msg = !msg; //状态切换一次,用于开关灯
var x = 0,
y = 0,
z = 0,
lastX = 0,
lastY = 0,
lastZ = 0;//此组变量分别记录对应x、y、z三轴的数值和上次的数值
wx.onAccelerometerChange(function (acceleration) {
var nowTime = new Date().getTime();//记录当前时间,单位毫秒
//如果这次摇的时间距离上次摇的时间有一定间隔 才执行
if (nowTime - lastTime > 100) {
var diffTime = nowTime - lastTime;//记录时间段
lastTime = nowTime;//记录本次摇动时间,为下次计算摇动时间做准备
x = acceleration.x;//获取x轴数值
y = acceleration.y;//获取y轴数值
z = acceleration.z;//获取z轴数值
}
})
var speed = Math.abs(x + y + z - lastX - lastY - lastZ) * 10 * 1000 / diffTime;
console.log(speed)//调试结果
if (speed > 110) {//如果计算出来的速度超过了阈值,就算作成功摇一摇
console.log(msg)
if (msg)
bleComm.writeValue('1');//蓝牙发送指令
else
bleComm.writeValue('0');//蓝牙发送指令
wx.stopAccelerometer(); //停止获取加速度
wx.vibrateLong();//手机震动
wx.showToast({ //弹框提示
title: '摇一摇OK',
icon: 'success',
duration: 500, //持续时间500毫秒
})
}
else {
wx.hideToast();//隐藏弹框
}
onLoad: function (options) {
// wx.showLoading({
// title: '靠近连接',
// mask: true
// })
// bleComm.connectDevice().then(res => {
// wx.showToast({
// title: '蓝牙连接成功',
// icon: 'success',
// duration: 300
// })
// });
},
END
杨立斌
美科
解惑
创建时间:2017/12/22
26摇一摇