डिवाइस का समय सिंक करें
जुड़े डिवाइस की internal clock को मौजूदा समय से synchronize करें। जब कोई निश्चित तारीख दिए बिना घड़ी मिलानी हो तब यह operation उपयोग करें।
पहले की आवश्यकताएँ
डिवाइस का समय synchronize करने से पहले सुनिश्चित करें:
- डिवाइस connected और stable state में है
- डिवाइस
DeviceInfoAPIprotocol का समर्थन करता है
API संदर्भ
फ्रेमवर्क
AIBuds.xcframework
Import
SDK उपयोग करने वाली files में main framework import करें:
- Swift
- Objective-C
import AIBuds#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>प्रोटोकॉल
syncDeviceTime method DeviceInfoAPI में परिभाषित है। यह protocol base device API protocol से inherit करता है।
- Swift
- Objective-C
/// 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?)
}/// The protocol for device information related API.
@protocol AIBudsDeviceInfoAPI <AIBudsDeviceAPI>
/// 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.
- (void)syncDeviceTimeWithCompletion:(AIBudsStatusCodeCompletionHandler _Nullable)completion;
@endInstance method
डिवाइस के समय को मौजूदा समय से synchronize करता है।
- Swift
- Objective-C
/// 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?)/// 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.
- (void)syncDeviceTimeWithCompletion:(AIBudsStatusCodeCompletionHandler _Nullable)completion;पैरामीटर
| पैरामीटर | प्रकार | विवरण |
|---|---|---|
completion | AIBudsStatusCodeCompletionHandler? | Operation पूरा होने पर call होने वाला optional completion handler। |
Callback पैरामीटर:
| नाम | प्रकार | विवरण |
|---|---|---|
success | Bool / BOOL | Operation सफल हो तो true, अन्यथा false। |
statusCode | NSNumber? | डिवाइस का status code। SDK के अनुसार operation असफल होने पर यह nil होता है। |
error | NSError? | Operation असफल हो तो error details, अन्यथा nil। |
Return value
यह method सीधे कोई value return नहीं करता। परिणाम completion handler से मिलता है।
उपयोग के उदाहरण
- Swift
- Objective-C
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")
)
}
}
}#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>
@interface DeviceManager ()
/// The connected device
@property(weak, nonatomic) id<AIBudsDeviceConvertible> device;
@end
@implementation DeviceManager
- (void)synchronizeDeviceTime {
id<AIBudsDeviceInfoAPI> device = (id<AIBudsDeviceInfoAPI>)self.device;
if (![device conformsToProtocol:@protocol(AIBudsDeviceInfoAPI)]) {
NSLog(@"Device does not support time synchronization");
return;
}
[device syncDeviceTimeWithCompletion:^(
BOOL success, NSNumber *_Nullable statusCode, NSError *_Nullable error) {
if (!success) {
NSLog(@"Time synchronization failed: %@",
error.localizedDescription ?: @"Unknown error");
return;
}
NSLog(@"Time synchronized successfully. Status code: %@", statusCode ?: @"N/A");
}];
}
@endError handling
Completion handler synchronization का परिणाम देता है:
- Operation पूरा मानने से पहले
successजाँचें। successfalseहो तो failure details के लिएerrorउपयोग करें।- उपलब्ध होने पर diagnostics या device-specific handling के लिए
statusCodeरखें। - लक्ष्य डिवाइस के लिए documented न होने वाला error या status code न मानें।
बेहतर तरीके
-
Protocol conformance जाँचें: Method call करने से पहले
DeviceInfoAPIsupport की पुष्टि करें। -
Connection के बाद call करें: डिवाइस connected और ready होने के बाद ही synchronize करें।
-
सभी completion values संभालें:
success,statusCodeऔरerrorतीनों देखें; केवलerrorपर निर्भर न रहें। -
UI main queue पर अपडेट करें: Completion से आने वाले UIKit updates main queue पर भेजें।
ध्यान देने योग्य बातें
syncDeviceTimeलक्ष्यDateस्वीकार नहीं करता; तारीख देनी हो तोsetDeviceTime(to:completion:)उपयोग करें।- Public API मौजूदा समय से synchronization बताता है, पर UTC conversion contract नहीं बताता।
- Time synchronization support model और firmware के अनुसार बदल सकता है।