123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- //
- // 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<UITouch>, 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()
- }
- }
- }
- }
|