device.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <template>
  2. <view class="device-container">
  3. <text class="title-rev">数据接收 : </text>
  4. <button class="bt-clear" type="primary" @click="btClearTap" hover-start-time="0">清空</button>
  5. <checkbox-group @change="checkScroll" class="checkbox-scroll">
  6. <checkbox checked="true"></checkbox>
  7. <text>滚动</text>
  8. </checkbox-group>
  9. <view class="view-bt-send">
  10. <button class="bt-send" type="primary" @click="btSendTap" hover-start-time="0">发送</button>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. // #ifdef APP
  16. import ecUI from '@/utils/ecUI.js'
  17. import ecBLE from '@/utils/ecBLE/ecBLE.js'
  18. // #endif
  19. // #ifdef MP
  20. const ecUI = require('@/utils/ecUI.js')
  21. const ecBLE = require('@/utils/ecBLE/ecBLE.js')
  22. // #endif
  23. let ctx
  24. let isCheckScroll = true
  25. let isCheckRevHex = false
  26. let isCheckSendHex = false
  27. let sendData = ''
  28. export default {
  29. data() {
  30. return {
  31. textRevData: '',
  32. scrollIntoView: 'scroll-view-bottom',
  33. }
  34. },
  35. onLoad() {
  36. ctx = this
  37. isCheckScroll = true
  38. isCheckRevHex = false
  39. isCheckSendHex = false
  40. sendData = ''
  41. //on disconnect
  42. ecBLE.onBLEConnectionStateChange(() => {
  43. ecUI.showModal('提示', '设备断开连接')
  44. })
  45. //receive data
  46. ecBLE.onBLECharacteristicValueChange((str, strHex) => {
  47. let data =
  48. ctx.textRevData +
  49. '[' +
  50. ctx.dateFormat('hh:mm:ss,S', new Date()) +
  51. ']: ' +
  52. (isCheckRevHex ? strHex.replace(/[0-9a-fA-F]{2}/g, ' $&') : str) +
  53. '\r\n'
  54. // console.log(data)
  55. ctx.textRevData = data
  56. if (isCheckScroll) {
  57. if (ctx.scrollIntoView === "scroll-view-bottom") {
  58. ctx.scrollIntoView = "scroll-view-bottom2"
  59. } else {
  60. ctx.scrollIntoView = "scroll-view-bottom"
  61. }
  62. }
  63. })
  64. },
  65. onUnload() {
  66. ecBLE.onBLEConnectionStateChange(() => {})
  67. ecBLE.onBLECharacteristicValueChange(() => {})
  68. ecBLE.closeBLEConnection()
  69. },
  70. methods: {
  71. checkScroll(e) {
  72. if (e.detail.value.length) isCheckScroll = true
  73. else isCheckScroll = false
  74. },
  75. checkRevHex(e) {
  76. if (e.detail.value.length) isCheckRevHex = true
  77. else isCheckRevHex = false
  78. },
  79. checkSendHex(e) {
  80. if (e.detail.value.length) isCheckSendHex = true
  81. else isCheckSendHex = false
  82. },
  83. inputSendData(e) {
  84. sendData = e.detail.value
  85. },
  86. btClearTap() {
  87. ctx.textRevData = ''
  88. },
  89. btSendTap() {
  90. if (isCheckSendHex) {
  91. let data = sendData
  92. .replace(/\s*/g, '')
  93. .replace(/\n/g, '')
  94. .replace(/\r/g, '')
  95. if (data.length === 0) {
  96. ecUI.showModal('提示', '请输入要发送的数据')
  97. return
  98. }
  99. if (data.length % 2 != 0) {
  100. ecUI.showModal('提示', '数据长度只能是双数')
  101. return
  102. }
  103. if (data.length > 488) {
  104. ecUI.showModal('提示', '最多只能发送244字节')
  105. return
  106. }
  107. if (!new RegExp('^[0-9a-fA-F]*$').test(data)) {
  108. ecUI.showModal('提示', '数据格式错误,只能是0-9,a-f,A-F')
  109. return
  110. }
  111. ecBLE.writeBLECharacteristicValue(data, true)
  112. } else {
  113. if (sendData.length === 0) {
  114. ecUI.showModal('提示', '请输入要发送的数据')
  115. return
  116. }
  117. let tempSendData = sendData.replace(/\n/g, '\r\n')
  118. if (tempSendData.length > 244) {
  119. ecUI.showModal('提示', '最多只能发送244字节')
  120. return
  121. }
  122. ecBLE.writeBLECharacteristicValue(tempSendData, false)
  123. }
  124. },
  125. dateFormat(fmt, date) {
  126. var o = {
  127. 'M+': date.getMonth() + 1, //月份
  128. 'd+': date.getDate(), //日
  129. 'h+': date.getHours(), //小时
  130. 'm+': date.getMinutes(), //分
  131. 's+': date.getSeconds(), //秒
  132. 'q+': Math.floor((date.getMonth() + 3) / 3), //季度
  133. S: date.getMilliseconds(), //毫秒
  134. }
  135. if (/(y+)/.test(fmt))
  136. fmt = fmt.replace(
  137. RegExp.$1,
  138. (date.getFullYear() + '').substr(4 - RegExp.$1.length)
  139. )
  140. for (var k in o)
  141. if (new RegExp('(' + k + ')').test(fmt)) {
  142. // console.log(RegExp.$1.length)
  143. // console.log(o[k])
  144. fmt = fmt.replace(
  145. RegExp.$1,
  146. RegExp.$1.length == 1 ?
  147. (o[k] + '').padStart(3, '0') :
  148. ('00' + o[k]).substr(('' + o[k]).length)
  149. )
  150. }
  151. return fmt
  152. },
  153. }
  154. }
  155. </script>
  156. <style>
  157. .device-container {
  158. height: 100vh;
  159. position: relative;
  160. }
  161. .title-rev {
  162. position: absolute;
  163. top: 0px;
  164. left: 20px;
  165. line-height: 45px;
  166. font-size: 17px;
  167. }
  168. .bt-clear {
  169. position: absolute;
  170. top: 8px;
  171. right: 165px;
  172. width: 55px !important;
  173. height: 29px;
  174. font-size: 14px;
  175. display: flex;
  176. justify-content: center;
  177. align-items: center;
  178. padding: 0;
  179. }
  180. .checkbox-scroll {
  181. position: absolute;
  182. top: 0;
  183. right: 90px;
  184. height: 45px;
  185. font-size: 15px;
  186. display: flex;
  187. align-items: center;
  188. }
  189. .checkbox-rev-hex {
  190. position: absolute;
  191. top: 0px;
  192. right: 20px;
  193. height: 45px;
  194. font-size: 15px;
  195. display: flex;
  196. align-items: center;
  197. }
  198. .scroll-view-container {
  199. position: absolute;
  200. top: 45px;
  201. left: 20px;
  202. right: 20px;
  203. padding: 0 3px 0 5px;
  204. background-color: #E5E5E5;
  205. }
  206. .scroll-view-rev {
  207. height: 150px;
  208. background-color: #E5E5E5;
  209. }
  210. .view-rev-gap {
  211. height: 5px;
  212. }
  213. .text-rev {
  214. font-size: 14px;
  215. word-break: break-all;
  216. font-family: Monospace;
  217. }
  218. .title-send {
  219. position: absolute;
  220. top: 200px;
  221. left: 20px;
  222. font-size: 17px;
  223. line-height: 45px;
  224. }
  225. .checkbox-send-hex {
  226. position: absolute;
  227. top: 200px;
  228. right: 20px;
  229. height: 45px;
  230. font-size: 15px;
  231. display: flex;
  232. align-items: center;
  233. }
  234. .view-input-send {
  235. position: absolute;
  236. top: 245px;
  237. left: 20px;
  238. right: 20px;
  239. padding: 2px 3px;
  240. background-color: #E5E5E5;
  241. overflow-x: hidden;
  242. }
  243. .input-send {
  244. height: 84px;
  245. width: 100%;
  246. background-color: #E5E5E5;
  247. }
  248. .view-bt-send {
  249. position: absolute;
  250. top: 370px;
  251. left: 20px;
  252. right: 20px;
  253. display: flex;
  254. }
  255. .bt-send {
  256. flex: 1;
  257. height: 45px;
  258. display: flex;
  259. justify-content: center;
  260. align-items: center;
  261. }
  262. </style>