device.vue 5.9 KB

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