scan.js 3.8 KB

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