index.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. const blueid = uni.getStorageSync('blueid');
  43. if(blueid){
  44. this.listViewTap(blueid);
  45. }else{
  46. this.timer = setInterval(() => {
  47. ctx.deviceListDataShow = JSON.parse(JSON.stringify(deviceListData))
  48. }, 800)
  49. }
  50. },
  51. onShow() {
  52. setTimeout(() => {
  53. ctx.openBluetoothAdapter()
  54. }, 100)
  55. },
  56. methods: {
  57. listViewTap(id){
  58. ecUI.showLoading('设备连接中')
  59. ecBLE.onBLEConnectionStateChange(res => {
  60. ecUI.hideLoading()
  61. if (res.ok) {
  62. uni.setStorageSync('blueid', id);
  63. ecBLE.stopBluetoothDevicesDiscovery()
  64. uni.navigateTo({ url: '../device/device' })
  65. } else {
  66. ecUI.showModal(
  67. '提示',
  68. '连接失败,errCode=' + res.errCode + ',errMsg=' + res.errMsg
  69. )
  70. }
  71. })
  72. ecBLE.createBLEConnection(id)
  73. },
  74. openBluetoothAdapter() {
  75. ecBLE.onBluetoothAdapterStateChange(res => {
  76. if (res.ok) {
  77. console.log('Bluetooth adapter ok')
  78. ctx.startBluetoothDevicesDiscovery()
  79. } else {
  80. ecUI.showModal(
  81. '提示',
  82. `Bluetooth adapter error | ${res.errCode} | ${res.errMsg}`,
  83. () => {
  84. }
  85. )
  86. }
  87. })
  88. ecBLE.openBluetoothAdapter()
  89. },
  90. startBluetoothDevicesDiscovery() {
  91. console.log('start search')
  92. ecBLE.onBluetoothDeviceFound(res => {
  93. // if(res.id==="EC:22:05:13:78:49")
  94. // console.log(`id:${res.id},name:${res.name},rssi:${res.rssi}`)
  95. for (const item of deviceListData) {
  96. if (item.id === res.id) {
  97. item.name = res.name
  98. item.rssi = res.rssi
  99. return
  100. }
  101. }
  102. let manufacturer = ''
  103. if (res.name.length === 11 && res.name.startsWith('@')) {
  104. manufacturer = 'eciot'
  105. }
  106. if (res.name.length === 15 && res.name.startsWith('BT_')) {
  107. manufacturer = 'eciot'
  108. }
  109. deviceListData.push({
  110. id: res.id,
  111. name: res.name,
  112. rssi: res.rssi,
  113. manufacturer,
  114. })
  115. })
  116. ecBLE.startBluetoothDevicesDiscovery()
  117. },
  118. }
  119. }
  120. </script>
  121. <style>
  122. .main-container {
  123. height: 100vh;
  124. }
  125. .list-item {
  126. height: 57px;
  127. position: relative;
  128. }
  129. .list-item-hover {
  130. background-color: #e5e4e9;
  131. }
  132. .list-item-img {
  133. position: absolute;
  134. width: 36px;
  135. height: 36px;
  136. left: 20px;
  137. top: 10px;
  138. }
  139. .list-item-name {
  140. position: absolute;
  141. font-size: 22px;
  142. left: 76px;
  143. top: 0px;
  144. line-height: 56px;
  145. }
  146. .list-item-rssi-img {
  147. position: absolute;
  148. width: 20px;
  149. height: 20px;
  150. right: 20px;
  151. top: 13px;
  152. }
  153. .list-item-rssi {
  154. position: absolute;
  155. width: 40px;
  156. height: 20px;
  157. right: 10px;
  158. top: 33px;
  159. font-size: 12px;
  160. font-weight: bold;
  161. display: flex;
  162. justify-content: center;
  163. }
  164. .list-item-line {
  165. position: absolute;
  166. height: 1px;
  167. width: 100vw;
  168. left: 20px;
  169. top: 56px;
  170. background-color: #c6c6c8;
  171. }
  172. .notice {
  173. display: flex;
  174. justify-content: center;
  175. align-items: center;
  176. margin-top: 10px;
  177. font-size: 13px;
  178. color: #909399;
  179. }
  180. .gap {
  181. height: 57px;
  182. }
  183. </style>