index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <template>
  2. <view>
  3. <cu-custom
  4. ref="custom"
  5. id="custom"
  6. bgImage="https://yostest175549.obs.cn-east-2.myhuaweicloud.com:443/202306151686796745663B52544232.png"
  7. :isBack="true"
  8. >
  9. <block slot="backText">返回</block>
  10. <block slot="content"> 搜索蓝牙设备 </block>
  11. </cu-custom>
  12. <view class="head">
  13. <view class="title">蓝牙设备列表</view>
  14. <u-loading-icon v-if="beSearching" size="21" />
  15. <view v-else class="anew" @click="startBluetooth(services)">
  16. 继续搜索
  17. </view>
  18. </view>
  19. <My_listbox ref="List" :pullDown="false">
  20. <view class="device" v-for="item in devices" :key="item.deviceId">
  21. <view class="left">
  22. <text class="iconfont icon-shuifa" />
  23. </view>
  24. <view class="content">
  25. <view class="name u-line-2">{{ item.name || item.deviceId }}</view>
  26. <view class="rssi">
  27. <view class="text">{{ getRSSIText(item.RSSI) }} </view>
  28. <view class="signal">
  29. <view
  30. class="cell"
  31. v-for="h in [4, 6, 8, 10]"
  32. :key="h"
  33. :style="{
  34. height: h + 'px',
  35. background:
  36. h &lt;= getRSSIStyle(item.RSSI).h
  37. ? getRSSIStyle(item.RSSI).BC
  38. : '#BBBBBB',
  39. }"
  40. />
  41. </view>
  42. </view>
  43. </view>
  44. <view style="flex: 1" />
  45. <view
  46. class="but"
  47. hover-class="navigator-hover"
  48. @click="connected ? '' : createBLEConnection(item)"
  49. >
  50. <u-loading-icon
  51. v-if="connected == item.deviceId"
  52. color="#FFF"
  53. inactive-color="#999"
  54. mode="circle"
  55. size="18"
  56. />
  57. <text v-else>连接</text>
  58. </view>
  59. </view>
  60. <view v-if="empty" class="empty">
  61. <u-empty />
  62. <view class="title"> 如果长时间未搜索到设备 </view>
  63. <view class="row"> 1.请检查设备是否开启蓝牙与地理位置 </view>
  64. <view class="row">
  65. 2.授权小程序蓝牙和位置想信息授权
  66. <text style="margin-left: 4px; font-weight: 700" @click="openSetting"
  67. >去设置</text
  68. >
  69. </view>
  70. <view class="row">
  71. 3.搜索全部蓝牙设备
  72. <text style="margin-left: 4px; font-weight: 700" @click="setServices"
  73. >设置</text
  74. >
  75. </view>
  76. </view>
  77. </My_listbox>
  78. </view>
  79. </template>
  80. <script>
  81. export default {
  82. components: {},
  83. name: "Bluetooth",
  84. data() {
  85. return {
  86. beSearching: false, //搜索状态
  87. connected: "", //连接状态
  88. empty: true,
  89. services: [],
  90. devices: [],
  91. };
  92. },
  93. onLoad(options) {
  94. if (options.services) {
  95. this.services = JSON.parse(options.services);
  96. this.startBluetooth(["FW01"]);
  97. } else {
  98. this.startBluetooth();
  99. }
  100. this.$refs.List.setHeight();
  101. },
  102. onUnload() {
  103. this.stopBluetooth();
  104. },
  105. methods: {
  106. setServices() {
  107. this.services = [];
  108. this.stopBluetooth();
  109. setTimeout(() => {
  110. this.startBluetooth([]);
  111. });
  112. },
  113. openSetting() {
  114. uni.openSetting();
  115. },
  116. /* 开始搜索 */
  117. startBluetooth(services = []) {
  118. let that = this;
  119. uni.onBluetoothDeviceFound(function ({ devices }) {
  120. let index = that.devices.findIndex(
  121. (v) => v.deviceId == devices[0].deviceId
  122. );
  123. if (index == -1) {
  124. that.devices = that.devices.concat(devices);
  125. } else {
  126. that.devices[index] = devices[0];
  127. }
  128. that.empty = that.devices.length == 0;
  129. });
  130. uni.startBluetoothDevicesDiscovery({
  131. // services,
  132. success(res) {
  133. that.beSearching = true;
  134. that.$refs.List.setHeight();
  135. },
  136. fail: (fail) => {
  137. that.beSearching = false;
  138. that.handleFail(fail);
  139. if (fail.errno == 10000) uni.openBluetoothAdapter();
  140. },
  141. });
  142. },
  143. /* 停止搜索 */
  144. stopBluetooth() {
  145. let that = this;
  146. uni.stopBluetoothDevicesDiscovery({
  147. success: (success) => {
  148. that.beSearching = false;
  149. },
  150. });
  151. },
  152. /* 开始连接 */
  153. createBLEConnection(item) {
  154. this.stopBluetooth();
  155. let that = this,
  156. device = {
  157. device: item,
  158. };
  159. that.connected = item.deviceId;
  160. uni.createBLEConnection({
  161. deviceId: item.deviceId,
  162. success(res) {
  163. if (res.errCode == 0) {
  164. uni.getBLEDeviceServices({
  165. deviceId: item.deviceId,
  166. success(res) {
  167. console.log("获取蓝牙服务列表", res);
  168. if (res.services.length == 0) {
  169. uni.showModal({
  170. content: "未获取到蓝牙设备服务列表",
  171. showCancel: false,
  172. confirmText: "确认",
  173. });
  174. that.connected = "";
  175. that.closeBLEConnection();
  176. } else {
  177. let services = res.services.find(
  178. (v) => v.uuid.split("-")[0] == "0000FFE0"
  179. );
  180. try {
  181. uni.setBLEMTU({
  182. deviceId: item.deviceId,
  183. mtu: 512,
  184. success: (success) => {
  185. console.log("设置mtu512", success);
  186. },
  187. fail: (fail) => {
  188. console.log("设置mtu512fail", fail);
  189. },
  190. });
  191. } catch (error) {}
  192. if (services) {
  193. device.services = services;
  194. uni.getBLEDeviceCharacteristics({
  195. deviceId: item.deviceId,
  196. serviceId: services.uuid,
  197. success(res) {
  198. let write = false,
  199. notify = false;
  200. res.characteristics.forEach((v) => {
  201. if (v.properties.notify) {
  202. device.Ncharacteristic = v;
  203. notify = true;
  204. console.log("获取到notify" + v.uuid.split("-")[0]);
  205. } else if (res.characteristics[0].properties.write) {
  206. device.Wcharacteristic = v;
  207. write = true;
  208. console.log("获取到write" + v.uuid.split("-")[0]);
  209. }
  210. });
  211. if (write && notify) {
  212. uni.showModal({
  213. content: `${
  214. item.name || item.deviceId
  215. }连接成功\n是否进入设备操作页`,
  216. confirmText: "确认",
  217. cancelText: "重选",
  218. success: ({ confirm }) => {
  219. if (confirm) {
  220. console.log("设备连接完成");
  221. that.$Http.setBluetooth(device);
  222. } else {
  223. that.closeBLEConnection();
  224. that.connected = "";
  225. }
  226. },
  227. });
  228. } else {
  229. uni.showModal({
  230. content: "设备未开启notify或write权限,已断开连接",
  231. showCancel: false,
  232. confirmText: "确认",
  233. });
  234. that.closeBLEConnection();
  235. that.connected = "";
  236. }
  237. },
  238. });
  239. } else {
  240. uni.showModal({
  241. content: "未获取到蓝牙设备指定‘0000FFE0’服务",
  242. showCancel: false,
  243. confirmText: "确认",
  244. });
  245. that.connected = "";
  246. }
  247. }
  248. },
  249. fail(err) {
  250. console.error("获取蓝牙服务失败", err);
  251. that.handleFail(fail);
  252. that.connected = "";
  253. },
  254. });
  255. } else {
  256. console.log("设备连接失败", that.codes[res.errCode]);
  257. that.handleFail(res);
  258. that.connected = "";
  259. }
  260. },
  261. fail: (fail) => {
  262. console.log("连接失败", fail);
  263. that.handleFail(fail);
  264. that.connected = "";
  265. },
  266. });
  267. },
  268. /* 关闭连接 */
  269. closeBLEConnection() {
  270. let that = this;
  271. if (this.connected)
  272. uni.closeBLEConnection({
  273. deviceId: that.connected,
  274. success(res) {},
  275. });
  276. this.connected = "";
  277. },
  278. handleFail(fail) {
  279. const codes = {
  280. 0: "ok",
  281. "-1": "已连接 ",
  282. 10000: "未初始化蓝牙适配器",
  283. 10001: "当前蓝牙适配器不可用",
  284. 10002: "没有找到指定设备",
  285. 10003: "连接失败",
  286. 10004: "没有找到指定服务",
  287. 10005: "没有找到指定特征值",
  288. 10006: "当前连接已断开",
  289. 10007: "当前特征值不支持此操作",
  290. 10008: "其余所有系统上报的异常",
  291. 10009: "系统版本低于 4.3 不支持 BLE",
  292. 10010: "已连接",
  293. 10011: "配对设备需要配对码",
  294. 10012: "连接超时",
  295. 10013: "连接 deviceId 为空或者是格式不正确",
  296. };
  297. this.connected = false;
  298. uni.showModal({
  299. content: codes[fail.errCode] || fail.errMsg,
  300. showCancel: false,
  301. confirmText: "确认",
  302. });
  303. this.closeBLEConnection();
  304. },
  305. getRSSIText: function (rssi) {
  306. let text = "";
  307. if (rssi >= -70) {
  308. text = "可连接";
  309. } else if (rssi < -90) {
  310. text = "不可连接";
  311. } else {
  312. text = "信号差";
  313. }
  314. return text + `(${rssi})`;
  315. },
  316. getRSSIStyle: function (rssi) {
  317. let obj = {
  318. h: 10,
  319. BC: "#5AB73F",
  320. };
  321. if (rssi < -60 && rssi >= -69) {
  322. obj.h = 8;
  323. } else if (rssi <= -70 && rssi >= -79) {
  324. obj.h = 6;
  325. obj.BC = "#F29C37";
  326. } else if (rssi <= -80 && rssi >= -89) {
  327. obj.h = 4;
  328. obj.BC = "#EB4B5C";
  329. } else if (rssi <= -90) {
  330. obj.h = 0;
  331. }
  332. return obj;
  333. },
  334. },
  335. };
  336. </script>
  337. <style lang="scss" scoped>
  338. .head {
  339. display: flex;
  340. justify-content: space-between;
  341. align-items: center;
  342. width: 100vw;
  343. padding: 10px;
  344. box-sizing: border-box;
  345. .title {
  346. font-family: PingFang SC, PingFang SC;
  347. font-weight: 500;
  348. font-size: 15px;
  349. color: #ffffff;
  350. }
  351. .anew {
  352. font-size: 12px;
  353. color: #ffffff;
  354. opacity: 0.8;
  355. }
  356. }
  357. .device {
  358. display: flex;
  359. align-items: center;
  360. width: 355px;
  361. height: 78px;
  362. background: #ffffff;
  363. border-radius: 4px;
  364. margin: 0 auto 10px;
  365. .left {
  366. width: 61px;
  367. text-align: center;
  368. color: #333333;
  369. font-size: 18px;
  370. }
  371. .content {
  372. width: 168px;
  373. .name {
  374. font-family: PingFang SC, PingFang SC;
  375. font-weight: bold;
  376. font-size: 12px;
  377. color: #333333;
  378. }
  379. .rssi {
  380. display: flex;
  381. align-items: center;
  382. height: 17px;
  383. margin-top: 4px;
  384. .text {
  385. font-family: PingFang SC, PingFang SC;
  386. font-size: 12px;
  387. color: #666666;
  388. margin-right: 18px;
  389. }
  390. .signal {
  391. display: flex;
  392. align-items: flex-end;
  393. height: 12px;
  394. .cell {
  395. width: 3px;
  396. border-radius: 1px;
  397. margin-right: 1px;
  398. }
  399. }
  400. }
  401. }
  402. .but {
  403. display: flex;
  404. align-items: center;
  405. justify-content: center;
  406. width: 56px;
  407. height: 24px;
  408. background: #0b3f7e;
  409. border-radius: 4px;
  410. font-family: PingFang SC, PingFang SC;
  411. font-weight: 500;
  412. font-size: 12px;
  413. color: #ffffff;
  414. margin-right: 20px;
  415. }
  416. }
  417. .empty {
  418. text-align: center;
  419. color: #fff;
  420. padding-top: 30px;
  421. .title {
  422. font-weight: 700;
  423. margin-top: 40px;
  424. }
  425. .row {
  426. margin-top: 14px;
  427. font-size: 12px;
  428. }
  429. }
  430. </style>