bluetoothConfig.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <template>
  2. <gui-page
  3. :custom-header="true"
  4. :is-header-sized="false"
  5. :header-class="['gui-theme-background-color', isLandscapeScreen?'width: 100vmax;':'']"
  6. :style="[isLandscapeScreen?'width: 100vmax':'']"
  7. >
  8. <template #gHeader>
  9. <view
  10. :style="[isLandscapeScreen?'height:44px;width: 100vmax':'height:44px;']"
  11. class="gui-flex gui-nowrap gui-rows gui-align-items-center"
  12. >
  13. <!-- 使用组件实现返回按钮及返回首页按钮 -->
  14. <text
  15. style="font-size:44rpx;"
  16. class="gui-header-leader-btns gui-color-white font-icons"
  17. @tap="goHome"
  18. >&#xe6c5;</text>
  19. <!-- 导航文本此处也可以是其他自定义内容 -->
  20. <text
  21. class="gui-h4 gui-blod gui-flex1 gui-text-center gui-color-white gui-ellipsis gui-primary-text"
  22. >蓝牙设置</text>
  23. <!-- 此处加一个右侧展位元素与左侧同宽,实现标题居中 -->
  24. <!-- 实际宽度请根据自己情况设置 -->
  25. <view style="width:40px;">
  26. <text
  27. style="font-size:44rpx;"
  28. class="gui-color-white font-icons"
  29. @tap="saveBluetoothConfig"
  30. >&#xe6b4;</text>
  31. </view>
  32. </view>
  33. </template>
  34. <template #gBody>
  35. <!--
  36. 1.手机蓝牙打开
  37. 2.微信也许访问蓝牙和位置信息
  38. 3.重启蓝牙设备
  39. 4.不行的时候,删掉手机中蓝牙,重新链接
  40. -->
  41. <view class="content">
  42. <button size="mini" type="primary" @tap="startBluetoothDeviceDiscovery">搜索周边设备</button>
  43. <button size="mini" type="warn" @tap="stopBluetoothDevicesDiscovery">停止搜索</button>
  44. <scroll-view class="device_list" scroll-y="true" show-scrollbar="true">
  45. <radio-group>
  46. <!-- v-if="item.name.length>0" -->
  47. <view v-for="(item,index) in devicesList" :key="index" class="device_item">
  48. <view style="font-size: 32rpx; color: #333;">
  49. <radio :value="item.deviceId" @tap="select_deviceId(item)" />{{ item.name }}
  50. </view>
  51. <view style="font-size: 20rpx">信号强度: ({{ computedDBM(Math.max(100+item.RSSI,0)) }})
  52. </view>
  53. <view style="font-size: 20rpx">deviceId: {{ item.deviceId }}</view>
  54. <view style="font-size: 20rpx">Service数量: {{ item.advertisServiceUUIDs.length || 0 }}</view>
  55. <radio-group v-if="deviceId===item.deviceId">
  56. <view
  57. v-for="(service,service_index) in serviceList"
  58. :key="service_index"
  59. style="font-size: 20rpx"
  60. >
  61. <radio
  62. style="transform:scale(0.7)"
  63. :value="service.uuid"
  64. @tap="select_service(service)"
  65. />
  66. {{ service.uuid }}
  67. </view>
  68. </radio-group>
  69. </view>
  70. </radio-group>
  71. </scroll-view>
  72. <canvas canvas-id="shareCanvas" style="width: 240px; height: 240px;" />
  73. </view>
  74. </template>
  75. </gui-page>
  76. </template>
  77. <script>
  78. import Bluetooth from '@/unit/print/bluetooth.js'
  79. import {
  80. setFileStorage
  81. } from '@/unit/fileStorage.js'
  82. const bluetooth = new Bluetooth()
  83. export default {
  84. components: {},
  85. data() {
  86. return {
  87. isOpenBle: false, // 是否已经打开蓝牙,默认为false
  88. devicesList: [], // 设备列表
  89. serviceList: [], // 服务列表
  90. deviceId: '', // 选中的deviceId
  91. canvas_width: 240,
  92. canvas_height: 240
  93. }
  94. },
  95. // 页面卸载是关闭蓝牙链接
  96. onUnload() {
  97. // bluetooth.closeBLEConnection();
  98. // bluetooth.closeBluetoothAdapter();
  99. },
  100. // 页面打开,获取位置,打开蓝牙链接
  101. onLoad() {
  102. uni.getLocation({
  103. type: 'wgs84',
  104. success: function(res) {
  105. console.log('当前位置的经度:' + res.longitude)
  106. console.log('当前位置的纬度:' + res.latitude)
  107. }
  108. })
  109. bluetooth.serviceName = ''
  110. bluetooth.openBluetoothAdapter()
  111. },
  112. computed: {
  113. computedDBM: function() {
  114. return (num) => {
  115. if (num < 33.33) {
  116. return '弱'
  117. } else if (num > 33.33 && num < 66.66) {
  118. return '中'
  119. } else if (num > 66.66 && num < 100) {
  120. return '强'
  121. }
  122. }
  123. }
  124. },
  125. methods: {
  126. // 搜索周边设备
  127. startBluetoothDeviceDiscovery() {
  128. uni.showLoading({
  129. title: '蓝牙搜索中'
  130. })
  131. const self = this
  132. self.devicesList = []
  133. setTimeout(() => {
  134. plus.bluetooth.startBluetoothDevicesDiscovery({
  135. success: res => {
  136. plus.bluetooth.onBluetoothDeviceFound(devices => {
  137. // console.log("发现设备: " + JSON.stringify(devices));
  138. if (devices.devices[0]?.name !== '') {
  139. self.devicesList.push(devices.devices[0])
  140. }
  141. // 不重复,就添加到devicesList中,
  142. // if (!self.devicesList.some(item => {
  143. // return item.deviceId === devices.devices[0]
  144. // .deviceId
  145. // })) {
  146. // if (devices.devices[0]?.name !== "") {
  147. // self.devicesList.push(devices.devices[0])
  148. // }
  149. // }
  150. })
  151. },
  152. fail: res => {
  153. uni.hideLoading()
  154. // self.showToast(`搜索设备失败` + JSON.stringify(err))
  155. }
  156. })
  157. }, 200)
  158. },
  159. // 停止搜索蓝牙设备
  160. stopBluetoothDevicesDiscovery() {
  161. uni.hideLoading()
  162. bluetooth.stopBluetoothDevicesDiscovery()
  163. },
  164. // 选中设备
  165. async select_deviceId(item) {
  166. this.deviceId = item.deviceId
  167. bluetooth.serviceName = item.name
  168. bluetooth.deviceId = item.deviceId
  169. uni.setStorageSync('deviceId', bluetooth.deviceId)
  170. this.serviceList = []
  171. try {
  172. // 1.链接设备
  173. // const result = await bluetooth.createBLEConnection()
  174. await bluetooth.createBLEConnection()
  175. // 2.寻找服务
  176. const result2 = await bluetooth.getBLEDeviceServices()
  177. if (result2?.length === 0) {
  178. uni.showToast({
  179. title: '此设备无服务',
  180. duration: 2000,
  181. icon: 'none'
  182. })
  183. }
  184. // console.log("获取服务: " + JSON.stringify(result2));
  185. this.serviceList = result2
  186. } catch (e) {
  187. // TODO handle the exception
  188. // console.log("e: " + JSON.stringify(e));
  189. if (e.message === 'already connect') {
  190. // 2.寻找服务
  191. const result2 = await bluetooth.getBLEDeviceServices()
  192. this.serviceList = result2
  193. }
  194. }
  195. },
  196. // 选中服务
  197. async select_service(res) {
  198. bluetooth.serviceId = res.uuid
  199. uni.setStorageSync('serviceId', res.uuid)
  200. // console.log(JSON.parse(JSON.stringify(this.serviceList)));
  201. try {
  202. // const result = await bluetooth.getBLEDeviceCharacteristics()
  203. await bluetooth.getBLEDeviceCharacteristics()
  204. } catch (e) {
  205. // TODO handle the exception
  206. console.log('e: ' + JSON.stringify(e))
  207. }
  208. },
  209. goHome() {
  210. uni.setStorageSync('bluetoothConfig', JSON.stringify(bluetooth))
  211. uni.$goHome()
  212. },
  213. // 保存设置
  214. saveBluetoothConfig() {
  215. // #ifdef APP-PLUS
  216. setFileStorage('bluetoothConfig.txt', 0, bluetooth)
  217. // #endif
  218. }
  219. }
  220. }
  221. </script>
  222. <style>
  223. .content {
  224. height: calc(100vh - 85px);
  225. margin-top: 85px;
  226. }
  227. page {
  228. color: #333;
  229. }
  230. button {
  231. margin: 10upx;
  232. }
  233. .gui-header-leader-btns {
  234. color: black;
  235. font-size: 24px !important;
  236. margin-left: 24rpx;
  237. }
  238. .devices_summary {
  239. margin-top: 5rpx;
  240. padding: 20rpx;
  241. font-size: 30rpx;
  242. }
  243. .device_list {
  244. margin: 5rpx 20rpx 5rpx 20rpx;
  245. border: 1rpx solid #ddd;
  246. border-radius: 10rpx;
  247. background-color: #FdFdFd;
  248. min-height: 0rpx;
  249. max-height: calc(100vh - 300px);
  250. width: 700rpx;
  251. }
  252. .device_item {
  253. border-bottom: 1rpx solid #ddd;
  254. padding: 20rpx;
  255. color: #666;
  256. }
  257. .device_item_hover {
  258. background-color: rgba(0, 0, 0, .1);
  259. }
  260. .connected_info {
  261. position: fixed;
  262. bottom: 0;
  263. width: 100%;
  264. background-color: #F0F0F0;
  265. padding: 10px;
  266. padding-bottom: 20px;
  267. margin-bottom: env(safe-area-inset-bottom);
  268. font-size: 14px;
  269. min-height: 100px;
  270. box-shadow: 0px 0px 3px 0px;
  271. }
  272. .connected_info .operation {
  273. position: absolute;
  274. display: inline-block;
  275. right: 30px;
  276. }
  277. </style>