BluetoothManager+OTA.swift 3.1 KB
//
//  BluetoothManager+OTA.swift
//  HDFwear
//
//  Created by admin on 2024/1/23.
//

import Foundation

extension BluetoothManager {
    // 开始ota需要做的几个动作
    // 生成一个新的ota的页面,在该页面内做ota的相关行为,传入一些必要参数,当前的peripheral,otaManager,ota文件path,以及关闭页面的回调
    // 设置isOTAUpdating,discoverServices扫描当前外设的服务,并在回调中进行设置,该动作触发手表进入固件升级状态
    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)")
    }

}