index.vue 7.6 KB

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