index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. try {
  121. if (devices[0].name.includes("IBP")) {
  122. console.log("IBP开头的蓝牙设备", devices[0]);
  123. that.devices = that.devices.concat(devices);
  124. }
  125. } catch (error) {}
  126. that.empty = that.devices.length == 0;
  127. });
  128. uni.startBluetoothDevicesDiscovery({
  129. // services,
  130. success(res) {
  131. that.beSearching = true;
  132. that.$refs.List.setHeight();
  133. },
  134. fail: (fail) => {
  135. that.beSearching = false;
  136. that.handleFail(fail);
  137. if (fail.errno == 10000) uni.openBluetoothAdapter();
  138. },
  139. });
  140. },
  141. /* 停止搜索 */
  142. stopBluetooth() {
  143. let that = this;
  144. uni.stopBluetoothDevicesDiscovery({
  145. success: (success) => {
  146. that.beSearching = false;
  147. },
  148. });
  149. },
  150. /* 开始连接 */
  151. createBLEConnection(item) {
  152. this.stopBluetooth();
  153. let that = this,
  154. device = {
  155. device: item,
  156. };
  157. that.connected = item.deviceId;
  158. uni.createBLEConnection({
  159. deviceId: item.deviceId,
  160. success(res) {
  161. if (res.errCode == 0) {
  162. uni.getBLEDeviceServices({
  163. deviceId: item.deviceId,
  164. success(res) {
  165. console.log("获取蓝牙服务列表", res);
  166. if (res.services.length == 0) {
  167. uni.showModal({
  168. content: "未获取到蓝牙设备服务列表",
  169. showCancel: false,
  170. confirmText: "确认",
  171. });
  172. that.closeBLEConnection();
  173. } else {
  174. let services = res.services.find(
  175. (v) => v.uuid.split("-")[0] == "0000FFE0"
  176. );
  177. try {
  178. uni.setBLEMTU({
  179. deviceId: item.deviceId,
  180. mtu: 512,
  181. success: (success) => {
  182. console.log("设置mtu512", success);
  183. },
  184. fail: (fail) => {
  185. console.log("设置mtu512fail", fail);
  186. },
  187. });
  188. } catch (error) {}
  189. if (services) {
  190. device.services = services;
  191. uni.getBLEDeviceCharacteristics({
  192. deviceId: item.deviceId,
  193. serviceId: services.uuid,
  194. success(res) {
  195. let write = false,
  196. notify = false;
  197. res.characteristics.forEach((v) => {
  198. if (v.properties.notify) {
  199. device.Ncharacteristic = v;
  200. notify = true;
  201. console.log("获取到notify" + v.uuid.split("-")[0]);
  202. } else if (res.characteristics[0].properties.write) {
  203. device.Wcharacteristic = v;
  204. write = true;
  205. console.log("获取到write" + v.uuid.split("-")[0]);
  206. }
  207. });
  208. if (write && notify) {
  209. uni.showModal({
  210. content: `${
  211. item.name || item.deviceId
  212. }连接成功\n是否进入设备操作页`,
  213. confirmText: "确认",
  214. cancelText: "重选",
  215. success: ({ confirm }) => {
  216. if (confirm) {
  217. console.log("设备连接完成");
  218. that.$Http.setBluetooth(device);
  219. } else {
  220. that.closeBLEConnection();
  221. }
  222. },
  223. });
  224. } else {
  225. uni.showModal({
  226. content: "设备未开启notify或write权限,已断开连接",
  227. showCancel: false,
  228. confirmText: "确认",
  229. });
  230. that.closeBLEConnection();
  231. }
  232. },
  233. });
  234. } else {
  235. uni.showModal({
  236. content: "未获取到蓝牙设备指定‘0000FFE0’服务",
  237. showCancel: false,
  238. confirmText: "确认",
  239. });
  240. }
  241. }
  242. },
  243. fail(err) {
  244. console.error("获取蓝牙服务失败", err);
  245. that.handleFail(fail);
  246. },
  247. });
  248. } else {
  249. console.log("设备连接失败", that.codes[res.errCode]);
  250. that.handleFail(res);
  251. }
  252. },
  253. fail: (fail) => {
  254. console.log("连接失败", fail);
  255. that.handleFail(fail);
  256. },
  257. });
  258. },
  259. /* 关闭连接 */
  260. closeBLEConnection() {
  261. let that = this;
  262. if (this.connected)
  263. uni.closeBLEConnection({
  264. deviceId: that.connected,
  265. success(res) {},
  266. });
  267. this.connected = "";
  268. },
  269. handleFail(fail) {
  270. const codes = {
  271. 0: "ok",
  272. "-1": "已连接 ",
  273. 10000: "未初始化蓝牙适配器",
  274. 10001: "当前蓝牙适配器不可用",
  275. 10002: "没有找到指定设备",
  276. 10003: "连接失败",
  277. 10004: "没有找到指定服务",
  278. 10005: "没有找到指定特征值",
  279. 10006: "当前连接已断开",
  280. 10007: "当前特征值不支持此操作",
  281. 10008: "其余所有系统上报的异常",
  282. 10009: "系统版本低于 4.3 不支持 BLE",
  283. 10010: "已连接",
  284. 10011: "配对设备需要配对码",
  285. 10012: "连接超时",
  286. 10013: "连接 deviceId 为空或者是格式不正确",
  287. };
  288. this.connected = false;
  289. uni.showModal({
  290. content: codes[fail.errCode] || fail.errMsg,
  291. showCancel: false,
  292. confirmText: "确认",
  293. });
  294. this.closeBLEConnection();
  295. },
  296. getRSSIText: function (rssi) {
  297. let text = "";
  298. if (rssi >= -70) {
  299. text = "可连接";
  300. } else if (rssi < -90) {
  301. text = "不可连接";
  302. } else {
  303. text = "信号差";
  304. }
  305. return text + `(${rssi})`;
  306. },
  307. getRSSIStyle: function (rssi) {
  308. let obj = {
  309. h: 10,
  310. BC: "#5AB73F",
  311. };
  312. if (rssi < -60 && rssi >= -69) {
  313. obj.h = 8;
  314. } else if (rssi <= -70 && rssi >= -79) {
  315. obj.h = 6;
  316. obj.BC = "#F29C37";
  317. } else if (rssi <= -80 && rssi >= -89) {
  318. obj.h = 4;
  319. obj.BC = "#EB4B5C";
  320. } else if (rssi <= -90) {
  321. obj.h = 0;
  322. }
  323. return obj;
  324. },
  325. },
  326. };
  327. </script>
  328. <style lang="scss" scoped>
  329. .head {
  330. display: flex;
  331. justify-content: space-between;
  332. align-items: center;
  333. width: 100vw;
  334. padding: 10px;
  335. box-sizing: border-box;
  336. .title {
  337. font-family: PingFang SC, PingFang SC;
  338. font-weight: 500;
  339. font-size: 15px;
  340. color: #ffffff;
  341. }
  342. .anew {
  343. font-size: 12px;
  344. color: #ffffff;
  345. opacity: 0.8;
  346. }
  347. }
  348. .device {
  349. display: flex;
  350. align-items: center;
  351. width: 355px;
  352. height: 78px;
  353. background: #ffffff;
  354. border-radius: 4px;
  355. margin: 0 auto 10px;
  356. .left {
  357. width: 61px;
  358. text-align: center;
  359. color: #333333;
  360. font-size: 18px;
  361. }
  362. .content {
  363. width: 168px;
  364. .name {
  365. font-family: PingFang SC, PingFang SC;
  366. font-weight: bold;
  367. font-size: 12px;
  368. color: #333333;
  369. }
  370. .rssi {
  371. display: flex;
  372. align-items: center;
  373. height: 17px;
  374. margin-top: 4px;
  375. .text {
  376. font-family: PingFang SC, PingFang SC;
  377. font-size: 12px;
  378. color: #666666;
  379. margin-right: 18px;
  380. }
  381. .signal {
  382. display: flex;
  383. align-items: flex-end;
  384. height: 12px;
  385. .cell {
  386. width: 3px;
  387. border-radius: 1px;
  388. margin-right: 1px;
  389. }
  390. }
  391. }
  392. }
  393. .but {
  394. display: flex;
  395. align-items: center;
  396. justify-content: center;
  397. width: 56px;
  398. height: 24px;
  399. background: #0b3f7e;
  400. border-radius: 4px;
  401. font-family: PingFang SC, PingFang SC;
  402. font-weight: 500;
  403. font-size: 12px;
  404. color: #ffffff;
  405. margin-right: 20px;
  406. }
  407. }
  408. .empty {
  409. text-align: center;
  410. color: #fff;
  411. padding-top: 30px;
  412. .title {
  413. font-weight: 700;
  414. margin-top: 40px;
  415. }
  416. .row {
  417. margin-top: 14px;
  418. font-size: 12px;
  419. }
  420. }
  421. </style>