设置屏幕协议修改
authorliangzhenying <867174124@qq.com>
Tue, 2 Jul 2024 06:24:25 +0000 (14:24 +0800)
committerliangzhenying <867174124@qq.com>
Tue, 2 Jul 2024 06:24:25 +0000 (14:24 +0800)
IOTBluetooth/IOTBluetooth/Example/AbilityViewController.swift
IOTBluetooth/IOTBluetooth/Example/Datas.swift
IOTBluetooth/IOTBluetooth/Example/LogViewController.swift
IOTBluetooth/IOTBluetooth/Example/ViewController.swift
IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate
IOTBluetooth/IOTBluetooth/IOTBluetooth.xcodeproj/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist
IOTBluetooth/IOTBluetooth/IOTBluetooth/IBChecker.swift
IOTBluetooth/IOTBluetooth/IOTBluetooth/IBManager.swift
IOTBluetooth/IOTBluetooth/IOTBluetooth/IBParameter.swift
IOTBluetooth/IOTBluetooth/IOTBluetooth/Models/IBHistorySleep.swift
IOTBluetooth/IOTBluetooth/IOTBluetooth/Models/IBScreen.swift

index 2322d08..938dda3 100644 (file)
@@ -10,16 +10,24 @@ import IOTBluetooth
 
 class AbilityViewController: BaseViewController {
     
+    
+    
     @IBOutlet weak var listView: UITableView!
     
     override func viewDidLoad() {
         super.viewDidLoad()
         title = "已连接" + IBManager.currentConnected!.name
+        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Log", style: .plain, target: self, action: #selector(showLog))
     }
     
     deinit {
         IBManager.remove(self)
     }
+    
+    @objc func showLog(){
+        let vc = storyboard!.instantiateViewController(identifier: "LogViewController")
+        navigationController?.pushViewController(vc, animated: true)
+    }
 }
 
 extension AbilityViewController: UITableViewDelegate, UITableViewDataSource {
index 14b1189..17b4dd0 100644 (file)
@@ -8,6 +8,8 @@
 import Foundation
 import IOTBluetooth
 
+
+
 let datas: [(ability: IBAbility, label: String, parameters: [IBParameter])] = [
     (.userData,"设置用户数据",[.userInfo(IBUserInfo())]),
     (.language, "设置语言", [.language(.english)]),
@@ -23,7 +25,7 @@ let datas: [(ability: IBAbility, label: String, parameters: [IBParameter])] = [
     (.reboot, "重新启动", []),
     (.shutdown, "关机", []),
     (.fetchScreen, "获取手表屏幕参数", []),
-    (.screen, "设置手表屏幕参数", [.screen(IBScreen())]),
+    (.screen, "设置手表屏幕参数", [.screen(Config.setScreen())]),
     (.fetchBattery, "获取电量", []),
     (.fetchClock, "获取闹钟设置", []),
     (.setClock, "发送闹钟设置", [.clock(IBClock())]),
@@ -83,6 +85,7 @@ let datas: [(ability: IBAbility, label: String, parameters: [IBParameter])] = [
     (.syncCalorie, "同步卡路里历史数据", [.value(0)]),
     (.syncDistance, "同步距离历史数据", [.value(0)]),
     (.syncTrain, "同步基础训练历史数据", [.value(0)]),
+    (.syncSleep, "同步睡眠历史数据", [.value(0)]),
     (.syncGPS, "同步路径坐标集历史数据", [.value(1654012800)]),
     (.sendSyncEnd, "发送同步数据结束消息", []),
     (.addressBook, "发送通讯录", [.addressBook([IBAddressBook()])]),
@@ -101,3 +104,17 @@ let datas: [(ability: IBAbility, label: String, parameters: [IBParameter])] = [
     
     (.sendWeather, "发送天气数据", [.weather(IBWeather())]),
 ]
+
+class Config{
+    static func setScreen()->IBScreenConfig{
+        let model = IBScreenConfig()
+        model.type = 0
+        model.brightness = 255
+        
+//        model.type = 1
+//        model.duration = .minute
+//        model.wrist = 0
+        
+        return model
+    }
+}
index 246784a..2d8b114 100644 (file)
@@ -19,6 +19,9 @@ class LogViewController: BaseViewController {
     override func viewDidAppear(_ animated: Bool) {
         super.viewDidAppear(animated)
         textView.text = IBManager.logs.joined(separator: "\n")
+        
+        let range = NSMakeRange(textView.text.count - 1, 1)
+        textView.scrollRangeToVisible(range)
     }
 }
 
index 1d6af06..0d68752 100644 (file)
@@ -16,6 +16,7 @@ class ViewController: BaseViewController {
     override func viewDidLoad() {
         super.viewDidLoad()
         title = "IOTBluetooth"
+        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Log", style: .plain, target: self, action: #selector(showLog))
         
         IBManager.subscribe(.connectStateDidChange, observer: self) {  object, userInfo in
             print("connect notice " + "\(object!)")
@@ -44,6 +45,7 @@ class ViewController: BaseViewController {
     }
     
     @IBAction func startScan(_ sender:  UIButton) {
+        print("loggger ...")
         title = "正在扫描蓝牙设备..."
         IBManager.scan { [unowned self] peripheral in
             if let _ = datas.filter({ $0.uuid == peripheral.uuid }).last {
@@ -64,7 +66,10 @@ class ViewController: BaseViewController {
         IBManager.stopScan()
         title = "IOTBluetooth"
     }
-    
+    @objc func showLog(){
+        let vc = storyboard!.instantiateViewController(identifier: "LogViewController")
+        navigationController?.pushViewController(vc, animated: true)
+    }
 }
 
 extension ViewController: UITableViewDelegate, UITableViewDataSource {
index a29aacd..a1c401b 100644 (file)
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
index b99ea7c..9ace3af 100644 (file)
@@ -14,8 +14,8 @@
             filePath = "IOTBluetooth/IBChecker.swift"
             startingColumnNumber = "9223372036854775807"
             endingColumnNumber = "9223372036854775807"
-            startingLineNumber = "348"
-            endingLineNumber = "348"
+            startingLineNumber = "362"
+            endingLineNumber = "362"
             landmarkName = "unknown"
             landmarkType = "0">
          </BreakpointContent>
       <BreakpointProxy
          BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
          <BreakpointContent
-            uuid = "47EB3A2E-F088-471D-8274-DC421A2A67BF"
+            uuid = "E014DBEF-37A5-40CA-9357-146A5D0E85B8"
             shouldBeEnabled = "Yes"
             ignoreCount = "0"
             continueAfterRunningActions = "No"
-            filePath = "IOTBluetooth/IBluetooth.swift"
+            filePath = "IOTBluetooth/Models/IBHistorySleep.swift"
             startingColumnNumber = "9223372036854775807"
             endingColumnNumber = "9223372036854775807"
-            startingLineNumber = "370"
-            endingLineNumber = "370"
-            landmarkName = "peripheral(_:didUpdateValueFor:error:)"
+            startingLineNumber = "48"
+            endingLineNumber = "48"
+            landmarkName = "init(_:)"
             landmarkType = "7">
          </BreakpointContent>
       </BreakpointProxy>
index 50ca79f..32277be 100644 (file)
@@ -175,10 +175,24 @@ struct IBBloodOxygenChecker: IBChecker {
 }
 struct IBScreenChecker: IBChecker {
     static func check(_ parameters: [IBParameter]) -> [UInt8] {
-        guard case let .screen(value) = parameters[.screen] else { fatalError("缺少参数: screen") }
+        guard case let .screen(value) = parameters[.screen] else { fatalError("缺少参数: IBScreenConfig") }
         Logger.log(value.description)
-        let data = [value.brightness.byte] + [value.duration.rawValue.byte] + [value.wrist.byte]
-        return data
+        if value.type == 0{
+            if let brightness = value.brightness {
+                return [value.type.byte] + [brightness.byte]
+            }
+            else{
+                fatalError("参数错误: IBScreenConfig")
+            }
+        }
+        else{
+            if let duration = value.duration,let wrist = value.wrist {
+                return [value.type.byte] + [duration.rawValue.byte] + [wrist.byte]
+            }
+            else{
+                fatalError("参数错误: IBScreenConfig")
+            }
+        }
     }
 }
 
index 06c43b0..1949760 100644 (file)
@@ -27,11 +27,23 @@ public class IBManager {
     }
     
     public static var logs: [String] { Logger.logs }
+    
+    
+//    public static func redirectNSlogToDocumentFolder() {
+//        let filePath: String  =  NSHomeDirectory () +  "IOTBluetooth_\(Date().format(.long)).log"
+//        let defaultManager = FileManager.default
+//        try? defaultManager.removeItem(atPath: filePath)
+//        
+//        print("log path = " + filePath)
+//        
+//        freopen(filePath.cString(using: String.Encoding.ascii), "a+", stdout)
+//        freopen(filePath.cString(using: String.Encoding.ascii), "a+", stderr)
+//    }
 }
 
 public extension IBManager {
     /// SDK 版本号
-    static let version: String = "1.0.14"
+    static let version: String = "1.0.15"
     /// 默认为 false
     static var isDebug: Bool = false {
         didSet { Logger.isDebug = isDebug }
index 1cc8df9..45599c7 100644 (file)
@@ -67,7 +67,7 @@ public enum IBParameter {
     /// 血氧
     case bloodOxygen(IBBloodOxygen)
     /// 设置屏幕
-    case screen(IBScreen)
+    case screen(IBScreenConfig)
     /// 设置声音和震动
     case vibrateSound(IBVibrateSound)
     /// 设置目标设置
index b84a695..aad33d4 100644 (file)
@@ -15,19 +15,36 @@ 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 number = data[0].int
-        
-        
-        for i in stride(from: 0, to: data.count, by: 8){
-            if data.count > i + 7 {
-                let model = IBHistorySleepItem()
-//                model.time = data[i...i+3].int
-//                model.value = data[i+4...i+7].int
-//                historyList.append(model)
+        let times = data[0].int
+        let contentData:Data = data.suffix(from: 1).map({ $0 }).data
+        var index = 0
+        for _ in 0..<times {
+            if contentData.count > index + 1 {
+                let len = contentData[index...index+1].int
+                if contentData.count > index + len {
+                    let itemData = contentData[index+2 ... index+1+len].map({ $0 }).data
+                    let item = IBHistorySleepItem()
+                    item.sleepId = itemData[0].int
+                    item.type = itemData[1].int
+                    item.startTime = itemData[2...5].int
+                    item.endTime = itemData[6...9].int
+                    item.totalSleep = IBSleepTime(hour: itemData[10].int, minute: itemData[11].int)
+                    if(item.type == 1){
+                        item.deepSleep = IBSleepTime(hour: itemData[12].int, minute: itemData[13].int)
+                        item.lightSleep = IBSleepTime(hour: itemData[14].int, minute: itemData[15].int)
+                        item.remSleep = IBSleepTime(hour: itemData[16].int, minute: itemData[17].int)
+                        item.awake = IBSleepTime(hour: itemData[18].int, minute: itemData[19].int)
+                        item.states = IBSleepState(time: itemData[20...23].int, state: itemData[24].int)
+                    }
+                    historyList.append(item)
+                }
+                index += 2 + len
             }
+
         }
+
         list = historyList
     }
 }
@@ -35,7 +52,7 @@ import Foundation
 /// 睡眠历史记录
 public class IBHistorySleepItem {
     /// 睡眠ID,0~255,表示第几次睡眠
-    public var sleepId: UInt8 = 0
+    public var sleepId: Int = 0
     /// 睡眠类型,0:小睡(无总时长之后的所有项目)1:睡眠
     public var type: Int = 0
     /// 入睡时间戳,Unix时间戳
@@ -45,15 +62,15 @@ public class IBHistorySleepItem {
     /// 总时长
     public var totalSleep: IBSleepTime = IBSleepTime(hour: 0, minute: 0)
     /// 深睡时长
-    public var deepSleep: IBSleepTime = IBSleepTime(hour: 0, minute: 0)
+    public var deepSleep: IBSleepTime?
     /// 浅睡时长
-    public var lightSleep: IBSleepTime = IBSleepTime(hour: 0, minute: 0)
+    public var lightSleep: IBSleepTime?
     /// 快速动眼时长
-    public var remSleep: IBSleepTime = IBSleepTime(hour: 0, minute: 0)
+    public var remSleep: IBSleepTime?
     /// 清醒时长
-    public var awake: IBSleepTime = IBSleepTime(hour: 0, minute: 0)
+    public var awake: IBSleepTime?
     /// 睡眠状态
-    public var states:[IBSleepState] = []
+    public var states:IBSleepState?
 
 
     public init() { }
@@ -78,9 +95,14 @@ public struct IBSleepTime {
 
 
 /// 睡眠状态
-public class IBSleepState {
+public struct IBSleepState {
     /// Unix时间戳
     public var time: Int = 0
     /// 状态,0:进入睡眠   1:浅层睡眠   2:深层睡眠   3:清醒   4:结束睡眠   7:未出睡最后一笔睡眠数据(资料上报结束标志)   9:出睡最后一笔睡眠数据(资料上报结束标志)   12:快速眼动   13:取消入睡通知(睡眠不足 60 分钟出睡上报状态)   14:结束小睡
     public var state: Int = 0
+    
+    public init(time: Int, state: Int) {
+        self.time = time
+        self.state = state
+    }
 }
index 26d3cbd..9a1a017 100644 (file)
@@ -7,6 +7,37 @@
 
 import Foundation
 
+/// 设置屏幕参数
+public class IBScreenConfig {
+    /// 类型 0,亮度调节; 1,亮屏时长,抬腕亮屏
+    public var type: Int = 0
+    
+    /// 亮度 [51, 255],  type为0必须设置
+    public var brightness: Int?
+    
+    /// 亮屏时长,  type为1必须设置
+    public var duration: IBDuration?
+    
+    /// 抬腕亮屏(0=关闭, 1=开启),  type为1必须设置
+    public var wrist: Int?
+    
+    public init() { }
+    
+    var description: String  {
+        var str = "IBScreenConfig(type: \(type),"
+        if let brightness = brightness{
+            str += "brightness: \(brightness ))"
+        }
+        if let duration = duration {
+            str += "duration: \(duration )"
+        }
+        if let wrist = wrist {
+            str += "wrist: \(wrist ))"
+        }
+        return str
+    }
+}
+
 /// 屏幕参数
 public class IBScreen {
     /// 亮度 [0, 255]