From: liangzhenying <867174124@qq.com> Date: Wed, 10 Jul 2024 08:45:10 +0000 (+0800) Subject: 文件传输过程断开连接则取消发送 X-Git-Url: http://git.snicker.com.cn/Users/apple/Desktop/static/gitweb.css?a=commitdiff_plain;h=21783b1603067cf8bf7750d580f3225649dadcac;p=mirrors%2FIOTBluetooth-iOS.git 文件传输过程断开连接则取消发送 --- diff --git a/IOTBluetooth/IOTBluetooth/Example/AbilityViewController.swift b/IOTBluetooth/IOTBluetooth/Example/AbilityViewController.swift index 938dda3..b0c79bb 100644 --- a/IOTBluetooth/IOTBluetooth/Example/AbilityViewController.swift +++ b/IOTBluetooth/IOTBluetooth/Example/AbilityViewController.swift @@ -90,7 +90,7 @@ extension AbilityViewController{ activityIndicatorView.startAnimating() IBFileSender().ota(info).progress({ total, current in - print("\(total) : \(current)") +// print("\(total) : \(current)") }).response {[weak self] result in print(result) self?.activityIndicatorView.stopAnimating() diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate b/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate index 8ccc617..907e8e1 100644 Binary files a/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate and b/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index 392f79f..2e40ded 100644 --- a/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -39,16 +39,16 @@ diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBFileSender+Dial.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBFileSender+Dial.swift index 59a7b1b..ad202dd 100644 --- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBFileSender+Dial.swift +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBFileSender+Dial.swift @@ -85,6 +85,7 @@ extension IBFileSender{ self.lastData = nil if result.isSuccess { self.current += data.count + Logger.log("\(total) : \(current)") self.progress?(self.total,self.current) self.sendContentDial() } diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBFileSender+Mp3.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBFileSender+Mp3.swift index 8e196db..0090746 100644 --- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBFileSender+Mp3.swift +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBFileSender+Mp3.swift @@ -52,6 +52,7 @@ extension IBFileSender{ self.lastData = nil if result.isSuccess { self.current += data.count + Logger.log("\(total) : \(current)") self.progress?(self.total,self.current) self.sendContentMp3() } diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBFileSender+Ota.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBFileSender+Ota.swift index 68d090a..79e4d7d 100644 --- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBFileSender+Ota.swift +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBFileSender+Ota.swift @@ -51,6 +51,7 @@ extension IBFileSender{ self.lastData = nil if result.isSuccess { self.current += data.count + Logger.log("\(total) : \(current)") self.progress?(self.total,self.current) self.sendContentOta() } diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBManager.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBManager.swift index e51b97b..392dcd7 100644 --- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBManager.swift +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBManager.swift @@ -31,7 +31,7 @@ public class IBManager { public extension IBManager { /// SDK 版本号 - static let version: String = "1.0.17" + static let version: String = "1.0.18" /// 默认为 false static var isDebug: Bool = false { didSet { Logger.isDebug = isDebug } diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBluetooth.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBluetooth.swift index 0a7756c..dcf57ab 100644 --- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBluetooth.swift +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBluetooth.swift @@ -134,6 +134,13 @@ class IBBluetooth: NSObject { /// - Parameters: /// - sender: 消息类 func send(_ sender: IBSender) { + if currentConnected.peripheral.state == .disconnected{ + sender.completion(.response.code(.failure).message("手机未连上蓝牙设备")) + sender.remove() + Logger.log("手机未连上蓝牙设备!") + return + } + if let _ = lastSender { sender.completion(.response.code(.failure).message("有命令正在执行中...")) sender.remove() @@ -160,14 +167,18 @@ class IBBluetooth: NSObject { } func send(fileData data: Data) { // Thread.sleep(forTimeInterval:0.02) + if currentConnected.peripheral.state == .disconnected{ + self.fileSender?.lastData?.fileWriteHander?(.response.code(.failure).message("手机未连上蓝牙设备")) + Logger.log("手机未连上蓝牙设备!") + return + } + startTimer() Logger.log("\(writeFileCharacteristicUUID) Write:", data) currentConnected.peripheral.writeValue(data, for: writeFileCharacteristic, type: .withResponse) } func sendDeviceInfo(){ -// IBNotify.post(.authStateDidChange, userInfo: ["state": 0]) - let mac = UserDefaults.standard.object(forKey: "IOTBletooth.mac") as? [Int] ?? [] let model = IBPhoneInfo() model.mac = mac @@ -242,7 +253,7 @@ extension IBBluetooth: CBCentralManagerDelegate { device.macAddress = manufacturer.macAddress device.softwareVersion = manufacturer.softwareVersion IBManager.discoverHandler?(device) - Logger.log("发现设备", device.name, device.macAddress) +// Logger.log("发现设备", device.name, device.macAddress) } /// 连接上设备 @@ -272,7 +283,8 @@ extension IBBluetooth: CBCentralManagerDelegate { sender.completion?(.response.code(.failure)) sender.remove() } - + + self.fileSender?.lastData?.fileWriteHander?(.response.code(.failure).message("手机未连上蓝牙设备")) IBManager.completeHandler?(false) IBManager.completeHandler = nil @@ -402,7 +414,7 @@ extension IBBluetooth { func startTimer() { deinitTimer() - Self.timer = Timer.scheduledTimer(withTimeInterval: 10.0, repeats: false, block: { [unowned self] _ in + Self.timer = Timer.scheduledTimer(withTimeInterval: 3.0, repeats: false, block: { [unowned self] _ in deinitTimer() if let sender = lastSender{ lastSender = nil diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/Models/IBHistorySleep.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/Models/IBHistorySleep.swift index aad33d4..7f04ee0 100644 --- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/Models/IBHistorySleep.swift +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/Models/IBHistorySleep.swift @@ -15,7 +15,6 @@ import Foundation if(data.count <= 0){ return } -// 72 01 4b 02 00 00 00 47 00 00 00 01 00 00 00 3e 00 05 0c 00 01 00 1a 49 7e 66 2a 57 7e 66 64 00 0c 00 02 00 ed 53 7e 66 ed 53 7e 66 39 00 0c 00 03 00 28 1e 7e 66 38 2c 7e 66 0a 00 0c 00 04 00 f9 3f 7e 66 f9 3f 7e 66 2a 00 0c 00 05 00 e6 5b var historyList:[IBHistorySleepItem] = [] let times = data[0].int let contentData:Data = data.suffix(from: 1).map({ $0 }).data diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/Tools/Logger.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/Tools/Logger.swift index 2e3aba4..ef2de69 100644 --- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/Tools/Logger.swift +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/Tools/Logger.swift @@ -46,13 +46,13 @@ class Logger { extension Logger { static func log(_ logs: Any..., line: Int = #line, separator: String = " ", terminator: String = "\n") { objc_sync_enter(Logger.shared) + let logString = Logger.shared.dateString + " [IOTBluetooth-\(String(format: "%03d", line))]: " + logs.map({ "\($0)" }).joined(separator: separator) + terminator if Logger.isDebug { print(Logger.shared.dateString, terminator: " [IOTBluetooth-\(String(format: "%03d", line))]: ") print(logs, separator: separator, terminator: terminator) - let logString = Logger.shared.dateString + " [IOTBluetooth-\(String(format: "%03d", line))]: " + logs.map({ "\($0)" }).joined(separator: separator) + terminator Self.logs.append(logString) - Logger.shared.saveLog(log: logString) } + Logger.shared.saveLog(log: logString) objc_sync_exit(Logger.shared)