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

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

जुड़े डिवाइस की internal clock को ऐप द्वारा दी गई तारीख और समय पर set करें।

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

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

  • डिवाइस connected और stable state में है
  • डिवाइस DeviceInfoAPI protocol का समर्थन करता है
  • लक्ष्य Date वही समय दिखाता है जिसे ऐप भेजना चाहता है

API संदर्भ

फ्रेमवर्क

AIBuds.xcframework

Import

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

Swift
import AIBuds

प्रोटोकॉल

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

Swift
/// The protocol for device information related API.
protocol DeviceInfoAPI: DeviceAPI {
    /// Sets the device's system time.
    /// - Parameters:
    ///   - date: The target time to set on the device.
    ///   - 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 setDeviceTime(
        to date: Date,
        completion: AIBudsStatusCodeCompletionHandler?
    )
}

Instance method

डिवाइस का system time दी गई तारीख पर set करता है।

Swift
/// Sets the device's system time.
/// - Parameters:
///   - date: The target time to set on the device.
///   - 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 setDeviceTime(
    to date: Date,
    completion: AIBudsStatusCodeCompletionHandler?
)

पैरामीटर

पैरामीटरप्रकारविवरण
dateDate / NSDateडिवाइस पर set किया जाने वाला समय।
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?

    /// Sets the connected device to the supplied date
    func setDeviceTime(to date: Date) {
        guard let device = device as? DeviceInfoAPI else {
            print("Device does not support setting the time")
            return
        }

        device.setDeviceTime(to: date) { success, statusCode, error in
            if !success {
                print(
                    "Failed to set device time: " + (error?.localizedDescription ?? "Unknown error")
                )
                return
            }

            print(
                "Device time set successfully. Status code: " + (statusCode?.stringValue ?? "N/A")
            )
        }
    }
}

Error handling

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

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

बेहतर तरीके

  1. लक्ष्य तारीख जाँचें: सुनिश्चित करें कि ऐप इच्छित तारीख और समय भेज रहा है।

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

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

  4. सभी completion values संभालें: success, statusCode और error देखें।

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

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

  • Public API Date स्वीकार करता है, पर UTC conversion contract नहीं बताता। लक्ष्य डिवाइस द्वारा न बताई timezone assumptions न जोड़ें।
  • डिवाइस को केवल मौजूदा समय से synchronize करना हो तो syncDeviceTime(_:) उपयोग करें।
  • लक्ष्य समय set करने का support model और firmware के अनुसार बदल सकता है।