scan.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. const {
  2. BtHelper
  3. } = require('../../devices/bt_helper.js');
  4. module.exports = {
  5. compareList: compareList,
  6. compareList2: compareList2,
  7. }
  8. var deviceListIndex = 0
  9. async function compareList2(deviceList, callback) {
  10. let isHasWifi = deviceList.find(item => {
  11. return item.connectType === 3 && item.state === "online";
  12. });
  13. if (isHasWifi) {
  14. //有wifi
  15. console.log("有wifi在线")
  16. callback()
  17. return;
  18. }
  19. if (deviceList.length == 1) {
  20. // 只有一个直接连接
  21. let device = deviceList[0];
  22. if (device.state === 'online') {
  23. console.log("有在线的2", JSON.stringify(device))
  24. return;
  25. }
  26. // getApp().getBluetoothStatus();
  27. callback(deviceList[0])
  28. console.log("有在线的3", JSON.stringify(deviceList[0]))
  29. return;
  30. }
  31. let hasOnline = deviceList.find(item => {
  32. return item.state === 'online';
  33. });
  34. if (hasOnline) {
  35. //有在线的
  36. console.log("有蓝牙在线", JSON.stringify(hasOnline))
  37. callback(hasOnline)
  38. return;
  39. }
  40. let bleDev = deviceList[deviceListIndex];
  41. if (bleDev.connectType === 1) {
  42. callback(deviceList[deviceListIndex])
  43. deviceListIndex++
  44. } else {
  45. for (var i = 0; i < deviceList.length; i++) {
  46. let element = deviceList[i];
  47. if (element.connectType === 1) {
  48. bleDev = element;
  49. callback(element)
  50. deviceListIndex = i;
  51. return;
  52. }
  53. }
  54. }
  55. if (deviceListIndex >= deviceList.length) {
  56. deviceListIndex = 0;
  57. }
  58. // _startScan(deviceListIndex, deviceList, callback);
  59. }
  60. function _startScan(index, deviceList, callback) {
  61. console.log("_startScan", index);
  62. if (index >= deviceList.length) {
  63. deviceListIndex = 0
  64. callback()
  65. return
  66. }
  67. let element = deviceList[index];
  68. BtHelper.getInstance().startScan(element, function (boolean) {
  69. if (!boolean) {
  70. index++;
  71. if (index < deviceList.length) {
  72. element = deviceList[index];
  73. // _startScan(index, deviceList, callback);
  74. } else {
  75. deviceListIndex = 0
  76. // callback()
  77. // return
  78. }
  79. }
  80. }, function (res) {
  81. deviceListIndex = 0
  82. console.log("自动连接上的", element.deviceId, res.deviceId);
  83. BtHelper.getInstance().stopSearch();
  84. callback(element)
  85. });
  86. }
  87. function compareList(changeCallback, addBlueCallback) {
  88. var pages = getCurrentPages();
  89. var deviceList = pages[0].getDeviceList()
  90. if (deviceList.length > 0) {
  91. const {
  92. BtHelper
  93. } = require('../../devices/bt_helper.js');
  94. if (BtHelper.getInstance().getCallBackConnect() == null) {
  95. var compareList = BtHelper.getInstance().getCompareList();
  96. var dissmissDevice = BtHelper.getInstance().getDissmissDevice();
  97. console.log("compareList", compareList);
  98. console.log("dissmissDevice", dissmissDevice);
  99. ///去掉未连接的离线的
  100. for (var i = 0; i < dissmissDevice.length; i++) {
  101. for (var j = 0; j < compareList.length; j++) {
  102. if (compareList[j].deviceId == dissmissDevice[i].deviceId) {
  103. compareList.splice(j, 1);
  104. break;
  105. }
  106. }
  107. }
  108. if (compareList.length > 0) {
  109. ///对比在线的蓝牙设备
  110. var isChanged = false;
  111. for (var i = 0; i < deviceList.length; i++) {
  112. var tempItem = deviceList[i];
  113. if (tempItem.connectType != 3) {
  114. var has = false;
  115. for (var j = 0; j < compareList.length; j++) {
  116. // "state":"online" "offline" MW-SR1(4G_WIFI)
  117. if (tempItem.deviceId === compareList[j].deviceId) {
  118. has = true;
  119. break;
  120. }
  121. }
  122. if (has) {
  123. if (tempItem.state != "online") {
  124. isChanged = true;
  125. tempItem.state = "online";
  126. }
  127. } else {
  128. if (tempItem.state != "offline") {
  129. isChanged = true;
  130. tempItem.state = "offline";
  131. }
  132. }
  133. }
  134. }
  135. if (isChanged) {
  136. changeCallback(deviceList);
  137. }
  138. ///没有连接则连接第一个在线的蓝牙
  139. var autoConnected = pages[0].getAutoConnected();
  140. var deviceListSelect = pages[0].getDeviceListSelect();
  141. if (!autoConnected && deviceListSelect == null) {
  142. var list = pages[0].getDeviceList();
  143. for (var i = 0; i < list.length; i++) {
  144. var item = list[i];
  145. if (item.connectType == 1 && item.state == "online") {
  146. item.name = item.devName;
  147. BtHelper.getInstance().connect(item, function (isConnected, device) {
  148. if (isConnected) {
  149. addBlueCallback(device);
  150. }
  151. });
  152. break;
  153. }
  154. }
  155. }
  156. }
  157. BtHelper.getInstance().startScan(null, null, null);
  158. }
  159. }
  160. };