util.js 4.9 KB

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