文件传输协议修改
authorliangzhenying <867174124@qq.com>
Tue, 5 Nov 2024 10:00:40 +0000 (18:00 +0800)
committerliangzhenying <867174124@qq.com>
Tue, 5 Nov 2024 10:00:40 +0000 (18:00 +0800)
IOTBluetooth/IOTBluetooth/Example/AbilityViewController.swift
IOTBluetooth/IOTBluetooth/IOTBluetooth/IBAbility.swift
IOTBluetooth/IOTBluetooth/IOTBluetooth/IBFileSender+Dial.swift
IOTBluetooth/IOTBluetooth/IOTBluetooth/IBFileSender+Mp3.swift
IOTBluetooth/IOTBluetooth/IOTBluetooth/IBFileSender+Ota.swift
IOTBluetooth/IOTBluetooth/IOTBluetooth/IBFileSender.swift
IOTBluetooth/IOTBluetooth/IOTBluetooth/IBNotify.swift
IOTBluetooth/IOTBluetooth/IOTBluetooth/IBReceiveManager.swift
IOTBluetooth/IOTBluetooth/IOTBluetooth/IBluetooth.swift

index 5265b15..aca7796 100644 (file)
@@ -104,7 +104,7 @@ extension AbilityViewController{
         
         activityIndicatorView.startAnimating()
         IBFileSender().mp3(info).progress({ total, current in
-            print("\(total) : \(current)")
+//            print("\(total) : \(current)")
         }).response {[weak self]  result in
             print(result)
             self?.activityIndicatorView.stopAnimating()
@@ -155,7 +155,7 @@ extension AbilityViewController{
         info.UID = 10002
         info.files = [file]
         
-        info.theme = "黑暗"
+        info.theme = "黑暗001"
         
 //        activityIndicatorView.startAnimating()
         IBFileSender().dial(info).progress({ total, current in
index 9fde543..68a1dbd 100644 (file)
@@ -75,7 +75,7 @@ extension IBAbility {
         /// 长指令
         case long = 0x02
         /// 文件指令(用来发送文件,比如表盘、MP3、OTA等)
-        case file = 0x04
+        case file = 0x06
         /// Ai指令
         case ai = 0x05
     }
@@ -444,7 +444,7 @@ public extension IBAbility {
     
     /// 发送MP3文件信息
     /// - Parameter mp3: see ``IBMp3File``
-    static let mp3 = IBAbility.file.id(0x21).parser(IBGeneralParser.self).checker(IBMp3Checker.self)
+    static let mp3 = IBAbility.file.id(0x07).parser(IBGeneralParser.self).checker(IBMp3Checker.self)
     
 //    /// 发送自定义表盘文件汇总信息
 //    /// - Parameter customDial: see ``IBCustomDial``
@@ -452,15 +452,15 @@ public extension IBAbility {
     
     /// 发送表盘文件汇总信息
     /// - Parameter dial: see ``IBDial``
-    static let dial = IBAbility.file.id(0x22).parser(IBGeneralParser.self).checker(IBDialChecker.self)
+    static let dial = IBAbility.file.id(0x05).parser(IBGeneralParser.self).checker(IBDialChecker.self)
     
     /// 发送单个表盘文件信息
     /// - Parameter dialFile: see ``IBDailFile``
-    internal static let dialFile = IBAbility.file.id(0x24).parser(IBGeneralParser.self).checker(IBDialFileChecker.self)
+    internal static let dialFile = IBAbility.file.id(0x06).parser(IBGeneralParser.self).checker(IBDialFileChecker.self)
     
     /// 发送文件校验码
     /// - Parameter value:Int
-    internal static let fileCheckCode = IBAbility.file.id(0x23).parser(IBGeneralParser.self).checker(IBValueChecker.self)
+    internal static let fileCheckCode = IBAbility.file.id(0x08).parser(IBGeneralParser.self).checker(IBValueChecker.self)
     
     /// 删除指定文件
     /// - Parameter deleteFile: see ``IBDeleteFile``
@@ -472,7 +472,7 @@ public extension IBAbility {
     
     /// 发送OTA文件信息
     /// - Parameter ota: see ``IBOtaFile``
-    static let ota = IBAbility.file.id(0x20).parser(IBGeneralParser.self).checker(IBOtaChecker.self)
+    static let ota = IBAbility.file.id(0x01).parser(IBGeneralParser.self).checker(IBOtaChecker.self)
     
 
     /// 发送EPO文件信息
index ad202dd..ede2f01 100644 (file)
@@ -84,7 +84,7 @@ extension IBFileSender{
                 guard let self = self else { return }
                 self.lastData = nil
                 if result.isSuccess {
-                    self.current += data.count
+                    self.current += data.count - 18
                     Logger.log("\(total) : \(current)")
                     self.progress?(self.total,self.current)
                     self.sendContentDial()
index 0090746..f8de145 100644 (file)
@@ -51,7 +51,7 @@ extension IBFileSender{
                 guard let self = self else { return }
                 self.lastData = nil
                 if result.isSuccess {
-                    self.current += data.count
+                    self.current += data.count - 18
                     Logger.log("\(total) : \(current)")
                     self.progress?(self.total,self.current)
                     self.sendContentMp3()
index 79e4d7d..0df56a4 100644 (file)
@@ -50,7 +50,7 @@ extension IBFileSender{
                 guard let self = self else { return }
                 self.lastData = nil
                 if result.isSuccess {
-                    self.current += data.count
+                    self.current += data.count - 18
                     Logger.log("\(total) : \(current)")
                     self.progress?(self.total,self.current)
                     self.sendContentOta()
index f36d7bf..4a8b354 100644 (file)
@@ -107,11 +107,32 @@ extension IBFileSender{
         //            filePath = path
         //        }
     }
+//    func readFile(from path:String) ->([Data]?,Int,Int){
+//        let fileURL = URL(fileURLWithPath: path)
+//        do {
+//            let data = try Data(contentsOf:fileURL)
+//            let seg = 495
+//            var list = [Data]()
+//            for i in stride(from: 0, to: data.count, by: seg) {
+//                if i+seg < data.count {
+//                    list.append(data[i ... i+seg - 1])
+//                }
+//                else{
+//                    list.append(data[i ... data.count - 1])
+//                }
+//            }
+//            let lrc = LRC.calculateLRC(data.bytes).int
+//            return (list,data.count,lrc)
+//        } catch {
+//            return (nil,0,0)
+//        }
+//    }
+    
     func readFile(from path:String) ->([Data]?,Int,Int){
         let fileURL = URL(fileURLWithPath: path)
         do {
             let data = try Data(contentsOf:fileURL)
-            let seg = 495
+            let seg = 492
             var list = [Data]()
             for i in stride(from: 0, to: data.count, by: seg) {
                 if i+seg < data.count {
@@ -121,8 +142,27 @@ extension IBFileSender{
                     list.append(data[i ... data.count - 1])
                 }
             }
+            
+            var fullList = [Data]();
+            for i in 0..<list.count {
+                /// 分包数
+                let packageCount = list.count
+                /// 数据包总长度
+                let dataTotalLength = data.count
+                /// 数据包长度
+                let dataLength = list[i].count
+                /// 数据包索引
+                let index = i
+                
+                var bytes: [UInt8] = [0x73, 0x06, 0x02]
+                bytes = bytes + packageCount.byte4 + dataTotalLength.byte4 + index.byte4 + dataLength.byte2
+                bytes = bytes + list[i].bytes + [LRC.calculateLRC(list[i].bytes)]
+                
+                fullList.append(bytes.data)
+            }
+            
             let lrc = LRC.calculateLRC(data.bytes).int
-            return (list,data.count,lrc)
+            return (fullList,data.count,lrc)
         } catch {
             return (nil,0,0)
         }
index 5947d60..032e093 100644 (file)
@@ -197,7 +197,7 @@ extension IBNotify {
             let result = IBGeneralParser.dataPacket(data)
             let code = result.int
             let desc = ["安装失败","安装成功"]
-            Logger.log("表盘安装状态:",desc[code])
+            Logger.log("安装状态:",desc[code])
             return true
         }
         //手表询问APP AI模型是否可用
@@ -211,7 +211,7 @@ extension IBNotify {
             return true
         }
         //文件已接收完成
-        if data.bytes[1] == 0x05 && data.bytes[2] == 0x20 {
+        if data.bytes[1] == 0x06 && data.bytes[2] == 0x03 {
             let result = IBGeneralParser.dataPacket(data)
             IBBluetooth.shared.fileSender?.sendFileResult(result[0].int == 1)
             return true
index 4c17cac..a326f2a 100644 (file)
@@ -23,6 +23,8 @@ class IBReceiveManager{
     
     
     func receice(characteristic: CBCharacteristic){
+        
+        
         guard let data = characteristic.value else { return }
         if(data.count == 2 && data.int == 0){
 //            Logger.log("收到0000指令异常")
index f63c91c..dda7dc8 100644 (file)
@@ -45,7 +45,8 @@ let writeCharacteristicUUID: String = "2AE3"
 /// Write 特征值(发送字节数>20)
 let writeBigCharacteristicUUID: String = "2AE4"
 /// Write 特征值(发送文件OAT、MP3等)
-let writeFileCharacteristicUUID: String = "2AE0"
+let writeFileCharacteristicUUID: String = "2AE6"
+let writeFileResponseUUID: String = "2AE7"
 
 class IBBluetooth: NSObject {
     static let shared = IBBluetooth()
@@ -293,6 +294,11 @@ extension IBBluetooth: CBPeripheralDelegate {
                 
                 if $0.uuid.uuidString == writeFileCharacteristicUUID {
                     writeFileCharacteristic = $0
+                    peripheral.setNotifyValue(true, for: $0)
+                    return
+                }
+                if $0.uuid.uuidString == writeFileResponseUUID {
+                    peripheral.setNotifyValue(true, for: $0)
                     return
                 }
             }
@@ -304,6 +310,9 @@ extension IBBluetooth: CBPeripheralDelegate {
         if let error = error {
             Logger.log(error)
         }
+        if let data = characteristic.value {
+            Logger.log( "[receive] \(characteristic.uuid.uuidString)" + data.joined(" "))
+        }
         receiveManager.receice(characteristic: characteristic)
     }