初始化
This commit is contained in:
149
pages/device/device.js
Normal file
149
pages/device/device.js
Normal file
@@ -0,0 +1,149 @@
|
||||
// pages/device.js
|
||||
const ecUI = require('../../utils/ecUI.js')
|
||||
const ecBLE = require('../../utils/ecBLE.js')
|
||||
|
||||
let ctx
|
||||
let isCheckScroll = true
|
||||
let isCheckRevHex = false
|
||||
let isCheckSendHex = false
|
||||
let sendData = ''
|
||||
|
||||
Page({
|
||||
/**
|
||||
* 页面的初始数据
|
||||
*/
|
||||
data: {
|
||||
textRevData: '',
|
||||
scrollIntoView: 'scroll-view-bottom',
|
||||
},
|
||||
/**
|
||||
* 生命周期函数--监听页面加载
|
||||
*/
|
||||
onLoad() {
|
||||
ctx = this
|
||||
isCheckScroll = true
|
||||
isCheckRevHex = false
|
||||
isCheckSendHex = false
|
||||
sendData = ''
|
||||
ecBLE.setChineseType(ecBLE.ECBLEChineseTypeGBK)
|
||||
|
||||
//on disconnect
|
||||
ecBLE.onBLEConnectionStateChange(() => {
|
||||
ecUI.showModal('提示', '设备断开连接')
|
||||
})
|
||||
//receive data
|
||||
ecBLE.onBLECharacteristicValueChange((str, strHex) => {
|
||||
let data =
|
||||
ctx.data.textRevData +
|
||||
ctx.dateFormat('[hh:mm:ss,S]:', new Date()) +
|
||||
(isCheckRevHex ? strHex.replace(/[0-9a-fA-F]{2}/g, ' $&') : str) +
|
||||
'\r\n'
|
||||
// console.log(data)
|
||||
ctx.setData({ textRevData: data })
|
||||
if (isCheckScroll) {
|
||||
if (ctx.data.scrollIntoView === "scroll-view-bottom") {
|
||||
ctx.setData({ scrollIntoView: "scroll-view-bottom2" })
|
||||
} else {
|
||||
ctx.setData({ scrollIntoView: "scroll-view-bottom" })
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
/**
|
||||
* 生命周期函数--监听页面卸载
|
||||
*/
|
||||
onUnload() {
|
||||
ecBLE.onBLEConnectionStateChange(() => { })
|
||||
ecBLE.onBLECharacteristicValueChange(() => { })
|
||||
ecBLE.closeBLEConnection()
|
||||
},
|
||||
checkScroll(e) {
|
||||
if (e.detail.value.length) isCheckScroll = true
|
||||
else isCheckScroll = false
|
||||
},
|
||||
checkRevHex(e) {
|
||||
if (e.detail.value.length) isCheckRevHex = true
|
||||
else isCheckRevHex = false
|
||||
},
|
||||
checkSendHex(e) {
|
||||
if (e.detail.value.length) isCheckSendHex = true
|
||||
else isCheckSendHex = false
|
||||
},
|
||||
inputSendData(e) {
|
||||
sendData = e.detail.value
|
||||
},
|
||||
btClearTap() {
|
||||
this.setData({ textRevData: '' })
|
||||
},
|
||||
btSendTap() {
|
||||
if (isCheckSendHex) {
|
||||
let data = sendData
|
||||
.replace(/\s*/g, '')
|
||||
.replace(/\n/g, '')
|
||||
.replace(/\r/g, '')
|
||||
if (data.length === 0) {
|
||||
ecUI.showModal('提示', '请输入要发送的数据')
|
||||
return
|
||||
}
|
||||
if (data.length % 2 != 0) {
|
||||
ecUI.showModal('提示', '数据长度只能是双数')
|
||||
return
|
||||
}
|
||||
if (data.length > 488) {
|
||||
ecUI.showModal('提示', '最多只能发送244字节')
|
||||
return
|
||||
}
|
||||
if (!new RegExp('^[0-9a-fA-F]*$').test(data)) {
|
||||
ecUI.showModal('提示', '数据格式错误,只能是0-9,a-f,A-F')
|
||||
return
|
||||
}
|
||||
ecBLE.writeBLECharacteristicValue(data, true)
|
||||
} else {
|
||||
if (sendData.length === 0) {
|
||||
ecUI.showModal('提示', '请输入要发送的数据')
|
||||
return
|
||||
}
|
||||
let tempSendData = sendData.replace(/\n/g, '\r\n')
|
||||
if (tempSendData.length > 244) {
|
||||
ecUI.showModal('提示', '最多只能发送244字节')
|
||||
return
|
||||
}
|
||||
ecBLE.writeBLECharacteristicValue(tempSendData, false)
|
||||
}
|
||||
},
|
||||
dateFormat(fmt, date) {
|
||||
let o = {
|
||||
'M+': date.getMonth() + 1, //月份
|
||||
'd+': date.getDate(), //日
|
||||
'h+': date.getHours(), //小时
|
||||
'm+': date.getMinutes(), //分
|
||||
's+': date.getSeconds(), //秒
|
||||
'q+': Math.floor((date.getMonth() + 3) / 3), //季度
|
||||
S: date.getMilliseconds(), //毫秒
|
||||
}
|
||||
if (/(y+)/.test(fmt))
|
||||
fmt = fmt.replace(
|
||||
RegExp.$1,
|
||||
(date.getFullYear() + '').substr(4 - RegExp.$1.length)
|
||||
)
|
||||
for (var k in o)
|
||||
if (new RegExp('(' + k + ')').test(fmt)) {
|
||||
// console.log(RegExp.$1.length)
|
||||
// console.log(o[k])
|
||||
fmt = fmt.replace(
|
||||
RegExp.$1,
|
||||
RegExp.$1.length == 1
|
||||
? (o[k] + '').padStart(3, '0')
|
||||
: ('00' + o[k]).substr(('' + o[k]).length)
|
||||
)
|
||||
}
|
||||
return fmt
|
||||
},
|
||||
checkChinese(e){
|
||||
if(e.detail.value==='gbk'){
|
||||
ecBLE.setChineseType(ecBLE.ECBLEChineseTypeGBK)
|
||||
}else{
|
||||
ecBLE.setChineseType(ecBLE.ECBLEChineseTypeUTF8)
|
||||
}
|
||||
}
|
||||
})
|
||||
5
pages/device/device.json
Normal file
5
pages/device/device.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"usingComponents": {},
|
||||
"enablePullDownRefresh": false,
|
||||
"disableScroll": true
|
||||
}
|
||||
47
pages/device/device.wxml
Normal file
47
pages/device/device.wxml
Normal file
@@ -0,0 +1,47 @@
|
||||
<!--pages/device.wxml-->
|
||||
<view class="device-container">
|
||||
<view class="control-rev">
|
||||
<text class="title-rev">数据接收 : </text>
|
||||
<button class="bt-clear" type="primary" bindtap="btClearTap" hover-start-time="0">清空</button>
|
||||
<checkbox-group bindchange="checkScroll" class="checkbox-scroll">
|
||||
<checkbox checked="true"></checkbox>
|
||||
<text>滚动</text>
|
||||
</checkbox-group>
|
||||
<checkbox-group bindchange="checkRevHex" class="checkbox-rev-hex">
|
||||
<checkbox></checkbox>
|
||||
<text>Hex</text>
|
||||
</checkbox-group>
|
||||
</view>
|
||||
<view class="scroll-view-container">
|
||||
<scroll-view class="scroll-view-rev" scroll-y="true" scroll-into-view="{{scrollIntoView}}">
|
||||
<view class="view-rev-gap"></view>
|
||||
<text class="text-rev" user-select="true" selectable="true">{{textRevData}}</text>
|
||||
<view class="view-rev-gap"></view>
|
||||
<view id="scroll-view-bottom"></view>
|
||||
<view id="scroll-view-bottom2"></view>
|
||||
</scroll-view>
|
||||
</view>
|
||||
<view class="control-send">
|
||||
<text class="title-send">数据发送 : </text>
|
||||
<checkbox-group bindchange="checkSendHex" class="checkbox-send-hex">
|
||||
<checkbox></checkbox>
|
||||
<text>Hex</text>
|
||||
</checkbox-group>
|
||||
</view>
|
||||
<view class="view-input-send">
|
||||
<textarea class="input-send" maxlength="-1" bindblur="inputSendData" bindinput="inputSendData" />
|
||||
</view>
|
||||
<view class="view-bt-send">
|
||||
<button class="bt-send" type="primary" bindtap="btSendTap" hover-start-time="0">发送</button>
|
||||
</view>
|
||||
<view class="control-chinese">
|
||||
<text class="title-chinese">中文字符集 : </text>
|
||||
<radio-group bindchange="checkChinese" class="checkbox-chinese">
|
||||
<radio value="utf8" />
|
||||
<text class="checkbox-chinese-first-item">UTF-8</text>
|
||||
<radio value="gbk" checked="true"/>
|
||||
<text>GBK</text>
|
||||
</radio-group>
|
||||
</view>
|
||||
<view><text class="text-support">技术支持:https://eciot.com</text></view>
|
||||
</view>
|
||||
130
pages/device/device.wxss
Normal file
130
pages/device/device.wxss
Normal file
@@ -0,0 +1,130 @@
|
||||
/* pages/device.wxss */
|
||||
.device-container {
|
||||
height: 100vh;
|
||||
}
|
||||
.control-rev{
|
||||
height: 45px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 0 20px;
|
||||
}
|
||||
.title-rev {
|
||||
font-size: 17px;
|
||||
flex:1;
|
||||
}
|
||||
|
||||
.bt-clear {
|
||||
width: 55px !important;
|
||||
height: 29px;
|
||||
font-size: 14px;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
padding: 0;
|
||||
margin-right: 15px !important;
|
||||
}
|
||||
|
||||
.checkbox-scroll {
|
||||
font-size: 15px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
margin-right: 10px;
|
||||
}
|
||||
|
||||
.checkbox-rev-hex {
|
||||
font-size: 15px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.scroll-view-container {
|
||||
margin: 0 20px;
|
||||
padding: 0 3px 0 5px;
|
||||
background-color: #F5F5F5;
|
||||
}
|
||||
|
||||
.scroll-view-rev {
|
||||
height: 150px;
|
||||
}
|
||||
|
||||
.view-rev-gap{
|
||||
height: 5px;
|
||||
}
|
||||
|
||||
.text-rev {
|
||||
font-size: 14px;
|
||||
word-break: break-all;
|
||||
font-family: Monospace;
|
||||
}
|
||||
|
||||
.control-send{
|
||||
height: 45px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 0 20px;
|
||||
margin-top: 5px;
|
||||
}
|
||||
|
||||
.title-send {
|
||||
font-size: 17px;
|
||||
flex:1;
|
||||
}
|
||||
|
||||
.checkbox-send-hex {
|
||||
font-size: 15px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.view-input-send {
|
||||
margin: 0 20px;
|
||||
padding: 2px 3px;
|
||||
background-color: #F5F5F5;
|
||||
}
|
||||
|
||||
.input-send {
|
||||
height: 84px;
|
||||
width: 100%;
|
||||
word-break: break-all;
|
||||
}
|
||||
|
||||
.view-bt-send {
|
||||
margin: 15px 20px 0px 20px;
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.bt-send {
|
||||
flex: 1;
|
||||
height: 45px;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.control-chinese{
|
||||
height: 45px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 0 20px;
|
||||
margin-top: 5px;
|
||||
}
|
||||
|
||||
.title-chinese{
|
||||
font-size: 17px;
|
||||
flex:1;
|
||||
}
|
||||
|
||||
.checkbox-chinese{
|
||||
font-size: 15px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
.checkbox-chinese-first-item{
|
||||
margin-right: 10px;
|
||||
}
|
||||
|
||||
.text-support{
|
||||
font-size: 14px;
|
||||
color: #909399;
|
||||
margin-left: 20px;
|
||||
}
|
||||
Reference in New Issue
Block a user