123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- const {
- BtHelper
- } = require('../../devices/bt_helper.js');
- module.exports = {
- compareList: compareList,
- compareList2: compareList2,
- }
- var deviceListIndex = 0
- async function compareList2(deviceList, callback) {
- let isHasWifi = deviceList.find(item => {
- return item.connectType === 3 && item.state === "online";
- });
- if (isHasWifi) {
- //有wifi
- console.log("有wifi在线")
- callback()
- return;
- }
- if (deviceList.length == 1) {
- // 只有一个直接连接
- let device = deviceList[0];
- if (device.state === 'online') {
- console.log("有在线的2", JSON.stringify(device))
- return;
- }
- // getApp().getBluetoothStatus();
- callback(deviceList[0])
- console.log("有在线的3", JSON.stringify(deviceList[0]))
- return;
- }
- let hasOnline = deviceList.find(item => {
- return item.state === 'online';
- });
- if (hasOnline) {
- //有在线的
- console.log("有蓝牙在线", JSON.stringify(hasOnline))
- callback(hasOnline)
- return;
- }
- let bleDev = deviceList[deviceListIndex];
- if (bleDev.connectType === 1) {
- callback(deviceList[deviceListIndex])
- deviceListIndex++
- } else {
- for (var i = 0; i < deviceList.length; i++) {
- let element = deviceList[i];
- if (element.connectType === 1) {
- bleDev = element;
- callback(element)
- deviceListIndex = i;
- return;
- }
- }
- }
- if (deviceListIndex >= deviceList.length) {
- deviceListIndex = 0;
- }
- // _startScan(deviceListIndex, deviceList, callback);
- }
- function _startScan(index, deviceList, callback) {
- console.log("_startScan", index);
- if (index >= deviceList.length) {
- deviceListIndex = 0
- callback()
- return
- }
- let element = deviceList[index];
- BtHelper.getInstance().startScan(element, function (boolean) {
- if (!boolean) {
- index++;
- if (index < deviceList.length) {
- element = deviceList[index];
- // _startScan(index, deviceList, callback);
- } else {
- deviceListIndex = 0
- // callback()
- // return
- }
- }
- }, function (res) {
- deviceListIndex = 0
- console.log("自动连接上的", element.deviceId, res.deviceId);
- BtHelper.getInstance().stopSearch();
- callback(element)
- });
- }
- function compareList(changeCallback, addBlueCallback) {
- var pages = getCurrentPages();
- var deviceList = pages[0].getDeviceList()
- if (deviceList.length > 0) {
- const {
- BtHelper
- } = require('../../devices/bt_helper.js');
- if (BtHelper.getInstance().getCallBackConnect() == null) {
- var compareList = BtHelper.getInstance().getCompareList();
- var dissmissDevice = BtHelper.getInstance().getDissmissDevice();
- console.log("compareList", compareList);
- console.log("dissmissDevice", dissmissDevice);
- ///去掉未连接的离线的
- for (var i = 0; i < dissmissDevice.length; i++) {
- for (var j = 0; j < compareList.length; j++) {
- if (compareList[j].deviceId == dissmissDevice[i].deviceId) {
- compareList.splice(j, 1);
- break;
- }
- }
- }
- if (compareList.length > 0) {
- ///对比在线的蓝牙设备
- var isChanged = false;
- for (var i = 0; i < deviceList.length; i++) {
- var tempItem = deviceList[i];
- if (tempItem.connectType != 3) {
- var has = false;
- for (var j = 0; j < compareList.length; j++) {
- // "state":"online" "offline" MW-SR1(4G_WIFI)
- if (tempItem.deviceId === compareList[j].deviceId) {
- has = true;
- break;
- }
- }
- if (has) {
- if (tempItem.state != "online") {
- isChanged = true;
- tempItem.state = "online";
- }
- } else {
- if (tempItem.state != "offline") {
- isChanged = true;
- tempItem.state = "offline";
- }
- }
- }
- }
- if (isChanged) {
- changeCallback(deviceList);
- }
- ///没有连接则连接第一个在线的蓝牙
- var autoConnected = pages[0].getAutoConnected();
- var deviceListSelect = pages[0].getDeviceListSelect();
- if (!autoConnected && deviceListSelect == null) {
- var list = pages[0].getDeviceList();
- for (var i = 0; i < list.length; i++) {
- var item = list[i];
- if (item.connectType == 1 && item.state == "online") {
- item.name = item.devName;
- BtHelper.getInstance().connect(item, function (isConnected, device) {
- if (isConnected) {
- addBlueCallback(device);
- }
- });
- break;
- }
- }
- }
- }
- BtHelper.getInstance().startScan(null, null, null);
- }
- }
- };
|