connectingDevice.vue 7.2 KB

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