scan.js 2.4 KB

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