Ana içeriğe geç

Cihazı Bulma

Bağlı cihazdan firmware tarafından tanımlanan bulma uyarısını başlatmasını isteyin. Bu API temsil edilen cihazı bulmaya yarar; yakındaki Bluetooth cihazlarını taramaz.

Ön Koşullar

Bu API’yi çağırmadan önce şunları doğrulayın:

  • Cihaz bağlı ve hazırdır
  • Cihaz DeviceFindAPI protokolünü destekler

API Referansı

Framework

AIBuds.xcframework

İçe Aktarma

Swift
import AIBuds

Protokol

findDevice yöntemi DeviceFindAPI içinde tanımlanır.

Swift
/// Controls the locate-device indication on a connected device.
///
/// This API operates on the device represented by the conforming object. It
/// does not perform Bluetooth discovery or scan for nearby devices. The
/// indication used to locate the device, such as sound, vibration, or another
/// firmware-defined behavior, depends on the device implementation.
///
/// A successful completion from `findDevice(_:)` means the start command was
/// accepted; it does not mean the device has been physically located. Observe
/// `DeviceDelegate.deviceDidReportFound(_:)` or
/// `SDKDelegate.deviceDidReportFound(_:)` when the device supports reporting
/// that it has been found.
public protocol DeviceFindAPI: DeviceAPI {
    /// Requests that the connected device start its locate-device indication.
    ///
    /// The completion reports whether the command was accepted and executed by
    /// the device. It does not report whether the user has located the device.
    /// Use `DeviceDelegate.deviceDidReportFound(_:)` or the corresponding
    /// `SDKDelegate` callback for a device-originated found event.
    /// - Parameters:
    ///   - completion: Called when command processing completes.
    ///     - success: `true` when the device accepted the command; otherwise,
    ///       `false`.
    ///     - error: The command or communication error, or `nil` on success.
    func findDevice(_ completion: AIBudsCompletionHandler?)
}

Örnek Yöntemi

Bağlı cihazdan bulma uyarısını başlatmasını ister.

Swift
/// Requests that the connected device start its locate-device indication.
///
/// The completion reports whether the command was accepted and executed by
/// the device. It does not report whether the user has located the device.
/// Use `DeviceDelegate.deviceDidReportFound(_:)` or the corresponding
/// `SDKDelegate` callback for a device-originated found event.
/// - Parameters:
///   - completion: Called when command processing completes.
///     - success: `true` when the device accepted the command; otherwise,
///       `false`.
///     - error: The command or communication error, or `nil` on success.
func findDevice(_ completion: AIBudsCompletionHandler?)

Parametreler

ParametreTürAçıklama
completionAIBudsCompletionHandler?Komut işleme tamamlandığında çağrılan isteğe bağlı işleyici.

Callback Parametreleri:

AdTürAçıklama
successBool / BOOLCihaz komutu kabul ettiyse true; aksi takdirde false.
errorNSError?Komut ya da iletişim hatası; başarılıysa nil.

Dönüş Değeri

Bu yöntem doğrudan değer döndürmez. Completion callback’i komutun işlenmesini bildirir; kullanıcının cihazı fiziksel olarak bulup bulmadığını bildirmez.

Kullanım Örnekleri

Swift
import AIBuds

final class DeviceManager {
    weak var device: DeviceConvertible?

    func startFindingDevice() {
        guard let device = device as? DeviceFindAPI else {
            print("Device does not support finding")
            return
        }

        device.findDevice { success, error in
            guard success else {
                print("Failed to start finding: \(error?.localizedDescription ?? "Unknown error")")
                return
            }

            print("Locate-device command accepted")
        }
    }
}

Cihaz Bulundu Olayını İzleme

Cihaz destekliyorsa deviceDidReportFound, cihaz tarafından gönderilen ayrı bitiş olayıdır:

Swift
/// Called when the device reports that it has been found.
///
/// This is distinct from `findDevice(_:)` completion, which only reports
/// command processing.
func deviceDidReportFound(_ device: DeviceConvertible) {
    DispatchQueue.main.async {
        self.showDeviceFoundState()
    }
}

Hata Yönetimi

Komutun kabul edildiğini göstermeden önce success değerini denetleyin; komut veya iletişim hatalarında error kullanın. Başarılı komut işlemeyi “cihaz bulundu” olarak göstermeyin; cihaz destekliyorsa ayrı bulundu olayını bekleyin.

En İyi Uygulamalar

  1. Protokol Uyumluluğunu Denetleyin: Yöntemi çağırmadan önce DeviceFindAPI desteğini doğrulayın.
  2. Kesin Arayüz Durumu Kullanın: “Komut kabul edildi” ile “cihaz bulundu” durumlarını ayırın.
  3. Durdurma Eylemi Sunun: Uyarıyı başlattıktan sonra stopFindDevice(_:) seçeneğini kullanılabilir yapın.
  4. Bulundu Olayını İzleyin: Desteklendiğinde cihaz veya SDK delegate callback’ini uygulayın.
  5. Kullanıcı Arayüzünü Ana Kuyrukta Güncelleyin: Completion ve delegate callback’lerindeki UIKit güncellemelerini ana kuyruğa yönlendirin.

Notlar

  • Bu API zaten bağlı olan cihazı denetler; cihaz keşfi veya taraması yapmaz.
  • Genel API bulma uyarısını tanımlar, ancak cihazın bunu nasıl sunacağını belirlemez.
  • Ses, titreşim, süre ve ses düzeyi davranışı cihaza özgü olabilir.
  • iPhone’u bulma ters yöndeki akıştır; iPhone’un Bulunduğunu Bildirme sayfasına bakın.