scan.js 4.1 KB

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