Blame view

HDFwear/Tools/BluetoothManager+OTA.swift 3.1 KB
671a3d9e   jason   refactor:replace ...
1
2
3
4
5
6
7
8
  //
  //  BluetoothManager+OTA.swift
  //  HDFwear
  //
  //  Created by admin on 2024/1/23.
  //
  
  import Foundation
7a10e2ea   jason   feat:BluetoothMan...
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  
  extension BluetoothManager {
      // 开始ota需要做的几个动作
      // 生成一个新的ota的页面在该页面内做ota的相关行为传入一些必要参数当前的peripheralotaManagerota文件path以及关闭页面的回调
      // 设置isOTAUpdatingdiscoverServices扫描当前外设的服务并在回调中进行设置该动作触发手表进入固件升级状态
      func startOtaUpdate() {
          let a = OTAUpdateViewController(nibName: "OTAUpdateViewController", bundle: nil)
          a.currentPeripheral = peripheral
          a.otaManager = otaManager
          a.closeBlock = {[weak self] in
              self?.isOTAUpdating = false
              self?.otaTransceiver.closeOTA()
              self?.disconnect()
              self?.reConnect()
          }
          let docPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
          print("nick_path: \(docPath)")
          let fullPath = (docPath as NSString).appendingPathComponent("QW230288A_HW_V1.5_0119.bin")
          print(fullPath)
          a.path = fullPath
          getCurrentVC().navigationController?.pushViewController(a, animated: true)
          
          isOTAUpdating = true
          peripheral?.discoverServices(nil)
      }
  
  }
  
  extension BluetoothManager: LEConnctionDelegate {
      func onPeripheralOtaConnected(_ peripheral: CBPeripheral) {
          let nc = NotificationCenter.default
          nc.post(name: Notification.Name("kMsgPeripheralConnected"), object: self, userInfo: nil)
  
          let mtu = peripheral.maximumWriteValueLength(for: .withoutResponse)
          print("mtu view:%d", mtu);
          otaManager.setMaxMtu(mtu)
      }
  }
  
  extension BluetoothManager: OTAManagerDelegate {
      func send(_ data: Data) {
          otaTransceiver.write(data)
      }
  
      func send(_ data: Data, index i: Int32) {
          otaTransceiver.write(data)
      }
  
      func receiveAudioPSN(_ psn: Int, data: Data) {
          print("nick_receiveAudioPSN")
          var dic = [String: Any]()
          dic["psn"] = psn
          dic["data"] = data
          NotificationCenter.default.post(name: NSNotification.Name(kMsgAudioPackageRecevied), object: self, userInfo: dic)
      }
  
      func receiveSpeed(_ speed: Int) {
          var dic = [String: Any]()
          dic["speed"] = speed
          NotificationCenter.default.post(name: NSNotification.Name(kMsgAudioSpeedRecevied), object: self, userInfo: dic)
      }
  
      func receive(_ status: RemoteStatus) {
          print("receiveRemoteStatus: \(status), \(String(describing: status.versionName)), \(String(describing: status.boardName))")
          var dic = [String: Any]()
          dic["status"] = status
          NotificationCenter.default.post(name: NSNotification.Name(kMsgRemoteStatusRecevied), object: self, userInfo: dic)
      }
  
      func onStatus(_ state: OTAStatus) {
          let dic = ["status": state.rawValue]
          NotificationCenter.default.post(name: NSNotification.Name(kMsgOTAStatus), object: self, userInfo: dic)
      }
  
      func onError(_ errCode: Int) {
          print("onError: \(errCode)")
      }
  
  }