// // SBTAddDeviceVCtr.swift // SolarBT // // Created by weclouds on 2019/2/28. // Copyright © 2019 weclouds. All rights reserved. // import UIKit import SwiftPopup import CoreBluetooth import PKHUD class SBTAddDeviceVCtr: SwiftPopup { var baby :BabyBluetooth? var langua : String? // @IBOutlet weak var addDeviceLabel: UILabel! // @IBOutlet weak var reScanBtn: UIButton! // @IBOutlet weak var closeBtn: UIButton! @IBOutlet weak var closeBtn: UIButton! @IBOutlet weak var reScanBtn: UIButton! @IBOutlet weak var addDeviceLabel: UILabel! @IBOutlet weak var tableView: UITableView! var contentArray :[String] = [""] var selectionIndex:Int? var completionCallback:((String)->Void)? override func viewDidLoad() { super.viewDidLoad() self.tableView.reloadData() checkTheBlueToothWasActivity() let array = BabyBluetoothSwift.shareInstance()?.peripherals log.debug(array) createUI() NotificationCenter.default.addObserver(self, selector: #selector(SBTAddDeviceVCtr.scanResultCallBack), name: NSNotification.Name(rawValue: kBabyBluetoothNotificationScanResult), object: nil) loadFromBabyBluetooth() //babyDelegate() } override func viewWillAppear(_ animated: Bool) { super.viewWillDisappear(animated) BabyBluetoothSwift.shareInstance()?.scanForPeripherals() } override func touchesBegan(_ touches: Set, with event: UIEvent?) { dismiss() BabyBluetoothSwift.shareInstance()?.cancelScan() } @IBAction func recontect(_ sender: Any) { rescan() } func rescan() { BabyBluetoothSwift.shareInstance()?.cancelScan() contentArray.removeAll() self.tableView.reloadData() delay(0.25) { BabyBluetoothSwift.shareInstance()?.scanForPeripherals() self.loadFromBabyBluetooth() } } @objc func delayload() { } @IBAction func close(_ sender: Any) { dismiss() BabyBluetoothSwift.shareInstance()?.cancelScan() } } extension SBTAddDeviceVCtr{ func createUI() { self.tableView.delegate = self self.tableView.dataSource = self self.tableView.register(UINib.init(nibName: "SBTAddDeviceCell", bundle: nil), forCellReuseIdentifier: "cell") } func loadFromBabyBluetooth() { contentArray.removeAll() var keys = [String]() let peripherals = BabyBluetoothSwift.shareInstance().peripherals as! [CBPeripheral] if peripherals.count > 0 { for peripheral in peripherals { let perpheralname = peripheral.name log.debug("搜索到的设备 ---\(perpheralname)") let devicetype0 = String((perpheralname?.prefix(1))!) //过滤数组 if devicetype0 == "0" || devicetype0 == "1" || devicetype0 == "3" || devicetype0 == "4" || devicetype0 == "5" && devicetype0.count == 16{ keys.append(peripheral.name!) }else{ if (perpheralname?.hasPrefix("BT-TH"))!{ keys.append(peripheral.name!) } } } log.debug("包含的数组 --- \(keys)") contentArray = keys } self.tableView.reloadData() } @objc func scanResultCallBack() { loadFromBabyBluetooth() } } extension SBTAddDeviceVCtr: UITableViewDelegate,UITableViewDataSource{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return contentArray.count } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 59 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell :SBTAddDeviceCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! SBTAddDeviceCell cell.bleName = contentArray[indexPath.row] if cell.bleName?.count == 0 { cell.connectBtn.setTitle("", for: .normal) }else{ cell.connectBtn.setTitle("Connect", for: .normal) if self.langua == "cn"{ cell.connectBtn.setTitle("连接", for: .normal) }else if langua == "en"{ cell.connectBtn.setTitle("Connect", for: .normal) } } cell.selectionStyle = .none cell.connectCallback = { log.debug("开始连接") } return cell } @objc func tapdismiss() { HUD.flash(.error, delay: 1) //self.dismiss() } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { var __isSuccess = false //判断有没有结果 HUD.show(.progress) HUD.dimsBackground = true PKHUD.sharedHUD.contentView.isUserInteractionEnabled = true let tap = UITapGestureRecognizer(target: self, action: #selector(tapdismiss)) PKHUD.sharedHUD.contentView.addGestureRecognizer(tap) log.debug("当前数组-----\(BabyBluetoothSwift.shareInstance()?.peripherals)") self.selectionIndex = indexPath.row let peripheral = contentArray[indexPath.row] if BabyBluetoothSwift.shareInstance()?.currPeripheral != nil { log.debug( "cururenttPeripheral -- \(BabyBluetoothSwift.shareInstance()?.currPeripheral.name ?? "当前没有设备")") BabyBluetoothSwift.shareInstance()?.cancelAllPeripheralsConnection() delay(1) { //开始连接 BabyBluetoothSwift.shareInstance()?.connectPeripheral(withName: peripheral) BabyBluetoothSwift.shareInstance()?.connectPeripheral(withName: peripheral, connectCompletionHandler: { (perih, isSuccess) in if isSuccess == true { HUD.flash(.success, delay: 1.0) log.debug("连接成功 iiii \(perih?.name)") __isSuccess = true if let peripheralname = perih?.name{ self.completionCallback!(peripheralname) } self.dismiss() }else{ HUD.flash(.error, delay: 1) log.debug("连接失败 iiii \(perih?.name)") __isSuccess = true self.rescan() // self.dismiss() } }) } }else{ log.debug(peripheral) //开始连接 // BabyBluetoothSwift.shareInstance()?.connectPeripheral(withName: peripheral) BabyBluetoothSwift.shareInstance()?.connectPeripheral(withName: peripheral, connectCompletionHandler: { (perih, isSuccess) in if isSuccess == true { HUD.flash(.success, delay: 1.0) log.debug("连接成功 iiii \(perih?.name)") __isSuccess = true self.dismiss() if let peripheralname = perih?.name{ self.completionCallback!(peripheralname) } }else{ HUD.flash(.error, delay: 1) log.debug("连接失败 iiii \(perih?.name)") __isSuccess = true // self.dismiss() self.rescan() } }) } delay(30) { log.debug("连接超时") if __isSuccess == false { HUD.flash(.error, delay: 1) self.dismiss() } } } }