connectingDevice.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <template>
  2. <view class="device">
  3. <view class="rssi">
  4. <view class="signal">
  5. <view
  6. class="cell"
  7. v-for="h in [4, 6, 8, 10]"
  8. :key="h"
  9. :style="{
  10. height: h + 'px',
  11. background:
  12. h &lt;= getRSSIStyle(Bluetooth.device.RSSI).h
  13. ? getRSSIStyle(Bluetooth.device.RSSI).BC
  14. : '#BBBBBB',
  15. }"
  16. />
  17. </view>
  18. </view>
  19. {{ Bluetooth.device.name || Bluetooth.device.deviceId }}
  20. </view>
  21. </template>
  22. <script>
  23. import { hexMD5 } from "../../pages/login/modules/md5";
  24. export default {
  25. props: {
  26. onFeedback: {
  27. type: Function,
  28. },
  29. },
  30. data() {
  31. return {
  32. Bluetooth: {},
  33. circulation: null,
  34. };
  35. },
  36. beforeDestroy() {
  37. clearInterval(this.circulation);
  38. },
  39. methods: {
  40. init() {
  41. let that = this;
  42. this.Bluetooth = this.$Http.Bluetooth;
  43. console.log("连接蓝牙设备详情", this.$Http.Bluetooth);
  44. uni.onBLEConnectionStateChange(function (res) {
  45. if (!res.connected && that.circulation) {
  46. uni.showModal({
  47. content: "蓝牙设备已断开连接",
  48. showCancel: false,
  49. });
  50. that.$Http.setDetailMode(0);
  51. clearInterval(that.circulation);
  52. that.circulation = null;
  53. }
  54. });
  55. this.circulation = setInterval(() => {
  56. uni.getBLEDeviceRSSI({
  57. deviceId: that.Bluetooth.device.deviceId,
  58. success: (success) => {
  59. console.log("监听信号强度", success);
  60. if (success.errCode == 0) that.Bluetooth.device.RSSI = success.RSSI;
  61. },
  62. });
  63. }, 3000);
  64. this.startNotice();
  65. },
  66. closeBLEConnection() {
  67. let that = this;
  68. uni.closeBLEConnection({
  69. deviceId: that.Bluetooth.device.deviceId,
  70. success(res) {
  71. that.$Http.setDetailMode(0);
  72. clearInterval(that.circulation);
  73. that.circulation = null;
  74. },
  75. });
  76. },
  77. /* 发送 */
  78. send(content = {}, item) {
  79. content.ts = Date.now();
  80. content.msgid = hexMD5(
  81. JSON.stringify({ w_functionid: item.w_functionid, ...msg })
  82. );
  83. item.msgid = content.msgid;
  84. item.msg = content;
  85. let msg = JSON.stringify(content);
  86. let that = this;
  87. const buffer = new ArrayBuffer(msg.length);
  88. const dataView = new DataView(buffer);
  89. for (var i = 0; i < msg.length; i++) {
  90. dataView.setUint8(i, msg.charAt(i).charCodeAt());
  91. }
  92. uni.writeBLECharacteristicValue({
  93. deviceId: that.Bluetooth.device.deviceId,
  94. serviceId: that.Bluetooth.service.uuid,
  95. characteristicId: that.Bluetooth.characteristic.uuid,
  96. value: buffer,
  97. success(res) {
  98. console.log("发送信息", res);
  99. },
  100. fail(err) {
  101. console.error("发送信息失败", err);
  102. that.handleFail(err);
  103. },
  104. });
  105. },
  106. /* 模拟接收 */
  107. fanhui(value) {
  108. let resHex = this.ab2hex(value);
  109. let result = JSON.parse(this.hexCharCodeToStr(resHex));
  110. delete result.d;
  111. result.status = 1;
  112. this.$emit("onFeedback", result);
  113. },
  114. /* 开始监听 */
  115. startNotice() {
  116. let that = this;
  117. uni.notifyBLECharacteristicValueChange({
  118. deviceId: that.Bluetooth.device.deviceId,
  119. serviceId: that.Bluetooth.service.uuid,
  120. characteristicId: that.Bluetooth.characteristic.uuid,
  121. state: true,
  122. success(res) {
  123. console.log("监听信息", res);
  124. uni.onBLECharacteristicValueChange((res) => {
  125. let resHex = that.ab2hex(res.value);
  126. let result = that.hexCharCodeToStr(resHex);
  127. console.log("Notice", result);
  128. that.$emit("onFeedback", result);
  129. });
  130. },
  131. fail(err) {
  132. console.error("监听信息失败", err);
  133. that.handleFail(err);
  134. },
  135. });
  136. },
  137. handleFail(fail) {
  138. const codes = {
  139. 0: "ok",
  140. "-1": "已连接 ",
  141. 10000: "未初始化蓝牙适配器",
  142. 10001: "当前蓝牙适配器不可用",
  143. 10002: "没有找到指定设备",
  144. 10003: "连接失败",
  145. 10004: "没有找到指定服务",
  146. 10005: "没有找到指定特征值",
  147. 10006: "当前连接已断开",
  148. 10007: "当前特征值不支持此操作",
  149. 10008: "其余所有系统上报的异常",
  150. 10009: "系统版本低于 4.3 不支持 BLE",
  151. 10010: "已连接",
  152. 10011: "配对设备需要配对码",
  153. 10012: "连接超时",
  154. 10013: "连接 deviceId 为空或者是格式不正确",
  155. };
  156. uni.showModal({
  157. content: codes[fail.errCode] || fail.errMsg,
  158. showCancel: false,
  159. confirmText: "确认",
  160. });
  161. this.$emit("onFeedback", {
  162. status: 999,
  163. msgid: 23232323,
  164. });
  165. },
  166. ab2hex(buffer) {
  167. const hexArr = Array.prototype.map.call(
  168. new Uint8Array(buffer),
  169. function (bit) {
  170. return ("00" + bit.toString(16)).slice(-2);
  171. }
  172. );
  173. return hexArr.join("");
  174. },
  175. hexCharCodeToStr(hexCharCodeStr) {
  176. var trimedStr = hexCharCodeStr.trim();
  177. var rawStr =
  178. trimedStr.substr(0, 2).toLowerCase() === "0x"
  179. ? trimedStr.substr(2)
  180. : trimedStr;
  181. var len = rawStr.length;
  182. if (len % 2 !== 0) {
  183. alert("存在非法字符!");
  184. return "";
  185. }
  186. var curCharCode;
  187. var resultStr = [];
  188. for (var i = 0; i < len; i = i + 2) {
  189. curCharCode = parseInt(rawStr.substr(i, 2), 16);
  190. resultStr.push(String.fromCharCode(curCharCode));
  191. }
  192. return resultStr.join("");
  193. },
  194. getRSSIText: function (rssi) {
  195. let text = "";
  196. if (rssi >= -70) {
  197. text = "可连接";
  198. } else if (rssi < -90) {
  199. text = "不可连接";
  200. } else {
  201. text = "信号差";
  202. }
  203. return text + `(${rssi})`;
  204. },
  205. getRSSIStyle: function (rssi) {
  206. let obj = {
  207. h: 10,
  208. BC: "#5AB73F",
  209. };
  210. if (rssi < -60 && rssi >= -69) {
  211. obj.h = 8;
  212. } else if (rssi <= -70 && rssi >= -79) {
  213. obj.h = 6;
  214. obj.BC = "#F29C37";
  215. } else if (rssi <= -80 && rssi >= -89) {
  216. obj.h = 4;
  217. obj.BC = "#EB4B5C";
  218. } else if (rssi <= -90) {
  219. obj.h = 0;
  220. }
  221. return obj;
  222. },
  223. },
  224. };
  225. </script>
  226. <style lang="scss" scope>
  227. .device {
  228. display: flex;
  229. align-items: center;
  230. margin-top: 15px;
  231. font-size: 14px;
  232. color: #fff;
  233. font-weight: 700;
  234. padding-left: 5px;
  235. padding-bottom: 5px;
  236. .rssi {
  237. display: flex;
  238. align-items: center;
  239. height: 17px;
  240. margin: 0 6px;
  241. .text {
  242. font-family: PingFang SC, PingFang SC;
  243. font-size: 12px;
  244. color: #666666;
  245. margin-right: 18px;
  246. }
  247. .signal {
  248. display: flex;
  249. align-items: flex-end;
  250. height: 12px;
  251. .cell {
  252. width: 3px;
  253. border-radius: 1px;
  254. margin-right: 1px;
  255. }
  256. }
  257. }
  258. }
  259. </style>