util.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. var isRead = false;
  79. var isWrited = false;
  80. that.manager.setWriteServiceId(serviceId);
  81. that.manager.getCharacteristics(device, function () {
  82. that.disconnect(device);
  83. onChanged(false);
  84. }, function (notifyCharaterId) {
  85. ///监听特征值变化
  86. console.log("gadsfasdfadfaf===444444==");
  87. var isTrue = that.manager.monitorCharacteristicValueChange(device, serviceId, notifyCharaterId);
  88. if (!isTrue) {
  89. onChanged(false);
  90. return;
  91. }
  92. ///数据接收
  93. that.manager.onBLECharacteristicValueChange(function (value) {
  94. BtParse.parseTLV(value);
  95. });
  96. isRead = true;
  97. if (isWrited) {
  98. console.log("gadsfasdfadfaf===7777==");
  99. setTimeout(() => {
  100. onChanged(true);
  101. }, 300);
  102. }
  103. },
  104. async function () {
  105. isWrited = true;
  106. console.log("gadsfasdfadfaf===555==");
  107. if (isRead) {
  108. console.log("gadsfasdfadfaf===8888==");
  109. setTimeout(() => {
  110. onChanged(true);
  111. }, 300);
  112. }
  113. },
  114. function () {
  115. onChanged(false);
  116. return;
  117. });
  118. }
  119. ///发送数据
  120. async sendData(device, wifiName, pwd) {
  121. var that = this;
  122. var text = that.sendWiFiInfo(wifiName, pwd);
  123. console.log("gadsfasdfadfaf===888==" + text);
  124. var isTrue = await that.manager.sendData(device, text);
  125. console.log("gadsfasdfadfaf===999==" + isTrue);
  126. ///发送数据成功
  127. if (isTrue) {
  128. }
  129. }
  130. sendWiFiInfo(wifiName, pwd) {
  131. const hex_util = require('../hex_util');
  132. let result = [];
  133. // 字母*6 +
  134. let wifiList = hex_util.string2ListInt(wifiName);
  135. // 数字*3 +
  136. let pwdList = hex_util.string2ListInt(pwd);
  137. // 16进制
  138. result.push(0x22);
  139. result.push(parseInt(hex_util.int2Hex(wifiList.length + pwdList.length + 6), 16));
  140. // 账号
  141. result.push(0x33);
  142. result.push(parseInt(hex_util.int2Hex(wifiList.length), 16));
  143. let p = result[3] + 4;
  144. let j = 0;
  145. for (let i = 4; i < p; i++) {
  146. result.splice(i, 0, wifiList[j++]);
  147. }
  148. // 密码
  149. result.splice(p, 0, 0x44);
  150. result.splice(++p, 0, parseInt(hex_util.int2Hex(pwdList.length), 16));
  151. p++;
  152. j = 0;
  153. for (let i = p; i < p + pwdList.length; i++) {
  154. result.splice(i, 0, pwdList[j++]);
  155. }
  156. ///[34, 22, 51, 5, 109, 117, 122, 101, 110, 68, 11, 109, 117, 122, 101, 110, 111, 102, 102, 105, 99, 101]
  157. console.log("发送wifi账号密码:", result.toString());
  158. return result.toString();
  159. }
  160. ///断开设备连接
  161. async disconnect(device) {
  162. var that = this;
  163. await that.manager.disconnect(device);
  164. }
  165. }
  166. module.exports = {
  167. BleUtil
  168. }