scan.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. module.exports = {
  2. compareList: compareList,
  3. }
  4. function compareList(changeCallback, addBlueCallback) {
  5. var deviceList = getCurrentPages()[0].getDeviceList()
  6. if (deviceList.length > 0) {
  7. const {
  8. BtHelper
  9. } = require('../../devices/bt_helper.js');
  10. if (BtHelper.getInstance().getCallBackConnect() == null) {
  11. var compareList = BtHelper.getInstance().getCompareList();
  12. var dissmissDevice = BtHelper.getInstance().getDissmissDevice();
  13. ///去掉未连接的离线的
  14. for (var i = 0; i < dissmissDevice.length; i++) {
  15. for (var j = 0; j < compareList.length; j++) {
  16. if (compareList[j].deviceId == dissmissDevice[i].deviceId) {
  17. compareList.splice(j, 1);
  18. break;
  19. }
  20. }
  21. }
  22. if (compareList.length > 0) {
  23. ///对比在线的蓝牙设备
  24. var isChanged = false;
  25. for (var i = 0; i < deviceList.length; i++) {
  26. var tempItem = deviceList[i];
  27. if (tempItem.connectType != 3) {
  28. var has = false;
  29. for (var j = 0; j < compareList.length; j++) {
  30. // "state":"online" "offline" MW-SR1(4G_WIFI)
  31. if (tempItem.deviceId === compareList[j].deviceId) {
  32. has = true;
  33. break;
  34. }
  35. }
  36. if (has) {
  37. if (tempItem.state != "online") {
  38. isChanged = true;
  39. tempItem.state = "online";
  40. }
  41. } else {
  42. if (tempItem.state != "offline") {
  43. isChanged = true;
  44. tempItem.state = "offline";
  45. }
  46. }
  47. }
  48. }
  49. if (isChanged) {
  50. changeCallback(deviceList);
  51. }
  52. ///没有连接则连接第一个在线的蓝牙
  53. var autoConnected = getCurrentPages()[0].getAutoConnected();
  54. var deviceListSelect = getCurrentPages()[0].getDeviceListSelect();
  55. if (!autoConnected && deviceListSelect == null) {
  56. var list = getCurrentPages()[0].getDeviceList();
  57. for (var i = 0; i < list.length; i++) {
  58. var item = list[i];
  59. if (item.connectType == 1 && item.state == "online") {
  60. item.name = item.devName;
  61. BtHelper.getInstance().connect(item, function (data) {
  62. if (data) {
  63. addBlueCallback(item);
  64. }
  65. });
  66. break;
  67. }
  68. }
  69. }
  70. }
  71. BtHelper.getInstance().startScan(null, null, null);
  72. }
  73. }
  74. };