From: liangzhenying <867174124@qq.com> Date: Tue, 4 Jun 2024 09:30:40 +0000 (+0800) Subject: 支持分包发送 X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=91b8445b85f02c6441c05bfc04b9aa0bdb1a262d;p=mirrors%2FIOTBluetooth-iOS.git 支持分包发送 --- diff --git a/IOTBluetooth/IOTBluetooth/Example/Datas.swift b/IOTBluetooth/IOTBluetooth/Example/Datas.swift index a72c68a..566d552 100644 --- a/IOTBluetooth/IOTBluetooth/Example/Datas.swift +++ b/IOTBluetooth/IOTBluetooth/Example/Datas.swift @@ -85,7 +85,7 @@ let datas: [(ability: IBAbility, label: String, parameters: [IBParameter])] = [ (.syncTrain, "同步基础训练历史数据", [.value(30)]), (.syncGPS, "同步路径坐标集历史数据", [.value(1654012800)]), (.sendSyncEnd, "发送同步数据结束消息", []), - (.addressBook, "发送通讯录", [.addressBook(IBAddressBook.test())]), + (.addressBook, "发送通讯录", [.addressBook([IBAddressBook()])]), // (.revokeFileSend, "撤销当前文件传输", [.file(.mp3)]), // (.fetchFiles, "获取指定类型所有文件", [.file(.mp3)]), 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 943a344..69bbb2d 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/IBAbility.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBAbility.swift index 655ac55..a7565fc 100644 --- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBAbility.swift +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBAbility.swift @@ -205,8 +205,8 @@ public extension IBAbility { /// APP获取某表盘ID是否已安装 /// - Parameter value:Int /// - Returns: Int - /// 0 = 未安装 \ - /// 1 = 已安装 \ + /// 1 = 已安装 \ + /// 2 = 未安装 \ /// Read from ``IBResponse``.value static let checkDialID = IBAbility.long.id(0x0D).parser(IBCheckDialParser.self).checker(IBSwitchDialChecker.self) diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBManager.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBManager.swift index 765644a..bf98a16 100644 --- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBManager.swift +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBManager.swift @@ -20,6 +20,10 @@ public class IBManager { // DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) { // IBBluetooth.shared.reconnected() // } +// 发送天气数据 设备重启 +// 获取sd卡剩余空间 失败 +// 同步路径坐标集历史数据 无响应 +// 结束运动模式后立即发送其他指令 设备重启 } public static var logs: [String] { Logger.logs } diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBParser.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBParser.swift index 97e3ea5..a4d90d5 100644 --- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBParser.swift +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBParser.swift @@ -54,9 +54,8 @@ struct IBFetchAuxiliaryParser: IBParser { struct IBCheckDialParser: IBParser { static func parse(_ data: Data) -> IBResponse { - var bytes = data.bytes - bytes.removeLast() - let code = bytes.last!.int + + let code = dataPacket(data).int let desc = ["指令校验失败", "已安装", "未安装"] return .response.code(code != 0 ? .success:.failure).message(desc[code]).value(code) } diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBSender.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBSender.swift index d9527d8..39e96ff 100644 --- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBSender.swift +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBSender.swift @@ -9,9 +9,6 @@ import Foundation public typealias IBCompletionHandler = (_ result: IBResponse) -> Void -//public typealias IBCmdSuccess = (_ result: IBResponse) -> Void -//public typealias IBCmdError = (_ error: IBError) -> Void - /// 蓝牙消息发送类 public class IBSender { public init() { @@ -22,39 +19,83 @@ public class IBSender { var parameters: [IBParameter] = [] var completion: IBCompletionHandler! -// var cmdSuccess: IBCmdSuccess? -// var cmdFailure: IBCmdError? - - var data: [UInt8] = [] - - var dataPacket: [UInt8] { + var dataPacket: [[UInt8]] { + var totalData: [UInt8] = [] if isValue(ability.checker) { - data = ability.checker.check(parameters) + totalData = ability.checker.check(parameters) } + +// totalData = [] +// for i in 0...202{ +// totalData.append(UInt8(i)) +// } -// let data: [UInt8] = data?.bytes ?? [] - /// 分包数 - let packageCount = 1 - /// 数据包总长度 - let dataTotalLength = data.count - /// 数据包长度 - let dataLength = data.count - /// 数据包索引 - let index = 1 - - var bytes: [UInt8] = [0x73, ability.type.rawValue, ability.id] - bytes = bytes + packageCount.byte4 + dataTotalLength.byte4 + index.byte4 + dataLength.byte2 - - if data.count > 0 { - bytes = bytes + data - bytes = bytes + [LRC.calculateLRC(data)] - } else { - bytes = bytes + [0x00] + let list = splitData(totalData) + if list.count > 1{ + var newList = [[UInt8]]() + /// 分包数 + let packageCount = list.count + /// 数据包总长度 + let dataTotalLength = totalData.count + /// 数据包长度 + let dataLength = list[0].count + /// 数据包索引 + let index = 1 + + var bytes: [UInt8] = [0x73, ability.type.rawValue, ability.id] + bytes = bytes + packageCount.byte4 + dataTotalLength.byte4 + index.byte4 + dataLength.byte2 + bytes = bytes + list[0] + [LRC.calculateLRC(list[0])] + + newList.append(bytes) + newList.append(contentsOf: list[1...list.count - 1]) + + return newList + } + else{ + /// 分包数 + let packageCount = 1 + /// 数据包总长度 + let dataTotalLength = totalData.count + /// 数据包长度 + let dataLength = totalData.count + /// 数据包索引 + let index = 1 + + var bytes: [UInt8] = [0x73, ability.type.rawValue, ability.id] + bytes = bytes + packageCount.byte4 + dataTotalLength.byte4 + index.byte4 + dataLength.byte2 + + if totalData.count > 0 { + bytes = bytes + totalData + bytes = bytes + [LRC.calculateLRC(totalData)] + } else { + bytes = bytes + [0x00] + } + + return [bytes] } - - return bytes } + private func splitData(_ listData:[UInt8])->[[UInt8]]{ + let data = listData.data + var list = [[UInt8]]() + var index = 0 + let seg = 492 + while index < data.count { + if index < data.count - seg { + let sub:[UInt8] = data[index...index + seg - 1].bytes + list.append(sub) + index += sub.count + } + else{ + let sub:[UInt8] = data[index...data.count - 1].bytes + index += sub.count + list.append(sub) + } + } + return list + } + + func isAnswer(_ data: Data) -> Bool { return ability.isAnswer(data) } @@ -91,14 +132,6 @@ public extension IBSender { IBBluetooth.shared.send(self) return self } -// /// 执行与响应处理 -// @discardableResult -// func response(success: @escaping IBCmdSuccess,failure:@escaping IBCmdError) -> Self { -// cmdSuccess = success -// cmdFailure = failure -// IBBluetooth.shared.send(self) -// return self -// } } extension IBSender { diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBluetooth.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBluetooth.swift index 2ba56a0..5709399 100644 --- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBluetooth.swift +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/IBluetooth.swift @@ -132,18 +132,19 @@ class IBBluetooth: NSObject { lastSender = sender startTimer() - let dataPacket = sender.dataPacket - Logger.log("\(sender.ability.characteristicUUID!) Write:", dataPacket.data.joined(" "), "Bytes(\(dataPacket.count))") - - var writeCharacteristic: CBCharacteristic = writeCharacteristic - if sender.ability.characteristicUUID == writeBigCharacteristicUUID { - writeCharacteristic = writeBigCharacteristic - } - if sender.ability.characteristicUUID == writeFileCharacteristicUUID { - writeCharacteristic = writeFileCharacteristic + for dataPacket in sender.dataPacket { + Logger.log("\(sender.ability.characteristicUUID!) Write:", dataPacket.data.joined(" "), "Bytes(\(dataPacket.count))") + + var writeCharacteristic: CBCharacteristic = writeCharacteristic + if sender.ability.characteristicUUID == writeBigCharacteristicUUID { + writeCharacteristic = writeBigCharacteristic + } + if sender.ability.characteristicUUID == writeFileCharacteristicUUID { + writeCharacteristic = writeFileCharacteristic + } + Thread.sleep(forTimeInterval:0.02) + currentConnected.peripheral.writeValue(dataPacket.data, for: writeCharacteristic, type: .withResponse) } - - currentConnected.peripheral.writeValue(dataPacket.data, for: writeCharacteristic, type: .withResponse) } func sendDeviceInfo(){ @@ -361,7 +362,7 @@ extension IBBluetooth: CBPeripheralDelegate { return } - Logger.log(characteristic.uuid.uuidString, "write success") +// Logger.log(characteristic.uuid.uuidString, "write success") } } @@ -376,7 +377,7 @@ extension IBBluetooth { lastSender?.completion?(.response.code(.timeout)) lastSender?.remove() }) - Logger.log("创建定时器") +// Logger.log("创建定时器") } func deinitTimer() { @@ -384,6 +385,6 @@ extension IBBluetooth { Self.timer?.invalidate() Self.timer = nil - Logger.log("销毁定时器") +// Logger.log("销毁定时器") } } diff --git a/IOTBluetooth/IOTBluetooth/IOTBluetooth/Models/IBAddressBook.swift b/IOTBluetooth/IOTBluetooth/IOTBluetooth/Models/IBAddressBook.swift index fef7b43..c76cad3 100644 --- a/IOTBluetooth/IOTBluetooth/IOTBluetooth/Models/IBAddressBook.swift +++ b/IOTBluetooth/IOTBluetooth/IOTBluetooth/Models/IBAddressBook.swift @@ -20,25 +20,25 @@ public class IBAddressBook { public init() { } - public static func test() -> [IBAddressBook]{ - var list = [IBAddressBook](); - for i in 0...10{ - let model = IBAddressBook() - model.gender = i%2 - model.name = "å¼ \(i)" - - for j in 0...1{ - let phone = IBPhoneNumber() - phone.number = "1300000000\(j)" - phone.location = "深圳市宝安区\(j)" - phone.operator = "中国移动" - model.phoneNumbers.append(phone) - } - list.append(model) - } - - return list - } +// public static func test() -> [IBAddressBook]{ +// var list = [IBAddressBook](); +// for i in 0...10{ +// let model = IBAddressBook() +// model.gender = i%2 +// model.name = "å¼ \(i)" +// +// for j in 0...1{ +// let phone = IBPhoneNumber() +// phone.number = "1300000000\(j)" +// phone.location = "深圳市宝安区\(j)" +// phone.operator = "中国移动" +// model.phoneNumbers.append(phone) +// } +// list.append(model) +// } +// +// return list +// } public var description: String { var phoneStr = "[ "