// pages/scanble/scanble.js
const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
loading: false,
color: '#ffffff',
background: '#25282B',
show: true,
animated: false,
bleName: '',
deviceId: '',
consoleLog: '',
services: '',
notifyId: '',
keepNotify: true, //用来判断持续通知覆盖 已编辑还未下发的值
writeId: '',
inputValue: '',
notifyValue: '',
selectTag: 0, //默认选中终端操作
operaTag: 0, //按下操作
password: '',
passwordValue: '',
distance: '',
distanceValue: 0,
current: '',
currentValue: 0.0,
controlTime: '',
controlTimeValue: 0,
autoTime: '',
autoTimeValue: 0,
title: '',
connectState: 0, //蓝牙连接状态 默认断开
commState: 0, //通讯状态
elecState: 1, //电池电量 1 正常 0 低电
// bleImg1: '../../images/icon_communication_sel.png',
// bleImg2: '../../images/icon_ abnormal_bg.png',
// elecImg1: '../../images/icon_electric_sel.png',
// elecImg2: '../../images/icon_ abnormal_bg.png',
// bleImg: '../../images/icon_communication_sel.png',
// elecImg: './../images/icon_electric_sel.png',
bleFlash: 'on', //通讯断开动画 默认开启
elecFlash: 'off', //低电动画 默认关闭
timer1: null,
timer2: null,
timer3: null,
showDialog: false,
isShowToast: false,
toastText: '操作成功',
toastImg: '',
toastImg1: '../../images/icon_check.png',
toastImg2: '../../images/icon_close.png',
showOperaDialog: false,
bleCanDo: true,
autoTip: '终端自动关机时间已到,15S后自动关机,如需阻止,可进行分合闸操作或下发配置',
autoShutState: 0, //自动关机状态 1 关机 0 未关机
elecTip: '终端电量过低,15S后自动关机,请及时充电!',
elecShutState: 0, //低电关机状态 1 关机 0 未关机
backTap: false, //用户点击返回
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
// this.scanCode()
var bleName = options.bleName
if (bleName) {
// wx.setNavigationBarTitle({
// title: bleName
// })
this.setData({
bleName: bleName
})
this.initBlue()
}
this.setStateImage()
},
/**
* 设置图片闪烁
*/
setStateImage: function () {
var that = this;
if (this.data.connectState == 0) {
this.data.timer1 = setInterval(function () {
// if (that.data.bleImg == that.data.bleImg1) {
// that.setData({
// bleImg: that.data.bleImg2
// })
// } else {
// that.setData({
// bleImg: that.data.bleImg1
// })
// }
if (that.data.bleFlash == 'off') {
that.setData({
bleFlash: 'on'
})
} else {
that.setData({
bleFlash: 'off'
})
}
}, 500)
}
if (this.data.elecState == 0) {
this.data.timer2 = setInterval(function () {
// if (that.data.elecImg == that.data.elecImg1) {
// that.setData({
// elecImg: that.data.elecImg2
// })
// } else {
// that.setData({
// elecImg: that.data.elecImg1
// })
// }
if (that.data.elecFlash == 'off') {
that.setData({
elecFlash: 'on'
})
} else {
that.setData({
elecFlash: 'off'
})
}
}, 500)
}
},
/**
* 终端操作点击
*/
homeSelect: function () {
this.setData({
selectTag: 0
})
//console.log(this.data.selectTag)
},
/**
* 终端设置点击
*/
mineSelect: function () {
this.setData({
selectTag: 1
})
//console.log(this.data.selectTag)
},
/**
* 按下
*/
handleTap1: function () {
this.setData({
operaTag: 1
})
},
/**
* 抬起
*/
handleTap2: function () {
if (this.data.commState == 0) {
app.showToast('蓝牙连接断开');
return
}
this.setData({
operaTag: 0,
showDialog: true
})
},
/**
* 操作密码
*/
passwordTap: function () {
this.setData({
title: '操作密码'
})
wx.navigateTo({
url: '/pages/setting/setting?title=' + '操作密码'
});
},
/**
* 推杆距离
*/
distanceTap: function () {
this.setData({
title: '推杆距离'
})
wx.navigateTo({
url: '/pages/setting/setting?title=' + '推杆距离'
});
},
/**
* 堵转电流
*/
currentTap: function () {
this.setData({
title: '堵转电流'
})
wx.navigateTo({
url: '/pages/setting/setting?title=' + '堵转电流'
});
},
/**
* 按压时间
*/
controlTap: function () {
this.setData({
title: '按压时间'
})
wx.navigateTo({
url: '/pages/setting/setting?title=' + '按压时间'
});
},
/**
* 自动关机时间
*/
autoTap: function () {
this.setData({
title: '自动关机时间'
})
wx.navigateTo({
url: '/pages/setting/setting?title=' + '自动关机时间'
});
},
/**
* 读取
*/
readTap: function () {
if (this.data.commState == 0) {
app.showToast('蓝牙连接断开');
return
}
this.setData({
keepNotify: true
})
this.startNotice(this.data.notifyId)
app.showToast('读取成功')
},
/**
* 下发
*/
writeTap: function () {
if (this.data.commState == 0) {
app.showToast('蓝牙连接断开');
return
}
//转换
var password = this.strToHexCharCode(this.data.passwordValue)
console.log(password)
var distance = this.int2hex(this.data.distanceValue, 8)
console.log(distance)
var current = this.SingleToHex(this.data.currentValue)
console.log(this.data.currentValue)
console.log(current)
var control = this.int2hex(this.data.controlTimeValue, 8)
console.log(control)
var auto = this.int2hex(this.data.autoTimeValue, 8)
console.log(auto)
var result = '0x1100' + password + distance + current + control + auto
this.sendMsg(result)
},
/**
* 1.0 初始化蓝牙设备
*/
initBlue: function () {
console.log('开启蓝牙');
var that = this;
wx.openBluetoothAdapter({ //调用微信小程序api 打开蓝牙适配器接口
success: function (res) {
微信小程序低功耗蓝牙
最新推荐文章于 2024-07-31 17:52:24 发布
本文详细介绍了微信小程序中如何初始化蓝牙并连接低功耗蓝牙设备,包括发送数据的完整步骤,同时提示读者可以根据自身需求定制交互界面。
摘要由CSDN通过智能技术生成