connectingDevice.vue 6.7 KB

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