util.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. const Manager = require('./manager');
  2. const BtParse = require('../../devices/bluetooth/bt_parse');
  3. /// MW-X4 (BZK)
  4. class BleUtil {
  5. static _instance = new BleUtil();
  6. static getInstance() {
  7. return BleUtil._instance;
  8. }
  9. constructor() {
  10. var that = this;
  11. that.manager = new Manager();
  12. }
  13. ///获取比较的数据
  14. getCompareList() {
  15. var that = this;
  16. return that.manager.getCompareList();
  17. }
  18. getDissmissDevice() {
  19. var that = this;
  20. return that.manager.getDissmissDevice();
  21. }
  22. getCallBackConnect() {
  23. var that = this;
  24. return that.manager.getCallBackConnect();
  25. }
  26. ///初始化蓝牙适配器
  27. initBluetoothAdapter() {
  28. var that = this;
  29. that.manager.initBlueAdapter();
  30. }
  31. ///获取蓝牙设备 connect:true,去连接,false,去对比
  32. listenBlueDevices() {
  33. var that = this;
  34. that.manager.listenBlueDevices();
  35. }
  36. ///搜索蓝牙设备
  37. startScan(connectWillDevice, boolean, callBackConnect) {
  38. var that = this;
  39. that.manager.startScan(
  40. connectWillDevice,
  41. boolean,
  42. callBackConnect,
  43. );
  44. }
  45. ///停止搜索蓝牙设备
  46. async stopScan() {
  47. var that = this;
  48. await that.manager.stopScan();
  49. }
  50. ///开始连接设备
  51. async startConnect(device, onChanged) {
  52. var that = this;
  53. await that.stopScan();
  54. await that.disconnect(device);
  55. var res = await that.manager.startConnect(device);
  56. if (!res) {
  57. that.disconnect(device);
  58. onChanged(false);
  59. return;
  60. }
  61. const strings = require('./../../utils/strings');
  62. var services = await that.manager.getNotifyServices(device);
  63. console.log("gadsfasdfadfaf===333==" + JSON.stringify(services));
  64. if (strings.isEmpty(services)) {
  65. that.disconnect(device);
  66. onChanged(false);
  67. return;
  68. }
  69. var serviceId = serviceId = services[0].uuid;
  70. for (let i = 0; i < services.length; i++) {
  71. if (services[i].uuid.toUpperCase().indexOf("FFE5") != -1 ||
  72. services[i].uuid.toUpperCase().indexOf("1111") != -1
  73. ) {
  74. serviceId = services[i].uuid;
  75. break;
  76. }
  77. }
  78. that.manager.setWriteServiceId(serviceId);
  79. that.manager.getCharacteristics(device, function () {
  80. that.disconnect(device);
  81. onChanged(false);
  82. },
  83. function (notifyUuid) {
  84. var isTrue = that.manager.monitorCharacteristicValueChange(device, serviceId, notifyUuid);
  85. if (!isTrue) {
  86. onChanged(false);
  87. return;
  88. }
  89. ///数据接收
  90. that.manager.onBLECharacteristicValueChange(function (value) {
  91. BtParse.parseTLV(value);
  92. });
  93. console.log("gadsfasdfadfaf===7777==");
  94. setTimeout(() => {
  95. onChanged(true);
  96. }, 300);
  97. });
  98. }
  99. ///发送数据
  100. async sendData(device, wifiName, pwd) {
  101. var that = this;
  102. var text = that.sendWiFiInfo(wifiName, pwd);
  103. console.log("gadsfasdfadfaf===888==" + text);
  104. var isTrue = await that.manager.sendData(device, text);
  105. console.log("gadsfasdfadfaf===999==" + isTrue);
  106. ///发送数据成功
  107. if (isTrue) {
  108. }
  109. }
  110. sendWiFiInfo(wifiName, pwd) {
  111. const hex_util = require('../hex_util');
  112. let result = [];
  113. // 字母*6 +
  114. let wifiList = hex_util.string2ListInt(wifiName);
  115. // 数字*3 +
  116. let pwdList = hex_util.string2ListInt(pwd);
  117. // 16进制
  118. result.push(0x22);
  119. result.push(parseInt(hex_util.int2Hex(wifiList.length + pwdList.length + 6), 16));
  120. // 账号
  121. result.push(0x33);
  122. result.push(parseInt(hex_util.int2Hex(wifiList.length), 16));
  123. let p = result[3] + 4;
  124. let j = 0;
  125. for (let i = 4; i < p; i++) {
  126. result.splice(i, 0, wifiList[j++]);
  127. }
  128. // 密码
  129. result.splice(p, 0, 0x44);
  130. result.splice(++p, 0, parseInt(hex_util.int2Hex(pwdList.length), 16));
  131. p++;
  132. j = 0;
  133. for (let i = p; i < p + pwdList.length; i++) {
  134. result.splice(i, 0, pwdList[j++]);
  135. }
  136. ///[34, 22, 51, 5, 109, 117, 122, 101, 110, 68, 11, 109, 117, 122, 101, 110, 111, 102, 102, 105, 99, 101]
  137. console.log("发送wifi账号密码:", result.toString());
  138. return result.toString();
  139. }
  140. ///断开设备连接
  141. async disconnect(device) {
  142. var that = this;
  143. await that.manager.disconnect(device);
  144. }
  145. }
  146. module.exports = {
  147. BleUtil
  148. }