index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <template>
  2. <scroll-view class="main-container" scroll-y="true"
  3. :refresher-enabled="false">
  4. <view v-for="(item, index) in deviceListDataShow" :key="item.id" class="list-item" hover-class="list-item-hover"
  5. hover-start-time="0" hover-stay-time="100" @click="listViewTap(item.id)">
  6. <image v-if="item.manufacturer==='eciot'" src="/static/img/ecble.png" class="list-item-img"></image>
  7. <image v-else src="/static/img/ble.png" class="list-item-img"></image>
  8. <text class="list-item-name">{{item.name}}</text>
  9. <image v-if="item.rssi >= -41" src="/static/img/s5.png" mode="aspectFit" class="list-item-rssi-img"></image>
  10. <image v-else-if="item.rssi >= -55" src="/static/img/s4.png" mode="aspectFit" class="list-item-rssi-img"></image>
  11. <image v-else-if="item.rssi >= -65" src="/static/img/s3.png" mode="aspectFit" class="list-item-rssi-img"></image>
  12. <image v-else-if="item.rssi >= -75" src="/static/img/s2.png" mode="aspectFit" class="list-item-rssi-img"></image>
  13. <image v-else="item.rssi < -75" src="/static/img/s1.png" mode="aspectFit" class="list-item-rssi-img"></image>
  14. <text class="list-item-rssi">{{item.rssi}}</text>
  15. <view class="list-item-line"></view>
  16. </view>
  17. <view v-if="deviceListDataShow.length==0" class="notice"> - 未发现设备,请确认蓝牙是否打开 -</view>
  18. <view class="gap"></view>
  19. </scroll-view>
  20. </template>
  21. <script>
  22. // #ifdef APP
  23. import ecUI from '@/utils/ecUI.js'
  24. import ecBLE from '@/utils/ecBLE/ecBLE.js'
  25. // #endif
  26. // #ifdef MP
  27. const ecUI = require('@/utils/ecUI.js')
  28. const ecBLE = require('@/utils/ecBLE/ecBLE.js')
  29. // #endif
  30. let ctx
  31. let deviceListData = []
  32. export default {
  33. data() {
  34. return {
  35. timer:"",
  36. deviceListDataShow: []
  37. }
  38. },
  39. onLoad() {
  40. ctx = this
  41. clearInterval(this.timer);
  42. this.timer = setInterval(() => {
  43. ctx.deviceListDataShow = JSON.parse(JSON.stringify(deviceListData))
  44. }, 800)
  45. },
  46. onShow() {
  47. setTimeout(() => {
  48. ctx.openBluetoothAdapter()
  49. }, 100)
  50. },
  51. methods: {
  52. listViewTap(id){
  53. ecUI.showLoading('设备连接中')
  54. ecBLE.onBLEConnectionStateChange(res => {
  55. ecUI.hideLoading()
  56. if (res.ok) {
  57. ecBLE.stopBluetoothDevicesDiscovery()
  58. uni.navigateTo({ url: '../device/device' })
  59. } else {
  60. ecUI.showModal(
  61. '提示',
  62. '连接失败,errCode=' + res.errCode + ',errMsg=' + res.errMsg
  63. )
  64. }
  65. })
  66. ecBLE.createBLEConnection(id)
  67. },
  68. openBluetoothAdapter() {
  69. ecBLE.onBluetoothAdapterStateChange(res => {
  70. if (res.ok) {
  71. console.log('Bluetooth adapter ok')
  72. ctx.startBluetoothDevicesDiscovery()
  73. } else {
  74. ecUI.showModal(
  75. '提示',
  76. `Bluetooth adapter error | ${res.errCode} | ${res.errMsg}`,
  77. () => {
  78. }
  79. )
  80. }
  81. })
  82. ecBLE.openBluetoothAdapter()
  83. },
  84. startBluetoothDevicesDiscovery() {
  85. console.log('start search')
  86. ecBLE.onBluetoothDeviceFound(res => {
  87. // if(res.id==="EC:22:05:13:78:49")
  88. // console.log(`id:${res.id},name:${res.name},rssi:${res.rssi}`)
  89. for (const item of deviceListData) {
  90. if (item.id === res.id) {
  91. item.name = res.name
  92. item.rssi = res.rssi
  93. return
  94. }
  95. }
  96. let manufacturer = ''
  97. if (res.name.length === 11 && res.name.startsWith('@')) {
  98. manufacturer = 'eciot'
  99. }
  100. if (res.name.length === 15 && res.name.startsWith('BT_')) {
  101. manufacturer = 'eciot'
  102. }
  103. deviceListData.push({
  104. id: res.id,
  105. name: res.name,
  106. rssi: res.rssi,
  107. manufacturer,
  108. })
  109. })
  110. ecBLE.startBluetoothDevicesDiscovery()
  111. },
  112. }
  113. }
  114. </script>
  115. <style>
  116. .main-container {
  117. height: 100vh;
  118. }
  119. .list-item {
  120. height: 57px;
  121. position: relative;
  122. }
  123. .list-item-hover {
  124. background-color: #e5e4e9;
  125. }
  126. .list-item-img {
  127. position: absolute;
  128. width: 36px;
  129. height: 36px;
  130. left: 20px;
  131. top: 10px;
  132. }
  133. .list-item-name {
  134. position: absolute;
  135. font-size: 22px;
  136. left: 76px;
  137. top: 0px;
  138. line-height: 56px;
  139. }
  140. .list-item-rssi-img {
  141. position: absolute;
  142. width: 20px;
  143. height: 20px;
  144. right: 20px;
  145. top: 13px;
  146. }
  147. .list-item-rssi {
  148. position: absolute;
  149. width: 40px;
  150. height: 20px;
  151. right: 10px;
  152. top: 33px;
  153. font-size: 12px;
  154. font-weight: bold;
  155. display: flex;
  156. justify-content: center;
  157. }
  158. .list-item-line {
  159. position: absolute;
  160. height: 1px;
  161. width: 100vw;
  162. left: 20px;
  163. top: 56px;
  164. background-color: #c6c6c8;
  165. }
  166. .notice {
  167. display: flex;
  168. justify-content: center;
  169. align-items: center;
  170. margin-top: 10px;
  171. font-size: 13px;
  172. color: #909399;
  173. }
  174. .gap {
  175. height: 57px;
  176. }
  177. </style>