| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- <template>
- <view class="device-container">
- <text class="title-rev">数据接收 : </text>
- <button class="bt-clear" type="primary" @click="btClearTap" hover-start-time="0">清空</button>
- <checkbox-group @change="checkScroll" class="checkbox-scroll">
- <checkbox checked="true"></checkbox>
- <text>滚动</text>
- </checkbox-group>
- <view class="view-bt-send">
- <button class="bt-send" type="primary" @click="btSendTap" hover-start-time="0">发送</button>
- </view>
- </view>
- </template>
- <script>
- // #ifdef APP
- import ecUI from '@/utils/ecUI.js'
- import ecBLE from '@/utils/ecBLE/ecBLE.js'
- // #endif
- // #ifdef MP
- const ecUI = require('@/utils/ecUI.js')
- const ecBLE = require('@/utils/ecBLE/ecBLE.js')
- // #endif
- let ctx
- let isCheckScroll = true
- let isCheckRevHex = false
- let isCheckSendHex = false
- let sendData = ''
- export default {
- data() {
- return {
- textRevData: '',
- scrollIntoView: 'scroll-view-bottom',
- }
- },
- onLoad() {
- ctx = this
- isCheckScroll = true
- isCheckRevHex = false
- isCheckSendHex = false
- sendData = ''
- //on disconnect
- ecBLE.onBLEConnectionStateChange(() => {
- ecUI.showModal('提示', '设备断开连接')
- })
- //receive data
- ecBLE.onBLECharacteristicValueChange((str, strHex) => {
- let data =
- ctx.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.textRevData = data
- if (isCheckScroll) {
- if (ctx.scrollIntoView === "scroll-view-bottom") {
- ctx.scrollIntoView = "scroll-view-bottom2"
- } else {
- ctx.scrollIntoView = "scroll-view-bottom"
- }
- }
- })
- },
- onUnload() {
- ecBLE.onBLEConnectionStateChange(() => {})
- ecBLE.onBLECharacteristicValueChange(() => {})
- ecBLE.closeBLEConnection()
- },
- methods: {
- 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() {
- ctx.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) {
- var 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
- },
- }
- }
- </script>
- <style>
- .device-container {
- height: 100vh;
- position: relative;
- }
- .title-rev {
- position: absolute;
- top: 0px;
- left: 20px;
- line-height: 45px;
- font-size: 17px;
- }
- .bt-clear {
- position: absolute;
- top: 8px;
- right: 165px;
- width: 55px !important;
- height: 29px;
- font-size: 14px;
- display: flex;
- justify-content: center;
- align-items: center;
- padding: 0;
- }
- .checkbox-scroll {
- position: absolute;
- top: 0;
- right: 90px;
- height: 45px;
- font-size: 15px;
- display: flex;
- align-items: center;
- }
- .checkbox-rev-hex {
- position: absolute;
- top: 0px;
- right: 20px;
- height: 45px;
- font-size: 15px;
- display: flex;
- align-items: center;
- }
- .scroll-view-container {
- position: absolute;
- top: 45px;
- left: 20px;
- right: 20px;
- padding: 0 3px 0 5px;
- background-color: #E5E5E5;
- }
- .scroll-view-rev {
- height: 150px;
- background-color: #E5E5E5;
- }
- .view-rev-gap {
- height: 5px;
- }
- .text-rev {
- font-size: 14px;
- word-break: break-all;
- font-family: Monospace;
- }
- .title-send {
- position: absolute;
- top: 200px;
- left: 20px;
- font-size: 17px;
- line-height: 45px;
- }
- .checkbox-send-hex {
- position: absolute;
- top: 200px;
- right: 20px;
- height: 45px;
- font-size: 15px;
- display: flex;
- align-items: center;
- }
- .view-input-send {
- position: absolute;
- top: 245px;
- left: 20px;
- right: 20px;
- padding: 2px 3px;
- background-color: #E5E5E5;
- overflow-x: hidden;
- }
- .input-send {
- height: 84px;
- width: 100%;
- background-color: #E5E5E5;
- }
- .view-bt-send {
- position: absolute;
- top: 370px;
- left: 20px;
- right: 20px;
- display: flex;
- }
- .bt-send {
- flex: 1;
- height: 45px;
- display: flex;
- justify-content: center;
- align-items: center;
- }
- </style>
|