// // BindingViewModels.swift // Lookfit // // Created by lemo. on 2020/3/21. // Copyright © 2020 Sheldon. All rights reserved. // import UIKit import CoreBluetooth class BindingViewModels: ViewModel { let connectState = PublishSubject() let scanState = PublishSubject() let scanTimeout = PublishSubject() let scanDevices = BehaviorRelay<[SectionModel]>.init(value: [SectionModel(model: MultiLanguageKey.scaning.localized, items: [])]) var scanBleModels: [BLEModel] = [] override init() { super.init() Bluetooth.shareInstance()!.delegate = self monitor() } private func monitor() { // 连接成功 kNotificationCenter.rx.notification(Notification.Name(rawValue: BluetoothNotificationAtConnectSuccess)) .subscribe(onNext: { [weak self] (notification) in self?.connectState.onNext(true) guard let bleModel = notification.object as? BLEModel else { return } // 保存当前设备信息 let deviceInfo = DeviceInfo(deviceName: bleModel.peripheral.name ?? "", UUIDString: bleModel.uuidString, MACString: bleModel.macString ?? "") UserDefaultsManager.saveDeviceInfo(deviceInfo: deviceInfo) // 发出绑定成功通知 kNotificationCenter.post(name: NSNotification.Name(BindingDevice), object: bleModel) }) .disposed(by: rx.disposeBag) } deinit { GCDTimer.shared.cancleTimer(WithTimerName: "connectedDevice") GCDTimer.shared.cancleTimer(WithTimerName: "scanDevice") Bluetooth.shareInstance()!.cancelScan() } } extension BindingViewModels { func startScan() { let state = Bluetooth.shareInstance()!.state.rawValue == 5 if state { // 清空当前重新扫描 scanBleModels.removeAll() // 超时处理 GCDTimer.shared.scheduledDispatchTimer(WithTimerName: "scanDevice", timeInterval: 15, queue: .main, repeats: false, immediately: false) { if self.scanBleModels.count == 0 { self.scanTimeout.onNext(()) } } // 开启扫描 Bluetooth.shareInstance()!.scan(forPeripheralsWithdiscoverServices: nil, isDFU: false) } scanState.onNext(state) } func connctDevice(bleModel: BLEModel) { let state = Bluetooth.shareInstance()!.state.rawValue == 5 if !state { self.connectState.onNext(false) return } // 连接设备 Bluetooth.shareInstance()!.connected(with: bleModel) // 超时处理 GCDTimer.shared.scheduledDispatchTimer(WithTimerName: "connectedDevice", timeInterval: 15, queue: .main, repeats: false, immediately: false) { self.connectState.onNext(false) } } } extension BindingViewModels: BluetoothDelegate { func onDiscover(toPeripheralsBleModel bleModel: BLEModel!) { guard let _ = bleModel.peripheral.name else { return } // 替换重复设备 var isAdd = false for (index, oldModel) in scanBleModels.enumerated() { if (oldModel.macString == bleModel.macString && oldModel.peripheral.name == bleModel.peripheral.name) || oldModel.uuidString == bleModel.uuidString { scanBleModels[index] = bleModel isAdd = true break } } if isAdd == false { scanBleModels.append(bleModel) } // 处理蓝牙信号强度量为正数127的问题 if let rssi = bleModel.rssi, rssi.intValue > 0 { bleModel.rssi = NSNumber(value: -bleModel.rssi.intValue) } var items: [TableViewCellModel] = [] scanBleModels.enumerated().forEach { (offset, tempBleModel) in let isBottomLine = offset != scanBleModels.count - 1 let rssiText = tempBleModel.rssi.intValue != 0 ? "\(tempBleModel.rssi.intValue)" : "" let cellModel = TableViewCellModel(title: tempBleModel.peripheral.name ?? "", isSwitch: false, description: tempBleModel.macString, isArrows: false, isOn: false, image: R.image.icon_device(), isBottomLine: isBottomLine, righeImg: nil, rightBottomText: rssiText) items.append(cellModel) } let sectionModel = SectionModel(model: MultiLanguageKey.scaning.localized, items: items) scanDevices.accept([sectionModel]) GCDTimer.shared.cancleTimer(WithTimerName: "scanDevice") } }