index.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. <u-modal :show="showPwd" @confirm="inputPwd" @cancel="cancel" :showCancelButton="true" :title="$t('buletooth.pwdinput')" >
  18. <view class="slot-content">
  19. <u--input
  20. type="number"
  21. :placeholder="$t('buletooth.pwdtip')"
  22. border="surround"
  23. v-model="pwd"
  24. ></u--input>
  25. </view>
  26. </u-modal>
  27. <view v-if="deviceListDataShow.length==0" class="notice"> - {{ $t('buletooth.nodevice') }} -</view>
  28. <view class="gap"></view>
  29. </scroll-view>
  30. </template>
  31. <script>
  32. import {getPwd,setPwd,parsePwd,sendPortDetailCmd} from "@/utils/weitiandi/device/device.js";
  33. // #ifdef APP
  34. import ecUI from '@/utils/ecUI.js'
  35. import ecBLE from '@/utils/ecBLE/ecBLE.js'
  36. // #endif
  37. // #ifdef MP
  38. const ecUI = require('@/utils/ecUI.js')
  39. const ecBLE = require('@/utils/ecBLE/ecBLE.js')
  40. // #endif
  41. let ctx
  42. let deviceListData = []
  43. export default {
  44. data() {
  45. return {
  46. showPwd:false,
  47. rightPwd:"",
  48. pwd:"",
  49. timer:"",
  50. buleid:"",
  51. deviceListDataShow: [],
  52. showTimer:null,
  53. }
  54. },
  55. onLoad() {
  56. ecUI.showLoading(this.$t('buletooth.init'))
  57. ctx = this
  58. clearInterval(this.timer);
  59. this.timer = setInterval(() => {
  60. ctx.deviceListDataShow = JSON.parse(JSON.stringify(deviceListData))
  61. }, 800)
  62. },
  63. onUnload(){
  64. ecBLE.stopBluetoothDevicesDiscovery();
  65. ecBLE.closeBLEConnection()
  66. },
  67. onShow() {
  68. if(this.showTimer!= null){
  69. clearTimeout(this.showTimer);
  70. }
  71. this.showTimer = setTimeout(() => {
  72. ctx.openBluetoothAdapter()
  73. }, 100)
  74. },
  75. methods: {
  76. cancel(){
  77. this.showPwd = false;
  78. uni.navigateBack({
  79. });
  80. },
  81. inputPwd(){
  82. if(!this.pwd ){
  83. this.$modal.showToast(this.$t('buletooth.nopwd'));
  84. }else{
  85. if(this.rightPwd && this.rightPwd === this.pwd){
  86. this.loginSuccess();
  87. }else{
  88. this.$modal.showToast(this.$t('buletooth.errpwd'));
  89. }
  90. }
  91. },
  92. loginSuccess(){
  93. this.showPwd = false;
  94. uni.setStorageSync("pwd",this.rightPwd);
  95. uni.setStorageSync('blueid', this.buleid);
  96. this.pwd = "";
  97. ecBLE.stopBluetoothDevicesDiscovery();
  98. uni.navigateTo({
  99. url: '/pages/weitiandi/bluetooth/status'
  100. });
  101. },
  102. getLocalPwd(){
  103. let pwd = uni.getStorageSync("pwd");
  104. return pwd;
  105. },
  106. listViewTap(id){
  107. let self = this;
  108. ecUI.showLoading(this.$t('buletooth.connecting'))
  109. ecBLE.onBLEConnectionStateChange(res => {
  110. console.log(res);
  111. if (res.ok) {
  112. setTimeout(function(){
  113. ecUI.hideLoading()
  114. getPwd();
  115. self.buleid = id;
  116. },3000)
  117. // uni.setStorageSync('blueid', id);
  118. // ecBLE.stopBluetoothDevicesDiscovery();
  119. //
  120. } else {
  121. ecUI.hideLoading()
  122. uni.removeStorageSync('blueid');
  123. ecUI.showModal(
  124. this.$t('buletooth.tip'),
  125. 'error,errCode=' + res.errCode + ',errMsg=' + res.errMsg
  126. )
  127. }
  128. });
  129. //receive data
  130. ecBLE.onBLECharacteristicValueChange((str, strHex) => {
  131. console.log("数据来了")
  132. let isCheckRevHex = true;
  133. let data =
  134. (isCheckRevHex ? strHex.replace(/[0-9a-fA-F]{2}/g, ' $&') : str)
  135. // console.log(data)
  136. self.$modal.closeLoading();
  137. console.log("settting:"+data);
  138. //AA 67 0D 05 00 00 00 00 00 00 00 00 00 25 E2 00 80
  139. let pwd = parsePwd(data);
  140. let localPwd = self.getLocalPwd()
  141. console.log("pwd:"+pwd);
  142. console.log("localPwd:"+localPwd)
  143. self.rightPwd = pwd;
  144. if(pwd != localPwd){
  145. self.showInputPwd();
  146. }else{
  147. self.loginSuccess();
  148. }
  149. })
  150. ecBLE.createBLEConnection(id)
  151. },
  152. showInputPwd(){
  153. this.showPwd = true;
  154. },
  155. openBluetoothAdapter() {
  156. let self = this;
  157. ecBLE.onBluetoothAdapterStateChange(res => {
  158. ecUI.hideLoading()
  159. if (res.ok) {
  160. console.log('Bluetooth adapter ok')
  161. let blueid = uni.getStorageSync('blueid');
  162. if(blueid){
  163. self.listViewTap(blueid);
  164. }else{
  165. ctx.startBluetoothDevicesDiscovery()
  166. }
  167. } else {
  168. ecUI.showModal(
  169. this.$t('buletooth.tip'),
  170. `Bluetooth adapter error | ${res.errCode} | ${res.errMsg}`,
  171. () => {
  172. }
  173. )
  174. }
  175. })
  176. ecBLE.openBluetoothAdapter()
  177. },
  178. startBluetoothDevicesDiscovery() {
  179. console.log('start search')
  180. ecBLE.onBluetoothDeviceFound(res => {
  181. // if(res.id==="EC:22:05:13:78:49")
  182. // console.log(`id:${res.id},name:${res.name},rssi:${res.rssi}`)
  183. for (const item of deviceListData) {
  184. if (item.id === res.id) {
  185. item.name = res.name
  186. item.rssi = res.rssi
  187. return
  188. }
  189. }
  190. let manufacturer = ''
  191. if (res.name.length === 11 && res.name.startsWith('@')) {
  192. manufacturer = 'eciot'
  193. }
  194. if (res.name.length === 15 && res.name.startsWith('BT_')) {
  195. manufacturer = 'eciot'
  196. }
  197. deviceListData.push({
  198. id: res.id,
  199. name: res.name,
  200. rssi: res.rssi,
  201. manufacturer,
  202. })
  203. })
  204. ecBLE.startBluetoothDevicesDiscovery()
  205. },
  206. }
  207. }
  208. </script>
  209. <style>
  210. .main-container {
  211. height: 100vh;
  212. }
  213. .list-item {
  214. height: 57px;
  215. position: relative;
  216. }
  217. .list-item-hover {
  218. background-color: #e5e4e9;
  219. }
  220. .list-item-img {
  221. position: absolute;
  222. width: 36px;
  223. height: 36px;
  224. left: 20px;
  225. top: 10px;
  226. }
  227. .list-item-name {
  228. position: absolute;
  229. font-size: 22px;
  230. left: 76px;
  231. top: 0px;
  232. line-height: 56px;
  233. }
  234. .list-item-rssi-img {
  235. position: absolute;
  236. width: 20px;
  237. height: 20px;
  238. right: 20px;
  239. top: 13px;
  240. }
  241. .list-item-rssi {
  242. position: absolute;
  243. width: 40px;
  244. height: 20px;
  245. right: 10px;
  246. top: 33px;
  247. font-size: 12px;
  248. font-weight: bold;
  249. display: flex;
  250. justify-content: center;
  251. }
  252. .list-item-line {
  253. position: absolute;
  254. height: 1px;
  255. width: 100vw;
  256. left: 20px;
  257. top: 56px;
  258. background-color: #c6c6c8;
  259. }
  260. .notice {
  261. display: flex;
  262. justify-content: center;
  263. align-items: center;
  264. margin-top: 10px;
  265. font-size: 13px;
  266. color: #909399;
  267. }
  268. .gap {
  269. height: 57px;
  270. }
  271. </style>