SBTAddDeviceVCtr.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // SBTAddDeviceVCtr.swift
  3. // SolarBT
  4. //
  5. // Created by weclouds on 2019/2/28.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import SwiftPopup
  10. import CoreBluetooth
  11. import PKHUD
  12. class SBTAddDeviceVCtr: SwiftPopup {
  13. var baby :BabyBluetooth?
  14. var langua : String?
  15. // @IBOutlet weak var addDeviceLabel: UILabel!
  16. // @IBOutlet weak var reScanBtn: UIButton!
  17. // @IBOutlet weak var closeBtn: UIButton!
  18. @IBOutlet weak var closeBtn: UIButton!
  19. @IBOutlet weak var reScanBtn: UIButton!
  20. @IBOutlet weak var addDeviceLabel: UILabel!
  21. @IBOutlet weak var tableView: UITableView!
  22. var contentArray :[String] = [""]
  23. var selectionIndex:Int?
  24. var completionCallback:((String)->Void)?
  25. override func viewDidLoad() {
  26. super.viewDidLoad()
  27. self.tableView.reloadData()
  28. checkTheBlueToothWasActivity()
  29. let array = BabyBluetoothSwift.shareInstance()?.peripherals
  30. log.debug(array)
  31. createUI()
  32. NotificationCenter.default.addObserver(self, selector: #selector(SBTAddDeviceVCtr.scanResultCallBack), name: NSNotification.Name(rawValue: kBabyBluetoothNotificationScanResult), object: nil)
  33. loadFromBabyBluetooth()
  34. //babyDelegate()
  35. }
  36. override func viewWillAppear(_ animated: Bool) {
  37. super.viewWillDisappear(animated)
  38. BabyBluetoothSwift.shareInstance()?.scanForPeripherals()
  39. }
  40. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  41. dismiss()
  42. BabyBluetoothSwift.shareInstance()?.cancelScan()
  43. }
  44. @IBAction func recontect(_ sender: Any) {
  45. rescan()
  46. }
  47. func rescan() {
  48. BabyBluetoothSwift.shareInstance()?.cancelScan()
  49. contentArray.removeAll()
  50. self.tableView.reloadData()
  51. delay(0.25) {
  52. BabyBluetoothSwift.shareInstance()?.scanForPeripherals()
  53. self.loadFromBabyBluetooth()
  54. }
  55. }
  56. @objc func delayload() {
  57. }
  58. @IBAction func close(_ sender: Any) {
  59. dismiss()
  60. BabyBluetoothSwift.shareInstance()?.cancelScan()
  61. }
  62. }
  63. extension SBTAddDeviceVCtr{
  64. func createUI() {
  65. self.tableView.delegate = self
  66. self.tableView.dataSource = self
  67. self.tableView.register(UINib.init(nibName: "SBTAddDeviceCell", bundle: nil), forCellReuseIdentifier: "cell")
  68. }
  69. func loadFromBabyBluetooth() {
  70. contentArray.removeAll()
  71. var keys = [String]()
  72. let peripherals = BabyBluetoothSwift.shareInstance().peripherals as! [CBPeripheral]
  73. if peripherals.count > 0 {
  74. for peripheral in peripherals {
  75. let perpheralname = peripheral.name
  76. log.debug("搜索到的设备 ---\(perpheralname)")
  77. let devicetype0 = String((perpheralname?.prefix(1))!)
  78. //过滤数组
  79. if devicetype0 == "0" || devicetype0 == "1" || devicetype0 == "3" || devicetype0 == "4" || devicetype0 == "5" && devicetype0.count == 16{
  80. keys.append(peripheral.name!)
  81. }else{
  82. if (perpheralname?.hasPrefix("BT-TH"))!{
  83. keys.append(peripheral.name!)
  84. }
  85. }
  86. }
  87. log.debug("包含的数组 --- \(keys)")
  88. contentArray = keys
  89. }
  90. self.tableView.reloadData()
  91. }
  92. @objc func scanResultCallBack() {
  93. loadFromBabyBluetooth()
  94. }
  95. }
  96. extension SBTAddDeviceVCtr: UITableViewDelegate,UITableViewDataSource{
  97. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  98. return contentArray.count
  99. }
  100. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  101. return 59
  102. }
  103. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  104. let cell :SBTAddDeviceCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! SBTAddDeviceCell
  105. cell.bleName = contentArray[indexPath.row]
  106. if cell.bleName?.count == 0 {
  107. cell.connectBtn.setTitle("", for: .normal)
  108. }else{
  109. cell.connectBtn.setTitle("Connect", for: .normal)
  110. if self.langua == "cn"{
  111. cell.connectBtn.setTitle("连接", for: .normal)
  112. }else if langua == "en"{
  113. cell.connectBtn.setTitle("Connect", for: .normal)
  114. }
  115. }
  116. cell.selectionStyle = .none
  117. cell.connectCallback = {
  118. log.debug("开始连接")
  119. }
  120. return cell
  121. }
  122. @objc func tapdismiss() {
  123. HUD.flash(.error, delay: 1)
  124. //self.dismiss()
  125. }
  126. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  127. var __isSuccess = false //判断有没有结果
  128. HUD.show(.progress)
  129. HUD.dimsBackground = true
  130. PKHUD.sharedHUD.contentView.isUserInteractionEnabled = true
  131. let tap = UITapGestureRecognizer(target: self, action: #selector(tapdismiss))
  132. PKHUD.sharedHUD.contentView.addGestureRecognizer(tap)
  133. log.debug("当前数组-----\(BabyBluetoothSwift.shareInstance()?.peripherals)")
  134. self.selectionIndex = indexPath.row
  135. let peripheral = contentArray[indexPath.row]
  136. if BabyBluetoothSwift.shareInstance()?.currPeripheral != nil {
  137. log.debug( "cururenttPeripheral -- \(BabyBluetoothSwift.shareInstance()?.currPeripheral.name ?? "当前没有设备")")
  138. BabyBluetoothSwift.shareInstance()?.cancelAllPeripheralsConnection()
  139. delay(1) {
  140. //开始连接
  141. BabyBluetoothSwift.shareInstance()?.connectPeripheral(withName: peripheral)
  142. BabyBluetoothSwift.shareInstance()?.connectPeripheral(withName: peripheral, connectCompletionHandler: { (perih, isSuccess) in
  143. if isSuccess == true {
  144. HUD.flash(.success, delay: 1.0)
  145. log.debug("连接成功 iiii \(perih?.name)")
  146. __isSuccess = true
  147. if let peripheralname = perih?.name{
  148. self.completionCallback!(peripheralname)
  149. }
  150. self.dismiss()
  151. }else{
  152. HUD.flash(.error, delay: 1)
  153. log.debug("连接失败 iiii \(perih?.name)")
  154. __isSuccess = true
  155. self.rescan()
  156. // self.dismiss()
  157. }
  158. })
  159. }
  160. }else{
  161. log.debug(peripheral)
  162. //开始连接
  163. // BabyBluetoothSwift.shareInstance()?.connectPeripheral(withName: peripheral)
  164. BabyBluetoothSwift.shareInstance()?.connectPeripheral(withName: peripheral, connectCompletionHandler: { (perih, isSuccess) in
  165. if isSuccess == true {
  166. HUD.flash(.success, delay: 1.0)
  167. log.debug("连接成功 iiii \(perih?.name)")
  168. __isSuccess = true
  169. self.dismiss()
  170. if let peripheralname = perih?.name{
  171. self.completionCallback!(peripheralname)
  172. }
  173. }else{
  174. HUD.flash(.error, delay: 1)
  175. log.debug("连接失败 iiii \(perih?.name)")
  176. __isSuccess = true
  177. // self.dismiss()
  178. self.rescan()
  179. }
  180. })
  181. }
  182. delay(30) {
  183. log.debug("连接超时")
  184. if __isSuccess == false {
  185. HUD.flash(.error, delay: 1)
  186. self.dismiss()
  187. }
  188. }
  189. }
  190. }