index.vue 4.8 KB

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