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

पहनने की पहचान की स्थिति देखें

पढ़ें कि पहनने की पहचान enabled है या नहीं, और इस समय कोई भी, एक या दोनों earbuds पहने गए हैं।

आवश्यकताएँ

  • डिवाइस कनेक्टेड और ready है।
  • डिवाइस DeviceWearDetectionAPI के अनुरूप है।

API संदर्भ

Framework

AIBuds.xcframework

Import

Swift
import AIBuds

प्रोटोकॉल

Swift
/// The protocol for device wear detection API.
protocol DeviceWearDetectionAPI: DeviceAPI {
    /// Indicates whether wear detection is currently enabled
    var isWearDetectionEnabled: Bool { get }

    /// The current wear status of the device.
    var wearStatus: WearStatus { get }
}

गुण

PropertyTypeविवरण
isWearDetectionEnabledBool / BOOLपहनने की पहचान इस समय enabled है या नहीं।
wearStatusWearStatusबाएँ/दाएँ की मौजूदा पहनने की स्थिति।

पहनने की स्थिति के मान

SwiftObjective-Cअर्थ
.unknownAIBudsWearStatusUnknownपहनने की स्थिति अज्ञात है।
.notWearingAIBudsWearStatusNotWearingकोई भी side नहीं पहना गया है।
.leftWearingAIBudsWearStatusLeftWearingकेवल बायाँ side पहना गया है।
.rightWearingAIBudsWearStatusRightWearingकेवल दायाँ side पहना गया है।
.bothWearingAIBudsWearStatusBothWearingदोनों sides पहने गए हैं।

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

Swift
import AIBuds

guard let device = device as? DeviceWearDetectionAPI else {
    print("Device does not support wear detection")
    return
}

print("Wear detection enabled: \(device.isWearDetectionEnabled)")

switch device.wearStatus {
case .unknown:
    print("Wear state is unknown")
case .notWearing:
    print("Neither side is worn")
case .leftWearing:
    print("Only the left side is worn")
case .rightWearing:
    print("Only the right side is worn")
case .bothWearing:
    print("Both sides are worn")
@unknown default:
    print("Unsupported wear-state value")
}

त्रुटि प्रबंधन

ये synchronous snapshot properties हैं और errors return नहीं करतीं। Unsupported protocol conformance और .unknown को स्पष्ट रूप से संभालें। पढ़ने के बाद वर्तमान स्थिति बदल सकती है।

श्रेष्ठ अभ्यास

  1. Enabled और Wear State अलग रखें: false और .notWearing अलग अवधारणाएँ हैं।
  2. Updates देखें: UI को current रखना हो तो didWearDetectionEnabledChanged और didWearStatusChanged implement करें।
  3. UI Main Thread पर अपडेट करें: आवश्यकता होने पर delegate से आए UI changes को main queue पर dispatch करें।

नोट्स

  • SDK getWearDetectionSwitchStatus या Result आधारित query methods के बजाय properties देता है।
  • .unknown को “नहीं पहना” के रूप में न दिखाएँ।