scan.js 2.5 KB

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