devices.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <!-- 蓝牙平台限制 todo https://uniapp.dcloud.io/api/system/bluetooth?id=stopbluetoothdevicesdiscovery -->
  2. <template>
  3. <view>
  4. <uni-nav-bar id="nav-bar" status-bar="true" @clickLeft="onBack()" title="选中设备型号" color="#000000" fixed="true" :border="false">
  5. <view slot="left">
  6. <view class=" flex align-center margin-left">
  7. <image class="p-left-arrow" src="../../../static/p-left-arrow.png"></image>
  8. </view>
  9. </view>
  10. </uni-nav-bar>
  11. <view v-if="devicesList.length==0" class="text-xs text-center text-gray margin-top">
  12. <text>下拉搜索设备 {{devicesList.length}}</text>
  13. </view>
  14. <view v-else class="card-view padding-top padding-bottom">
  15. <view class="cu-list menu margin-left margin-right">
  16. <view class="cu-item" :class="true?'arrow':''" v-for="(item,index) in devicesList" :key="index">
  17. <view class="content" @tap="_onConnectDevice(item,$event)">
  18. <image :src="item.icon" mode="aspectFit"></image>
  19. <text class="text-grey">{{item.cname}}</text>
  20. </view>
  21. </view>
  22. </view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. import BLE from '@/util/util-js/BLE.js'
  28. import {
  29. mapState
  30. } from 'vuex';
  31. export default {
  32. computed: mapState(['bOpenBluetooth', 'bOpenSuccess', 'bListenAdapterStateChange', 'bListenDeviceFound']),
  33. data() {
  34. return {
  35. // 设备列表
  36. devicesList: []
  37. }
  38. },
  39. onLoad() {
  40. this.Init();
  41. // #ifdef APP-PLUS
  42. this.searchDevice();
  43. // #endif
  44. },
  45. onShow() {
  46. uni.$on('updateGetDevices', this.onGetDevices);
  47. },
  48. onHide() {
  49. uni.$off('updateGetDevices', this.onGetDevices);
  50. },
  51. methods: {
  52. Init() {
  53. //假如没有初始化蓝牙,就来后再初始化
  54. if (!this.bOpenBluetooth) {
  55. /**
  56. * 蓝牙部分操作
  57. * */
  58. //初始化蓝牙模块
  59. BLE.openBluetoothAdapter({
  60. success: (res) => {
  61. this.$store.state.bOpenSuccess = true;
  62. this.$store.state.bOpenBluetooth = true;
  63. },
  64. fail: (fail) => {
  65. //fail 返回
  66. //给蒙层提示,提示用户打开蓝牙,显示引导层
  67. // this.$store.state.bGuidePages = true;
  68. // this.guideCurrent = 2;
  69. this.$store.state.bOpenSuccess = false;
  70. this.$store.state.bOpenBluetooth = false;
  71. uni.showToast({
  72. title: '蓝牙尚未开启!',
  73. icon: 'none'
  74. })
  75. },
  76. complete: () => {
  77. //初始化模块后,开始监听手机蓝牙状态
  78. if (this.bListenAdapterStateChange) return;
  79. this.$store.state.bListenAdapterStateChange = true;
  80. uni.onBluetoothAdapterStateChange((res) => {
  81. console.log('adapterState changed, now is', res)
  82. // 手机蓝牙状态
  83. this.$store.state.bOpenBluetooth = res.available;
  84. if (this.bOpenBluetooth && !this.bOpenSuccess) {
  85. this.Init();
  86. }
  87. })
  88. }
  89. });
  90. //监听断开事件
  91. BLE.onBLEConnectionStateChange();
  92. }
  93. },
  94. onBack() {
  95. uni.navigateBack({
  96. delta: 1
  97. })
  98. },
  99. /**
  100. * 开始查找设备
  101. * */
  102. startBluetoothDeviceDiscovery() {
  103. //在页面显示的时候判断是都已经初始化完成蓝牙适配器若成功,则开始查找设备
  104. let self = this;
  105. setTimeout(function() {
  106. if (self.bOpenBluetooth) {
  107. uni.startBluetoothDevicesDiscovery({
  108. //services: ['fff0'],
  109. success: res => {
  110. if (!self.bListenDeviceFound) {
  111. self.$store.state.bListenDeviceFound = true;
  112. self.onBluetoothDeviceFound();
  113. }
  114. },
  115. fail: res => {
  116. uni.showToast({
  117. icon: "none",
  118. title: "查找设备失败!",
  119. duration: 3000
  120. })
  121. }
  122. });
  123. } else {
  124. uni.showToast({
  125. icon: "none",
  126. title: "请打开手机蓝牙!",
  127. duration: 3000
  128. })
  129. console.log("未初始化蓝牙是配饰器:" + self.bOpenBluetooth);
  130. }
  131. }, 300);
  132. },
  133. /**
  134. * 停止搜索蓝牙设备
  135. */
  136. stopBluetoothDevicesDiscovery() {
  137. uni.stopBluetoothDevicesDiscovery({
  138. success: e => {
  139. console.log('停止搜索蓝牙设备:' + e.errMsg);
  140. },
  141. fail: e => {
  142. console.log('停止搜索蓝牙设备失败,错误码:' + e.errCode);
  143. }
  144. });
  145. },
  146. /**
  147. * 发现外围设备
  148. */
  149. onBluetoothDeviceFound() {
  150. let _self = this;
  151. console.log("发现外围设备 onBluetoothDeviceFound:");
  152. uni.onBluetoothDeviceFound(devices => {
  153. /**
  154. * 获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。
  155. */
  156. console.log("onBluetoothDeviceFound:", devices);
  157. uni.$emit('updateGetDevices', devices);
  158. });
  159. },
  160. onGetDevices() {
  161. let _self = this;
  162. uni.getBluetoothDevices({
  163. success: res => {
  164. let devicesList = res.devices;
  165. console.log("搜索的设备对应的信息:", res);
  166. //中间值
  167. let _tempList = [];
  168. //在这里查找对应名称的设备
  169. for (let i = 0; i < devicesList.length; i++) {
  170. let eq = devicesList[i];
  171. //蹦床的设备名字
  172. if (eq.name.indexOf("BBC") > -1) {
  173. // eq.cname = "蹦床";
  174. // eq.ename = "Trampoline";
  175. // eq.icon = "/static/trampoline1.png";
  176. // eq.mIcon = "/static/trampoline.png";
  177. // let E = 2.718281828459045;
  178. // let _dis = Math.pow(E, (Math.abs(eq.RSSI) - 66.78) / 16.56);
  179. // console.log('距离:', _dis);
  180. eq.cname = "绑带蓝牙";
  181. eq.ename = "BT04";
  182. eq.icon = "/static/trampoline1.png";
  183. eq.mIcon = "/static/trampoline.png";
  184. _tempList.push(eq);
  185. } else if (eq.name.indexOf("BT04-E") > -1) {
  186. eq.cname = "绑带蓝牙";
  187. eq.ename = "BT04";
  188. eq.icon = "/static/trampoline1.png";
  189. eq.mIcon = "/static/trampoline.png";
  190. _tempList.push(eq);
  191. } else if (eq.name.indexOf("ITAG") > -1) {
  192. eq.cname = "绑带蓝牙定位";
  193. eq.ename = "ITAG";
  194. eq.icon = "/static/trampoline1.png";
  195. eq.mIcon = "/static/trampoline.png";
  196. _tempList.push(eq);
  197. } else {
  198. eq.cname = eq.name;
  199. eq.icon = "/static/logo.png";
  200. eq.mIcon = "/static/logo.png";
  201. }
  202. }
  203. if (_tempList.length != 0) {
  204. uni.showToast({
  205. icon: "none",
  206. title: "查找设备成功!",
  207. duration: 1000
  208. })
  209. _self.devicesList = [];
  210. for (let i = 0, l = _tempList.length; i < l; i++) {
  211. _self.devicesList.push(_tempList[i]);
  212. }
  213. } else {
  214. uni.showToast({
  215. icon: "none",
  216. title: "未搜索到设备!",
  217. duration: 1000
  218. })
  219. }
  220. //停止搜索设备
  221. uni.stopPullDownRefresh();
  222. _self.stopBluetoothDevicesDiscovery();
  223. },
  224. fail: e => {
  225. console.log('获取蓝牙设备错误,错误码:' + e.errCode);
  226. }
  227. });
  228. },
  229. onPullDownRefresh(e) {
  230. //假如没有初始化蓝牙,就来后再初始化
  231. console.warn("************BLE.bOpenBluetooth 初始化蓝牙适配器 == ", this.bOpenBluetooth);
  232. this.Init();
  233. this.searchDevice();
  234. },
  235. searchDevice() {
  236. this.startBluetoothDeviceDiscovery();
  237. },
  238. // 提示点击连接设备
  239. _onConnectDevice(item, event) {
  240. // console.log(item, event);
  241. if (!this.bOpenBluetooth) {
  242. uni.showToast({
  243. title: "蓝牙未初始化",
  244. icon: "none"
  245. })
  246. return;
  247. }
  248. uni.showModal({
  249. title: "连接",
  250. content: "是否连接对应设备",
  251. confirmText: '是的',
  252. success: (res) => {
  253. if (res.confirm) {
  254. if (item.deviceId) {
  255. //连接设备后,
  256. this.$store.commit('addBLEDevice', item);
  257. uni.navigateBack({
  258. delta: 1
  259. })
  260. }
  261. }
  262. }
  263. })
  264. }
  265. }
  266. }
  267. </script>
  268. <style>
  269. </style>