पावर ऑफ
पावर ऑफ ऑपरेशन प्रोग्रामेटिक रूप से डिवाइस को बंद करता है। जब आपको डिवाइस को दूरस्थ रूप से बंद करने की जरूरत हो या नियंत्रित शटडाउन अनुक्रम के हिस्से के रूप में उपयोग करना हो तो यह ऑपरेशन उपयोगी होता है।
पूर्वापेक्षाएं
पावर ऑफ करने से पहले, सुनिश्चित करें:
- डिवाइस कनेक्टेड है और स्थिर स्थिति में है
- सभी असंग्रहीत डेटा सहेजा गया है
- उपयोगकर्ता को पता है कि शटडाउन के बाद डिवाइस डिस्कनेक्ट हो जाएगा
AI की सहायता से लागू करें
इस वर्कफ़्लो को AI से लागू करें
आधिकारिक “AIBuds पावर ऑफ लागू करें” स्किल से वर्कफ़्लो को अपने ऐप के अनुसार लागू करें।
https://docs-aibuds.github.io/hi/skills/implement-aibuds-power-off को पढ़ें और निर्देशों का पालन करें। इस स्किल से “AIBuds पावर ऑफ लागू करें” को इस iOS प्रोजेक्ट में लागू करें और परिणाम सत्यापित करें।API रेफरेंस
फ्रेमवर्क
AIBuds.xcframework
इंपोर्ट
जहां आप SDK का उपयोग करना चाहते हैं वहां फाइलों में, मुख्य हेडर को इंपोर्ट करें:
- Swift
- Objective-C
import AIBuds#import <AIBuds/AIBuds.h>
#import <AIBuds/AIBuds-Swift.h>प्रोटोकॉल
powerOff मेथड निम्नलिखित प्रोटोकॉल में परिभाषित है। यह प्रोटोकॉल बेस डिवाइस API प्रोटोकॉल से वंशानुगत है।
- Swift
- Objective-C
/// Defines common device operations including power off
protocol DeviceCommonAPI: DeviceAPI {
/// Power off the device
/// - Parameters:
/// - completion: Completion callback that returns the operation result
/// - success: `true` if the operation was successful; otherwise `false`.
/// - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
func powerOff(_ completion: AIBudsCompletionHandler?)
}/// Defines common device operations including power off
@protocol AIBudsDeviceCommonAPI <AIBudsDeviceAPI>
/// Power off the device
/// - Parameters:
/// - completion: Completion callback that returns the operation result
/// - success: `true` if the operation was successful; otherwise `false`.
/// - error: An `NSError` object that describes the error that occurred, or `nil` if the
/// operation was successful.
- (void)powerOffWithCompletion:(AIBudsCompletionHandler)completion;
@endइंस्टेंस मेथड
प्रोग्रामेटिक रूप से डिवाइस को पावर ऑफ करता है।
iOS 13.0+
- Swift
- Objective-C
/// Power off the device
/// - Parameters:
/// - completion: Completion callback that returns the operation result
/// - success: `true` if the operation was successful; otherwise `false`.
/// - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
func powerOff(_ completion: AIBudsCompletionHandler?)/// Power off the device
/// - Parameters:
/// - completion: Completion callback that returns the operation result
/// - success: `true` if the operation was successful; otherwise `false`.
/// - error: An `NSError` object that describes the error that occurred, or `nil` if the
/// operation was successful.
- (void)powerOffWithCompletion:(AIBudsCompletionHandler)completion;पैरामीटर्स
| पैरामीटर | प्रकार | विवरण |
|---|---|---|
| completion | AIBudsCompletionHandler? | ऑपरेशन पूरा होने पर बुलाया जाने वाला वैकल्पिक callback। |
Callback पैरामीटर:
| नाम | प्रकार | विवरण |
|---|---|---|
| success | Bool | ऑपरेशन सफल होने पर true, अन्यथा false। |
| error | NSError? | विफलता पर त्रुटि की जानकारी; सफलता पर nil। |
रिटर्न वैल्यू
यह मेथड सीधे कोई मान नहीं लौटाता। परिणाम कंप्लीशन कॉलबैक के माध्यम से प्रदान किया जाता है।
उपयोग के उदाहरण
- Swift
- Objective-C
import AIBuds
class DeviceManager {
/// The connected device
weak var device: DeviceConvertible?
/// Powers off the connected device
func powerOffDevice() {
// Ensure the device supports power off protocol
guard let device = device as? DeviceCommonAPI else {
print("Device does not support power off")
return
}
// Execute power off with completion handler
device.powerOff { [weak self] success, error in
// Handle failure case
if !success {
let errorMessage = {
if let error = error {
return "\(error)"
}
return "Unknown error"
}()
print("Power off failed: \(errorMessage)")
return
}
// Handle success case
print("Power off command sent successfully")
}
}
}#import <AIBuds/AIBuds.h>
@interface DeviceManager ()
/// The connected device
@property(weak, nonatomic) id<AIBudsDeviceConvertible> device;
@end
@implementation DeviceManager
- (void)powerOffDevice {
__weak typeof(self) weakSelf = self;
id<AIBudsDeviceCommonAPI> device = (id<AIBudsDeviceCommonAPI>)self.device;
// Ensure the device supports power off protocol
if ([device conformsToProtocol:@protocol(AIBudsDeviceCommonAPI)]) {
// Execute power off with completion handler
[device powerOffWithCompletion:^(BOOL success, NSError *_Nullable error) {
// Handle failure case
if (!success) {
NSLog(@"Power off failed: %@", error);
return;
}
// Handle success case
NSLog(@"Power off command sent successfully");
}];
}
}
@endएरर हैंडलिंग
कंप्लीशन हैंडलर निम्नलिखित एरर प्रकार लौटा सकता है:
त्रुटि डोमेन: AIBudsSDK.ErrorDomain
- Swift
- Objective-C
| त्रुटि कोड | विवरण | समाधान |
|---|---|---|
| .deviceNotConnected | डिवाइस कनेक्ट नहीं है | पेयरिंग और कनेक्शन की जाँच करें |
| .bleCommandExecFailedDueToTimeout | ऑपरेशन का समय समाप्त हो गया | ऑपरेशन फिर से चलाएँ |
| .deviceBusy | डिवाइस किसी अन्य ऑपरेशन में व्यस्त है | मौजूदा ऑपरेशन पूरा होने तक प्रतीक्षा करें |
| .deviceNotSupport | डिवाइस पावर ऑफ का समर्थन नहीं करता | कॉल करने से पहले डिवाइस की क्षमताएँ जाँचें |
| त्रुटि कोड | विवरण | समाधान |
|---|---|---|
| AIBudsSdkErrorCodeDeviceNotConnected | डिवाइस कनेक्ट नहीं है | पेयरिंग और कनेक्शन की जाँच करें |
| AIBudsSdkErrorCodeBleCommandExecFailedDueToTimeout | ऑपरेशन का समय समाप्त हो गया | ऑपरेशन फिर से चलाएँ |
| AIBudsSdkErrorCodeDeviceBusy | डिवाइस किसी अन्य ऑपरेशन में व्यस्त है | मौजूदा ऑपरेशन पूरा होने तक प्रतीक्षा करें |
| AIBudsSdkErrorCodeDeviceNotSupport | डिवाइस पावर ऑफ का समर्थन नहीं करता | कॉल करने से पहले डिवाइस की क्षमताएँ जाँचें |
सर्वश्रेष्ठ प्रथाएं
-
उपयोगकर्ता से पुष्टि लें: पावर ऑफ होने पर कनेक्शन टूट जाएगा, इसलिए पहले पुष्टि संवाद दिखाएँ।
-
UI को मुख्य थ्रेड पर अपडेट करें: Callback से UI बदलते समय
DispatchQueue.main.asyncका उपयोग करें। -
कमज़ोर संदर्भ का उपयोग करें: Retain cycle से बचने के लिए callback में
[weak self]का उपयोग करें। -
प्रोटोकॉल समर्थन जाँचें: मेथड कॉल करने से पहले सुनिश्चित करें कि डिवाइस
DeviceCommonAPIका समर्थन करता है। -
डिस्कनेक्शन संभालें: सफल पावर ऑफ के बाद ऐप में डिवाइस की कनेक्शन स्थिति सही ढंग से अपडेट करें।
नोट्स
- पावर ऑफ कमांड निष्पादित होने के बाद डिवाइस डिस्कनेक्ट हो जाएगा
- उपयोगकर्ता द्वारा डिवाइस को मैन्युअल रूप से फिर से चालू किया जा सकता है
- कोई भी चल रहा ऑपरेशन बाधित हो जाएगा
- पावर ऑफ में कुछ सेकंड लग सकते हैं