connectingDevice.vue 7.3 KB

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