connectingDevice.vue 7.3 KB

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