You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

120 lines
4.6 KiB

//
// 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<Bool>()
let scanState = PublishSubject<Bool>()
let scanTimeout = PublishSubject<Void>()
let scanDevices = BehaviorRelay<[SectionModel<String, TableViewCellModel>]>.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")
}
}