bluetooth.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. <template>
  2. <!--
  3. 1.手机蓝牙打开
  4. 2.微信也许访问蓝牙和位置信息
  5. 3.重启蓝牙设备
  6. 4.不行的时候,删掉手机中蓝牙,重新链接
  7. -->
  8. <view class="content">
  9. <button size="mini" type="primary" @tap="startBluetoothDeviceDiscovery">搜索周边设备</button>
  10. <button size="mini" type="warn" @tap="stopBluetoothDevicesDiscovery">停止搜索</button>
  11. <button type="primary" @tap="labelTest">测试打印</button>
  12. <scroll-view class="device_list" scroll-y="true" show-scrollbar="true">
  13. <radio-group>
  14. <!-- v-if="item.name.length>0" -->
  15. <view v-for="(item,index) in devicesList" :key="index" class="device_item">
  16. <view style="font-size: 32rpx; color: #333;">
  17. <radio :value="item.deviceId" @tap="select_deviceId(item)" />{{ item.name }}
  18. </view>
  19. <view style="font-size: 20rpx">信号强度: {{ item.RSSI }}dBm ({{ Math.max(100+item.RSSI,0) }}%)</view>
  20. <view style="font-size: 20rpx">deviceId: {{ item.deviceId }}</view>
  21. <view style="font-size: 20rpx">Service数量: {{ item.advertisServiceUUIDs.length || 0 }}</view>
  22. <radio-group v-if="deviceId===item.deviceId">
  23. <view
  24. v-for="(service,service_index) in serviceList"
  25. :key="service_index"
  26. style="font-size: 20rpx"
  27. >
  28. <radio style="transform:scale(0.7)" :value="service.uuid" @tap="select_service(service)" />
  29. {{ service.uuid }}
  30. </view>
  31. </radio-group>
  32. </view>
  33. </radio-group>
  34. </scroll-view>
  35. <canvas canvas-id="shareCanvas" style="width: 240px; height: 240px;" />
  36. </view>
  37. </template>
  38. <script>
  39. // import PrinterJobs from '@/unit/print/printerjobs.js'
  40. // import printerUtil from '@/unit/print/printerutil.js'
  41. // import util from '@/unit/print/util.js'
  42. // import drawQrcode from '@/unit/print/weapp.qrcode.esm.js'
  43. import Bluetooth from '@/unit/print/bluetooth.js'
  44. import tsc from '@/unit/CHITEN_SDK_APP/tsc.js'
  45. // import encode from '@/unit/CHITEN_SDK_APP/encoding.js'
  46. const bluetooth = new Bluetooth()
  47. export default {
  48. components: {},
  49. data() {
  50. return {
  51. isOpenBle: false, // 是否已经打开蓝牙,默认为false
  52. devicesList: [], // 设备列表
  53. serviceList: [], // 服务列表
  54. deviceId: '', // 选中的deviceId
  55. data: {
  56. looptime: 0,
  57. currentTime: 1,
  58. lastData: 0,
  59. oneTimeData: 0,
  60. buffSize: [],
  61. buffIndex: 0,
  62. printNum: [],
  63. printNumIndex: 0,
  64. printerNum: 1,
  65. currentPrint: 1,
  66. isLabelSend: false,
  67. isQuery: false
  68. }
  69. }
  70. },
  71. // 页面卸载是关闭蓝牙链接
  72. onUnload() {
  73. bluetooth.closeBLEConnection()
  74. bluetooth.closeBluetoothAdapter()
  75. },
  76. // 页面打开,获取位置,打开蓝牙链接
  77. onLoad() {
  78. this.initBlueUnit()
  79. uni.getLocation({
  80. type: 'wgs84',
  81. success: function(res) {
  82. console.log('当前位置的经度:' + res.longitude)
  83. console.log('当前位置的纬度:' + res.latitude)
  84. }
  85. })
  86. bluetooth.openBluetoothAdapter()
  87. },
  88. methods: {
  89. initBlueUnit: function() {
  90. this.data = {
  91. looptime: 0,
  92. currentTime: 1,
  93. lastData: 0,
  94. oneTimeData: 0,
  95. buffSize: [],
  96. buffIndex: 0,
  97. printNum: [],
  98. printNumIndex: 0,
  99. printerNum: 1,
  100. currentPrint: 1,
  101. isLabelSend: false,
  102. isQuery: false
  103. }
  104. var list = []
  105. var numList = []
  106. var j = 0
  107. for (let i = 10; i < 200; i += 10) {
  108. list[j] = i
  109. j++
  110. }
  111. for (let i = 1; i < 10; i++) {
  112. numList[i - 1] = i
  113. }
  114. this.data.buffSize = list // [10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190]
  115. this.data.oneTimeData = list[1] // 20
  116. this.data.printNum = numList // [1,2,3,4,5,6,7,8,9]
  117. this.data.printerNum = numList[0] // 1
  118. },
  119. Send: async function(buff) {
  120. console.log(bluetooth)
  121. const _this = this
  122. var currentTime = this.data.currentTime
  123. var loopTime = this.data.looptime
  124. var lastData = this.data.lastData
  125. var onTimeData = this.data.oneTimeData
  126. var printNum = this.data.printerNum
  127. var currentPrint = this.data.currentPrint
  128. var buf
  129. var dataView
  130. if (currentTime < loopTime) {
  131. buf = new ArrayBuffer(onTimeData)
  132. dataView = new DataView(buf)
  133. for (let i = 0; i < onTimeData; ++i) {
  134. dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
  135. }
  136. } else {
  137. buf = new ArrayBuffer(lastData)
  138. dataView = new DataView(buf)
  139. for (let i = 0; i < lastData; ++i) {
  140. dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
  141. }
  142. }
  143. console.log(bluetooth.deviceId,
  144. bluetooth.serviceId,
  145. bluetooth.writeId)
  146. await plus.bluetooth.writeBLECharacteristicValue({
  147. deviceId: bluetooth.deviceId,
  148. serviceId: bluetooth.serviceId,
  149. characteristicId: bluetooth.writeId,
  150. value: buf,
  151. success: function(res) {
  152. if (currentPrint === printNum) {
  153. wx.showToast({
  154. title: '已打印第' + currentPrint + '张成功'
  155. })
  156. }
  157. },
  158. fail: function(e) {
  159. console.log(e)
  160. wx.showToast({
  161. title: '打印第' + currentPrint + '张失败',
  162. icon: 'none'
  163. })
  164. },
  165. complete: function() {
  166. currentTime++
  167. if (currentTime <= loopTime) {
  168. // console.log(_this.data.currentTime, currentTime);
  169. _this.data.currentTime = currentTime
  170. _this.Send(buff)
  171. } else {
  172. if (currentPrint === printNum) {
  173. _this.data.looptime = 0
  174. _this.data.lastData = 0
  175. _this.data.currentTime = 1
  176. _this.data.isLabelSend = false
  177. _this.data.currentPrint = 0
  178. } else {
  179. currentPrint++
  180. _this.data.currentPrint = currentPrint
  181. _this.data.currentTime = 1
  182. _this.Send(buff)
  183. }
  184. }
  185. }
  186. })
  187. },
  188. prepareSend: function(buff) {
  189. var time = this.data.oneTimeData
  190. var looptime = parseInt(buff.length / time)
  191. var lastData = parseInt(buff.length % time)
  192. this.data.looptime = looptime + 1
  193. this.data.lastData = lastData
  194. this.data.currentTime = 1
  195. this.Send(buff)
  196. },
  197. labelTest: function() {
  198. this.initBlueUnit()
  199. var command = tsc.dlabelPrinter.createNew()
  200. // command.setSize(40, 30);
  201. // 此指令可将标签纸向前推送至下一张标签纸的起点开始打印
  202. // command.setHome();
  203. // 向上拉回指定长度
  204. command.setBackFeed(6)
  205. command.setSize(80, 40)
  206. // 清除数据缓存
  207. command.setCls()
  208. // 设置两张标签之间的间隙距离
  209. command.setGap(8)
  210. // 设置方框
  211. command.setBox(0, 0, 635, 315, 1)
  212. command.setBar(0, 40, 640, 2)
  213. command.setBar(0, 80, 640, 2)
  214. command.setBar(0, 120, 640, 2)
  215. command.setBar(0, 160, 640, 2)
  216. command.setBar(0, 200, 366, 2)
  217. command.setBar(0, 240, 366, 2)
  218. command.setBar(0, 280, 640, 2)
  219. // command.setBar(0, 320, 366, 2);
  220. // 竖线-1
  221. command.setBar(115, 0, 2, 40)
  222. command.setBar(115, 40, 2, 40)
  223. command.setBar(115, 80, 2, 40)
  224. command.setBar(115, 120, 2, 40)
  225. command.setBar(115, 160, 2, 40)
  226. command.setBar(115, 200, 2, 40)
  227. command.setBar(115, 240, 2, 40)
  228. command.setBar(115, 280, 2, 40)
  229. // 竖线-2
  230. command.setBar(360, 40, 2, 40)
  231. command.setBar(360, 80, 2, 40)
  232. command.setBar(360, 120, 2, 40)
  233. command.setBar(360, 160, 2, 40)
  234. command.setBar(360, 200, 2, 40)
  235. command.setBar(360, 240, 2, 40)
  236. command.setBar(360, 280, 2, 40)
  237. // 竖线-3
  238. command.setBar(475, 40, 2, 40)
  239. command.setBar(475, 80, 2, 40)
  240. command.setBar(475, 120, 2, 40)
  241. command.setBar(475, 280, 2, 40)
  242. /*
  243. // 文字
  244. command.setText(10, 6, "TSS24.BF2", 0, 1, 1, "物料名称")
  245. command.setText(126, 6, "TSS24.BF2", 0, 1, 1, "0.053mm透明保护膜8-15g(XD-P51Y)鑫道")
  246. // row2
  247. command.setText(10, 46, "TSS24.BF2", 0, 1, 1, "物料编码")
  248. command.setText(126, 46, "TSS24.BF2", 0, 1, 1, "3221000229")
  249. command.setText(366, 46, "TSS24.BF2", 0, 1, 1, "供应商")
  250. command.setText(481, 46, "TSS24.BF2", 0, 1, 1, "深圳华强北")
  251. // row3
  252. command.setText(10, 86, "TSS24.BF2", 0, 1, 1, "物料规格")
  253. command.setText(126, 86, "TSS24.BF2", 0, 1, 1, "80mm*250mm")
  254. command.setText(366, 86, "TSS24.BF2", 0, 1, 1, "分切日期")
  255. command.setText(481, 86, "TSS24.BF2", 0, 1, 1, "2023-07-29")
  256. // row4
  257. command.setText(10, 126, "TSS24.BF2", 0, 1, 1, "Lot no.")
  258. command.setText(126, 126, "TSS24.BF2", 0, 1, 1, "W08-230628-0064")
  259. command.setText(366, 126, "TSS24.BF2", 0, 1, 1, "储位")
  260. command.setText(481, 126, "TSS24.BF2", 0, 1, 1, "L-11")
  261. // row5
  262. command.setText(10, 166, "TSS24.BF2", 0, 1, 1, "场内批次号")
  263. command.setText(126, 166, "TSS24.BF2", 0, 1, 1, "W08-230628-0064-001")
  264. // row6
  265. command.setText(10, 206, "TSS24.BF2", 0, 1, 1, "检测日期")
  266. command.setText(126, 206, "TSS24.BF2", 0, 1, 1, "2023-06-28")
  267. // row7
  268. command.setText(10, 246, "TSS24.BF2", 0, 1, 1, "存储条件")
  269. command.setText(126, 246, "TSS24.BF2", 0, 1, 1, "2-5°≤40%")
  270. // row8
  271. command.setText(10, 286, "TSS24.BF2", 0, 1, 1, "分切单号")
  272. command.setText(126, 286, "TSS24.BF2", 0, 1, 1, "230705004")
  273. command.setText(366, 286, "TSS24.BF2", 0, 1, 1, "生产日期")
  274. command.setText(481, 286, "TSS24.BF2", 0, 1, 1, "2023-06-26")
  275. */
  276. command.setQrcode(366, 166, 'L', 5, 'A', '1025849662131312')
  277. // command.setBarCode(120, 170, "128", 48, 0, 0, 2, 2, "12345678")
  278. // command.setBar(300, 80, 5, 150);
  279. command.setPagePrint()
  280. this.data.isLabelSend = true
  281. this.prepareSend(command.getData())
  282. },
  283. // 搜索周边设备
  284. startBluetoothDeviceDiscovery() {
  285. uni.showLoading({
  286. title: '蓝牙搜索中'
  287. })
  288. const self = this
  289. self.devicesList = []
  290. setTimeout(() => {
  291. plus.bluetooth.startBluetoothDevicesDiscovery({
  292. success: res => {
  293. plus.bluetooth.onBluetoothDeviceFound(devices => {
  294. // console.log("发现设备: " + JSON.stringify(devices));
  295. // 不重复,就添加到devicesList中,
  296. if (!self.devicesList.some(item => {
  297. return item.deviceId === devices.devices[0]
  298. .deviceId
  299. })) {
  300. if (devices.devices[0]?.name !== '') {
  301. self.devicesList.push(devices.devices[0])
  302. }
  303. }
  304. })
  305. },
  306. fail: res => {
  307. uni.hideLoading()
  308. // self.showToast(`搜索设备失败` + JSON.stringify(err))
  309. }
  310. })
  311. }, 200)
  312. },
  313. // 停止搜索蓝牙设备
  314. stopBluetoothDevicesDiscovery() {
  315. uni.hideLoading()
  316. bluetooth.stopBluetoothDevicesDiscovery()
  317. },
  318. // 选中设备
  319. async select_deviceId(item) {
  320. this.deviceId = item.deviceId
  321. bluetooth.deviceId = item.deviceId
  322. uni.setStorageSync('deviceId', bluetooth.deviceId)
  323. this.serviceList = []
  324. try {
  325. // 1.链接设备
  326. // const result = await bluetooth.createBLEConnection()
  327. // 2.寻找服务
  328. const result2 = await bluetooth.getBLEDeviceServices()
  329. console.log('获取服务: ' + JSON.stringify(result2))
  330. this.serviceList = result2
  331. } catch (e) {
  332. // TODO handle the exception
  333. console.log('e: ' + JSON.stringify(e))
  334. if (e.message === 'already connect') {
  335. // 2.寻找服务
  336. const result2 = await bluetooth.getBLEDeviceServices()
  337. this.serviceList = result2
  338. }
  339. }
  340. },
  341. // 选中服务
  342. async select_service(res) {
  343. bluetooth.serviceId = res.uuid
  344. uni.setStorageSync('serviceId', res.uuid)
  345. // console.log(JSON.parse(JSON.stringify(this.serviceList)));
  346. try {
  347. // const result = await bluetooth.getBLEDeviceCharacteristics()
  348. } catch (e) {
  349. // TODO handle the exception
  350. console.log('e: ' + JSON.stringify(e))
  351. }
  352. },
  353. // 打印一次
  354. pickUpOnce() {
  355. bluetooth.notifyBLECharacteristicValue()
  356. const self = this
  357. setTimeout(() => {
  358. self.writeBLECharacteristicValue()
  359. }, 500)
  360. },
  361. // 写入控制命令
  362. async writeBLECharacteristicValue() {
  363. // 打印机可识别的数据格式为8进制格式, 如下
  364. // 转换为8位无符号整型数组
  365. const buffer = new Uint8Array([
  366. 66, 65, 67, 75, 70, 69, 69, 68, 32, 54, 13, 10, 83, 73, 90, 69, 32, 54, 48, 32, 109, 109,
  367. 44, 52, 48, 32, 109, 109, 13, 10, 67, 76, 83, 13, 10, 71, 65, 80, 32, 56, 32, 109, 109, 44,
  368. 48, 32, 109, 109, 13, 10, 66, 79, 88, 32, 48, 44, 48, 44, 52, 55, 53, 44, 51, 49, 53, 44,
  369. 49, 13, 10, 66, 65, 82, 32, 48, 44, 52, 48, 44, 52, 56, 48, 44, 50, 13, 10, 66, 65, 82, 32,
  370. 48, 44, 56, 48, 44, 52, 56, 48, 44, 50, 13, 10, 66, 65, 82, 32, 48, 44, 49, 50, 48, 44, 52,
  371. 56, 48, 44, 50, 13, 10, 66, 65, 82, 32, 48, 44, 49, 54, 48, 44, 52, 56, 48, 44, 50, 13, 10,
  372. 66, 65, 82, 32, 48, 44, 50, 48, 48, 44, 51, 54, 54, 44, 50, 13, 10, 66, 65, 82, 32, 48, 44,
  373. 50, 52, 48, 44, 51, 54, 54, 44, 50, 13, 10, 66, 65, 82, 32, 48, 44, 50, 56, 48, 44, 51, 54,
  374. 54, 44, 50, 13, 10, 66, 65, 82, 32, 48, 44, 51, 50, 48, 44, 51, 54, 54, 44, 50, 13, 10, 66,
  375. 65, 82, 32, 49, 49, 53, 44, 48, 44, 50, 44, 52, 48, 13, 10, 66, 65, 82, 32, 49, 49, 53, 44,
  376. 52, 48, 44, 50, 44, 52, 48, 13, 10, 66, 65, 82, 32, 49, 49, 53, 44, 56, 48, 44, 50, 44, 52,
  377. 48, 13, 10, 66, 65, 82, 32, 49, 49, 53, 44, 49, 50, 48, 44, 50, 44, 52, 48, 13, 10, 66, 65,
  378. 82, 32, 49, 49, 53, 44, 49, 54, 48, 44, 50, 44, 52, 48, 13, 10, 66, 65, 82, 32, 49, 49, 53,
  379. 44, 50, 48, 48, 44, 50, 44, 52, 48, 13, 10, 66, 65, 82, 32, 49, 49, 53, 44, 50, 52, 48, 44,
  380. 50, 44, 52, 48, 13, 10, 66, 65, 82, 32, 49, 49, 53, 44, 50, 56, 48, 44, 50, 44, 52, 48, 13,
  381. 10, 66, 65, 82, 32, 51, 54, 48, 44, 52, 48, 44, 50, 44, 52, 48, 13, 10, 66, 65, 82, 32, 51,
  382. 54, 48, 44, 56, 48, 44, 50, 44, 52, 48, 13, 10, 66, 65, 82, 32, 51, 54, 48, 44, 49, 50, 48,
  383. 44, 50, 44, 52, 48, 13, 10, 66, 65, 82, 32, 51, 54, 48, 44, 49, 54, 48, 44, 50, 44, 52, 48,
  384. 13, 10, 66, 65, 82, 32, 51, 54, 48, 44, 50, 48, 48, 44, 50, 44, 52, 48, 13, 10, 66, 65, 82,
  385. 32, 51, 54, 48, 44, 50, 52, 48, 44, 50, 44, 52, 48, 13, 10, 66, 65, 82, 32, 51, 54, 48, 44,
  386. 50, 56, 48, 44, 50, 44, 52, 48, 13, 10, 84, 69, 88, 84, 32, 49, 48, 44, 54, 44, 34, 84, 83,
  387. 83, 50, 52, 46, 66, 70, 50, 34, 44, 48, 44, 49, 44, 49, 44, 34, 206, 239, 193, 207, 195,
  388. 251, 179, 198, 34, 13, 10, 84, 69, 88, 84, 32, 49, 50, 54, 44, 54, 44, 34, 84, 83, 83, 50,
  389. 52, 46, 66, 70, 50, 34, 44, 48, 44, 49, 44, 49, 44, 34, 48, 46, 48, 53, 51, 109, 109, 205,
  390. 184, 195, 247, 177, 163, 187, 164, 196, 164, 56, 45, 49, 53, 103, 40, 88, 68, 45, 80, 53,
  391. 49, 89, 41, 246, 206, 181, 192, 34, 13, 10, 84, 69, 88, 84, 32, 49, 48, 44, 52, 54, 44, 34,
  392. 84, 83, 83, 50, 52, 46, 66, 70, 50, 34, 44, 48, 44, 49, 44, 49, 44, 34, 206, 239, 193, 207,
  393. 177, 224, 194, 235, 34, 13, 10, 84, 69, 88, 84, 32, 49, 50, 54, 44, 52, 54, 44, 34, 84, 83,
  394. 83, 50, 52, 46, 66, 70, 50, 34, 44, 48, 44, 49, 44, 49, 44, 34, 51, 50, 50, 49, 48, 48, 48,
  395. 50, 50, 57, 34, 13, 10, 84, 69, 88, 84, 32, 51, 54, 54, 44, 52, 54, 44, 34, 84, 83, 83, 50,
  396. 52, 46, 66, 70, 50, 34, 44, 48, 44, 49, 44, 49, 44, 34, 185, 169, 211, 166, 201, 204, 34,
  397. 13, 10, 84, 69, 88, 84, 32, 49, 48, 44, 56, 54, 44, 34, 84, 83, 83, 50, 52, 46, 66, 70, 50,
  398. 34, 44, 48, 44, 49, 44, 49, 44, 34, 206, 239, 193, 207, 185, 230, 184, 241, 34, 13, 10, 84,
  399. 69, 88, 84, 32, 49, 50, 54, 44, 56, 54, 44, 34, 84, 83, 83, 50, 52, 46, 66, 70, 50, 34, 44,
  400. 48, 44, 49, 44, 49, 44, 34, 56, 48, 109, 109, 42, 50, 53, 48, 109, 109, 34, 13, 10, 84, 69,
  401. 88, 84, 32, 51, 54, 54, 44, 56, 54, 44, 34, 84, 83, 83, 50, 52, 46, 66, 70, 50, 34, 44, 48,
  402. 44, 49, 44, 49, 44, 34, 183, 214, 199, 208, 200, 213, 198, 218, 34, 13, 10, 84, 69, 88, 84,
  403. 32, 49, 48, 44, 49, 50, 54, 44, 34, 84, 83, 83, 50, 52, 46, 66, 70, 50, 34, 44, 48, 44, 49,
  404. 44, 49, 44, 34, 76, 111, 116, 32, 110, 111, 46, 34, 13, 10, 84, 69, 88, 84, 32, 49, 50, 54,
  405. 44, 49, 50, 54, 44, 34, 84, 83, 83, 50, 52, 46, 66, 70, 50, 34, 44, 48, 44, 49, 44, 49, 44,
  406. 34, 87, 48, 56, 45, 50, 51, 48, 54, 50, 56, 45, 48, 48, 54, 52, 34, 13, 10, 84, 69, 88, 84,
  407. 32, 51, 54, 54, 44, 49, 50, 54, 44, 34, 84, 83, 83, 50, 52, 46, 66, 70, 50, 34, 44, 48, 44,
  408. 49, 44, 49, 44, 34, 180, 162, 206, 187, 34, 13, 10, 84, 69, 88, 84, 32, 49, 48, 44, 49, 54,
  409. 54, 44, 34, 84, 83, 83, 50, 52, 46, 66, 70, 50, 34, 44, 48, 44, 49, 44, 49, 44, 34, 179,
  410. 161, 196, 218, 197, 250, 180, 206, 186, 197, 34, 13, 10, 84, 69, 88, 84, 32, 49, 50, 54,
  411. 44, 49, 54, 54, 44, 34, 84, 83, 83, 50, 52, 46, 66, 70, 50, 34, 44, 48, 44, 49, 44, 49, 44,
  412. 34, 87, 48, 56, 45, 50, 51, 48, 54, 50, 56, 45, 48, 48, 54, 52, 45, 48, 48, 49, 34, 13, 10,
  413. 84, 69, 88, 84, 32, 49, 48, 44, 50, 48, 54, 44, 34, 84, 83, 83, 50, 52, 46, 66, 70, 50, 34,
  414. 44, 48, 44, 49, 44, 49, 44, 34, 188, 236, 178, 226, 200, 213, 198, 218, 34, 13, 10, 84, 69,
  415. 88, 84, 32, 49, 50, 54, 44, 50, 48, 54, 44, 34, 84, 83, 83, 50, 52, 46, 66, 70, 50, 34, 44,
  416. 48, 44, 49, 44, 49, 44, 34, 50, 48, 50, 51, 45, 48, 54, 45, 50, 56, 34, 13, 10, 84, 69, 88,
  417. 84, 32, 49, 48, 44, 50, 52, 54, 44, 34, 84, 83, 83, 50, 52, 46, 66, 70, 50, 34, 44, 48, 44,
  418. 49, 44, 49, 44, 34, 180, 230, 180, 162, 204, 245, 188, 254, 34, 13, 10, 84, 69, 88, 84, 32,
  419. 49, 50, 54, 44, 50, 52, 54, 44, 34, 84, 83, 83, 50, 52, 46, 66, 70, 50, 34, 44, 48, 44, 49,
  420. 44, 49, 44, 34, 50, 45, 53, 161, 227, 161, 220, 52, 48, 37, 34, 13, 10, 84, 69, 88, 84, 32,
  421. 49, 48, 44, 50, 56, 54, 44, 34, 84, 83, 83, 50, 52, 46, 66, 70, 50, 34, 44, 48, 44, 49, 44,
  422. 49, 44, 34, 183, 214, 199, 208, 181, 165, 186, 197, 34, 13, 10, 84, 69, 88, 84, 32, 49, 50,
  423. 54, 44, 50, 56, 54, 44, 34, 84, 83, 83, 50, 52, 46, 66, 70, 50, 34, 44, 48, 44, 49, 44, 49,
  424. 44, 34, 50, 51, 48, 55, 48, 53, 48, 48, 52, 34, 13, 10, 81, 82, 67, 79, 68, 69, 32, 51, 54,
  425. 54, 44, 49, 54, 54, 44, 76, 44, 53, 44, 65, 44, 48, 44, 34, 49, 48, 50, 53, 56, 52, 57, 54,
  426. 54, 50, 49, 51, 49, 51, 49, 50, 34, 13, 10, 80, 82, 73, 78, 84, 32, 49, 44, 49, 13, 10
  427. ])
  428. this.printbuffs(buffer)
  429. },
  430. printbuffs(buffer) {
  431. // 1.并行调用多次会存在写失败的可能性
  432. // 2.建议每次写入不超过20字节
  433. // 分包处理,延时调用
  434. const maxChunk = 20
  435. const delay = 20
  436. for (let i = 0, j = 0, length = buffer.byteLength; i < length; i += maxChunk, j++) {
  437. const subPackage = buffer.slice(i, i + maxChunk <= length ? (i + maxChunk) : length)
  438. setTimeout(this.printbuff, j * delay, subPackage)
  439. }
  440. },
  441. printbuff(buffer) {
  442. bluetooth.writeBLECharacteristicValue(buffer)
  443. }
  444. }
  445. }
  446. </script>
  447. <style>
  448. .content {
  449. height: calc(100vh - 85px);
  450. margin-top: 85px;
  451. }
  452. page {
  453. color: #333;
  454. }
  455. button {
  456. margin: 10upx;
  457. }
  458. .devices_summary {
  459. margin-top: 5rpx;
  460. padding: 20rpx;
  461. font-size: 30rpx;
  462. }
  463. .device_list {
  464. margin: 5rpx 20rpx 5rpx 20rpx;
  465. border: 1rpx solid #ddd;
  466. border-radius: 10rpx;
  467. background-color: #FdFdFd;
  468. min-height: 0rpx;
  469. max-height: calc(100vh - 300px);
  470. width: 700rpx;
  471. }
  472. .device_item {
  473. border-bottom: 1rpx solid #ddd;
  474. padding: 20rpx;
  475. color: #666;
  476. }
  477. .device_item_hover {
  478. background-color: rgba(0, 0, 0, .1);
  479. }
  480. .connected_info {
  481. position: fixed;
  482. bottom: 0;
  483. width: 100%;
  484. background-color: #F0F0F0;
  485. padding: 10px;
  486. padding-bottom: 20px;
  487. margin-bottom: env(safe-area-inset-bottom);
  488. font-size: 14px;
  489. min-height: 100px;
  490. box-shadow: 0px 0px 3px 0px;
  491. }
  492. .connected_info .operation {
  493. position: absolute;
  494. display: inline-block;
  495. right: 30px;
  496. }
  497. </style>