scan.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. console.log("有在线的3", JSON.stringify(deviceList[deviceListIndex]), deviceListIndex)
  41. callback(deviceList[deviceListIndex])
  42. deviceListIndex++
  43. if (deviceListIndex >= deviceList.length) {
  44. deviceListIndex = 0;
  45. }
  46. // _startScan(deviceListIndex, deviceList, callback);
  47. }
  48. function _startScan(index, deviceList, callback) {
  49. console.log("_startScan", index);
  50. if (index >= deviceList.length) {
  51. deviceListIndex = 0
  52. callback()
  53. return
  54. }
  55. let element = deviceList[index];
  56. BtHelper.getInstance().startScan(element, function (boolean) {
  57. if (!boolean) {
  58. index++;
  59. if (index < deviceList.length) {
  60. element = deviceList[index];
  61. // _startScan(index, deviceList, callback);
  62. } else {
  63. deviceListIndex = 0
  64. // callback()
  65. // return
  66. }
  67. }
  68. }, function (res) {
  69. deviceListIndex = 0
  70. console.log("自动连接上的", element.deviceId, res.deviceId);
  71. BtHelper.getInstance().stopSearch();
  72. callback(element)
  73. });
  74. }
  75. function compareList(changeCallback, addBlueCallback) {
  76. var pages = getCurrentPages();
  77. var deviceList = pages[0].getDeviceList()
  78. if (deviceList.length > 0) {
  79. const {
  80. BtHelper
  81. } = require('../../devices/bt_helper.js');
  82. if (BtHelper.getInstance().getCallBackConnect() == null) {
  83. var compareList = BtHelper.getInstance().getCompareList();
  84. var dissmissDevice = BtHelper.getInstance().getDissmissDevice();
  85. console.log("compareList", compareList);
  86. console.log("dissmissDevice", dissmissDevice);
  87. ///去掉未连接的离线的
  88. for (var i = 0; i < dissmissDevice.length; i++) {
  89. for (var j = 0; j < compareList.length; j++) {
  90. if (compareList[j].deviceId == dissmissDevice[i].deviceId) {
  91. compareList.splice(j, 1);
  92. break;
  93. }
  94. }
  95. }
  96. if (compareList.length > 0) {
  97. ///对比在线的蓝牙设备
  98. var isChanged = false;
  99. for (var i = 0; i < deviceList.length; i++) {
  100. var tempItem = deviceList[i];
  101. if (tempItem.connectType != 3) {
  102. var has = false;
  103. for (var j = 0; j < compareList.length; j++) {
  104. // "state":"online" "offline" MW-SR1(4G_WIFI)
  105. if (tempItem.deviceId === compareList[j].deviceId) {
  106. has = true;
  107. break;
  108. }
  109. }
  110. if (has) {
  111. if (tempItem.state != "online") {
  112. isChanged = true;
  113. tempItem.state = "online";
  114. }
  115. } else {
  116. if (tempItem.state != "offline") {
  117. isChanged = true;
  118. tempItem.state = "offline";
  119. }
  120. }
  121. }
  122. }
  123. if (isChanged) {
  124. changeCallback(deviceList);
  125. }
  126. ///没有连接则连接第一个在线的蓝牙
  127. var autoConnected = pages[0].getAutoConnected();
  128. var deviceListSelect = pages[0].getDeviceListSelect();
  129. if (!autoConnected && deviceListSelect == null) {
  130. var list = pages[0].getDeviceList();
  131. for (var i = 0; i < list.length; i++) {
  132. var item = list[i];
  133. if (item.connectType == 1 && item.state == "online") {
  134. item.name = item.devName;
  135. BtHelper.getInstance().connect(item, function (isConnected, device) {
  136. if (isConnected) {
  137. addBlueCallback(device);
  138. }
  139. });
  140. break;
  141. }
  142. }
  143. }
  144. }
  145. BtHelper.getInstance().startScan(null, null, null);
  146. }
  147. }
  148. };