index.vue 12 KB

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