diff --git a/LaserMethane.xcworkspace/xcuserdata/a203.xcuserdatad/UserInterfaceState.xcuserstate b/LaserMethane.xcworkspace/xcuserdata/a203.xcuserdatad/UserInterfaceState.xcuserstate index 92b401c..7af5918 100644 --- a/LaserMethane.xcworkspace/xcuserdata/a203.xcuserdatad/UserInterfaceState.xcuserstate +++ b/LaserMethane.xcworkspace/xcuserdata/a203.xcuserdatad/UserInterfaceState.xcuserstate Binary files differ diff --git a/LaserMethane.xcworkspace/xcuserdata/a203.xcuserdatad/UserInterfaceState.xcuserstate b/LaserMethane.xcworkspace/xcuserdata/a203.xcuserdatad/UserInterfaceState.xcuserstate index 92b401c..7af5918 100644 --- a/LaserMethane.xcworkspace/xcuserdata/a203.xcuserdatad/UserInterfaceState.xcuserstate +++ b/LaserMethane.xcworkspace/xcuserdata/a203.xcuserdatad/UserInterfaceState.xcuserstate Binary files differ diff --git a/LaserMethane/ViewController/Map/MapViewController.swift b/LaserMethane/ViewController/Map/MapViewController.swift index 9054bff..c53e6fa 100644 --- a/LaserMethane/ViewController/Map/MapViewController.swift +++ b/LaserMethane/ViewController/Map/MapViewController.swift @@ -56,6 +56,7 @@ private var writeCharacteristic: CBCharacteristic! private var deviceName: String! private var cbPeripheral: CBPeripheral! + private var isConnected = false // 弹窗里面的输入框 private var inputTextField: UITextField! private var inspectionId: String! @@ -239,17 +240,23 @@ return } centralManager.cancelPeripheralConnection(cbPeripheral) + isConnected = false } // 搜索蓝牙 @IBAction func searchBluetoothAction(_ sender: Any) { if isBluetoothAvailable() { - LoadingHub.shared.showLoading(text: "设备搜索中...") - centralManager.scanForPeripherals(withServices: nil, options: nil) - DispatchQueue.main.asyncAfter(deadline: .now() + 3, execute: { [self] in - // 列表显示扫描到的设备 - showTableView() - }) + if isConnected { + centralManager.cancelPeripheralConnection(cbPeripheral) + isConnected = false + } else { + LoadingHub.shared.showLoading(text: "设备搜索中...") + centralManager.scanForPeripherals(withServices: nil, options: nil) + DispatchQueue.main.asyncAfter(deadline: .now() + 3, execute: { [self] in + // 列表显示扫描到的设备 + showTableView() + }) + } } else { AlertHub.shared.showWaringAlert(controller: self, message: "蓝牙不可用,请检查") } @@ -481,16 +488,16 @@ func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) { deviceName = peripheral.name if deviceName != nil { - if deviceName.isNumber() { - // 如果不包含 就加入 - if !scanDevices.contains(peripheral) { - scanDevices.append(peripheral) - } - } -// // 如果不包含 就加入 -// if !scanDevices.contains(peripheral) { -// scanDevices.append(peripheral) +// if deviceName.isNumber() { +// // 如果不包含 就加入 +// if !scanDevices.contains(peripheral) { +// scanDevices.append(peripheral) +// } // } + // 如果不包含 就加入 + if !scanDevices.contains(peripheral) { + scanDevices.append(peripheral) + } } } @@ -501,6 +508,7 @@ cbPeripheral = peripheral cbPeripheral.delegate = self cbPeripheral.discoverServices(nil) + isConnected = true } // 设备连接失败 @@ -576,6 +584,8 @@ } if Constant.READ_CHARACTERISTIC_UUID.rawValue == characteristic.uuid.uuidString { let data = characteristic.value! +// print("蓝牙探测数据: \(data)") +// print("探测数据长度: \(data.count)") if data.count == 28 { // 51, 51, 50, 48, 50, 49, 48, 49, 48, 48, 48, 51, 13, 10, 170, 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 13, 10 // 只解析前14位,后14位是测量值 @@ -594,17 +604,20 @@ * 13,10是数据结束位 * */ // 浓度:4字节 0-99999 - let potencyValue = data[1] * UInt8(UInt16(256)) * 3 + - data[2] * UInt8(UInt16(256)) * 2 + - data[3] * UInt8(UInt16(256)) * 1 + data[4] + + let p1 = UInt(data[1]) * 256 * 3 + let p2 = UInt(data[2]) * 256 * 2 + let p3 = UInt(data[3]) * 256 * 1 + let potencyValue = p1 + p2 + p3 + UInt(data[4]) currentValueLabel.text = potencyValue.description // 报警值:2字节 0-65535 - let alarmValue = data[6] * UInt8(UInt16(256)) * 1 + data[7] + let alarmValue = UInt(data[6]) * 256 + UInt(data[7]) settingValueLabel.text = alarmValue.description // 5s内最大值: 4字节 0-99999 - let maxPotencyValue = data[8] * UInt8(UInt16(256)) * 3 + - data[9] * UInt8(UInt16(256)) * 2 + - data[10] * UInt8(UInt16(256)) * 1 + data[11] + let m1 = UInt(data[8]) * 256 * 3 + let m2 = UInt(data[9]) * 256 * 2 + let m3 = UInt(data[10]) * 256 * 1 + let maxPotencyValue = m1 + m2 + m3 + UInt(data[11]) maxValueLabel.text = maxPotencyValue.description // 判断是否需要报警 let isOpenWarning = keychain[Constant.OpenWarning.rawValue] ?? "OFF" @@ -625,8 +638,6 @@ // 当测量值小于设置值,报警还未结束,需要停止响铃 print("测量值小于设置值,停止响铃") } - } else { - print("报警开关未打开,不处理") } } else { print("错误数据")