मुख्य कंटेंट तक स्किप करें

डिवाइस का समय सिंक करें

जुड़े डिवाइस की internal clock को मौजूदा समय से synchronize करें। जब कोई निश्चित तारीख दिए बिना घड़ी मिलानी हो तब यह operation उपयोग करें।

पहले की आवश्यकताएँ

डिवाइस का समय synchronize करने से पहले सुनिश्चित करें:

  • डिवाइस connected और stable state में है
  • डिवाइस DeviceInfoAPI protocol का समर्थन करता है

API संदर्भ

फ्रेमवर्क

AIBuds.xcframework

Import

SDK उपयोग करने वाली files में main framework import करें:

Swift
import AIBuds

प्रोटोकॉल

syncDeviceTime method DeviceInfoAPI में परिभाषित है। यह protocol base device API protocol से inherit करता है।

Swift
/// The protocol for device information related API.
protocol DeviceInfoAPI: DeviceAPI {
    /// Synchronizes the device time with the current time.
    /// - Parameters:
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: `true` if the operation was successful; otherwise `false`.
    ///     - statusCode: The status code returned by the device. `nil` if the operation failed.
    ///     - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
    func syncDeviceTime(_ completion: AIBudsStatusCodeCompletionHandler?)
}

Instance method

डिवाइस के समय को मौजूदा समय से synchronize करता है।

Swift
/// Synchronizes the device time with the current time.
/// - Parameters:
///   - completion: A closure that is called when the operation completes.
///     - success: `true` if the operation was successful; otherwise `false`.
///     - statusCode: The status code returned by the device. `nil` if the operation failed.
///     - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
func syncDeviceTime(_ completion: AIBudsStatusCodeCompletionHandler?)

पैरामीटर

पैरामीटरप्रकारविवरण
completionAIBudsStatusCodeCompletionHandler?Operation पूरा होने पर call होने वाला optional completion handler।

Callback पैरामीटर:

नामप्रकारविवरण
successBool / BOOLOperation सफल हो तो true, अन्यथा false
statusCodeNSNumber?डिवाइस का status code। SDK के अनुसार operation असफल होने पर यह nil होता है।
errorNSError?Operation असफल हो तो error details, अन्यथा nil

Return value

यह method सीधे कोई value return नहीं करता। परिणाम completion handler से मिलता है।

उपयोग के उदाहरण

Swift
import AIBuds

final class DeviceManager {

    /// The connected device
    weak var device: DeviceConvertible?

    /// Synchronizes the connected device with the current time
    func synchronizeDeviceTime() {
        guard let device = device as? DeviceInfoAPI else {
            print("Device does not support time synchronization")
            return
        }

        device.syncDeviceTime { success, statusCode, error in
            if !success {
                print(
                    "Time synchronization failed: "
                        + (error?.localizedDescription ?? "Unknown error")
                )
                return
            }

            print(
                "Time synchronized successfully. Status code: " + (statusCode?.stringValue ?? "N/A")
            )
        }
    }
}

Error handling

Completion handler synchronization का परिणाम देता है:

  1. Operation पूरा मानने से पहले success जाँचें।
  2. success false हो तो failure details के लिए error उपयोग करें।
  3. उपलब्ध होने पर diagnostics या device-specific handling के लिए statusCode रखें।
  4. लक्ष्य डिवाइस के लिए documented न होने वाला error या status code न मानें।

बेहतर तरीके

  1. Protocol conformance जाँचें: Method call करने से पहले DeviceInfoAPI support की पुष्टि करें।

  2. Connection के बाद call करें: डिवाइस connected और ready होने के बाद ही synchronize करें।

  3. सभी completion values संभालें: success, statusCode और error तीनों देखें; केवल error पर निर्भर न रहें।

  4. UI main queue पर अपडेट करें: Completion से आने वाले UIKit updates main queue पर भेजें।

ध्यान देने योग्य बातें

  • syncDeviceTime लक्ष्य Date स्वीकार नहीं करता; तारीख देनी हो तो setDeviceTime(to:completion:) उपयोग करें।
  • Public API मौजूदा समय से synchronization बताता है, पर UTC conversion contract नहीं बताता।
  • Time synchronization support model और firmware के अनुसार बदल सकता है।