util.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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, connected) {
  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. if (strings.isEmpty(services)) {
  64. that.disconnect(device);
  65. onChanged(false);
  66. return;
  67. }
  68. var serviceId = serviceId = services[0].uuid;
  69. for (let i = 0; i < services.length; i++) {
  70. if (services[i].uuid.toUpperCase().indexOf("FFE5") != -1 ||
  71. services[i].uuid.toUpperCase().indexOf("1111") != -1
  72. ) {
  73. serviceId = services[i].uuid;
  74. break;
  75. }
  76. }
  77. that.manager.setWriteServiceId(serviceId);
  78. that.manager.getCharacteristics(device, function () {
  79. that.disconnect(device);
  80. onChanged(false);
  81. },
  82. function (notifyUuid) {
  83. var isTrue = that.manager.monitorCharacteristicValueChange(device, serviceId, notifyUuid);
  84. if (!isTrue) {
  85. onChanged(false);
  86. return;
  87. }
  88. onChanged(true);
  89. ///数据接收
  90. that.manager.onBLECharacteristicValueChange(device, function (value) {
  91. // BtParse.parseTLV(value);
  92. if (value == "mqtt") {
  93. } else if (value == "recv_finish") {
  94. connected(true);
  95. }
  96. });
  97. });
  98. }
  99. ///发送数据
  100. async sendData(device, wifiName, pwd) {
  101. var that = this;
  102. var list = that.sendWiFiInfo(wifiName, pwd);
  103. var isTrue = await that.manager.sendData(device, list);
  104. ///发送数据成功
  105. if (isTrue) {
  106. }
  107. }
  108. sendWiFiInfo(wifiName, pwd) {
  109. const hex_util = require('../hex_util');
  110. let result = [];
  111. // 字母*6 +
  112. let wifiList = hex_util.string2ListInt(wifiName);
  113. // 数字*3 +
  114. let pwdList = hex_util.string2ListInt(pwd);
  115. // 16进制
  116. result.push(0x22);
  117. result.push(parseInt(hex_util.int2Hex(wifiList.length + pwdList.length + 6), 16));
  118. // 账号
  119. result.push(0x33);
  120. result.push(parseInt(hex_util.int2Hex(wifiList.length), 16));
  121. let p = result[3] + 4;
  122. let j = 0;
  123. for (let i = 4; i < p; i++) {
  124. result.splice(i, 0, wifiList[j++]);
  125. }
  126. // 密码
  127. result.splice(p, 0, 0x44);
  128. result.splice(++p, 0, parseInt(hex_util.int2Hex(pwdList.length), 16));
  129. p++;
  130. j = 0;
  131. for (let i = p; i < p + pwdList.length; i++) {
  132. result.splice(i, 0, pwdList[j++]);
  133. }
  134. ///[34, 22, 51, 5, 109, 117, 122, 101, 110, 68, 11, 109, 117, 122, 101, 110, 111, 102, 102, 105, 99, 101]
  135. console.log("发送wifi账号密码:", result.toString());
  136. return result;
  137. }
  138. ///断开设备连接
  139. async disconnect(device) {
  140. var that = this;
  141. await that.manager.disconnect(device);
  142. }
  143. }
  144. module.exports = {
  145. BleUtil
  146. }