util.js 4.5 KB

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