index.vue 12 KB

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