【问题标题】:How to check if Airpods are connected to iPhone?如何检查 Airpods 是否连接到 iPhone?
【发布时间】:2019-01-02 23:37:19
【问题描述】:

我正在尝试检查 AirPods 是否已连接到 iPhone。如何以编程方式检查它?

对于 airpods,port.portType 值为 .builtInMic,这不足以检查 airpods 是否连接到 iphone

class func isMicAvailbale() -> Bool{
        let availableInputs:[AVAudioSessionPortDescription] = AVAudioSession.sharedInstance().availableInputs ?? []
        var micPresent = false;
        for port in availableInputs
        {
            if port.portType == .builtInMic{
                micPresent = true
            }
        }
        return micPresent
    }

【问题讨论】:

  • 嘿,我编辑了我的答案,我之前有点撒谎,我不知道 Airpods 不是 Apple MFI。对不起您的时间损失:(

标签: swift airpods


【解决方案1】:

我想到的一种方法是您可以使用Core Bluetooth API 通过蓝牙访问 airpods。但是,当您可以使用 AVSession 时,这可能有点过头了。我不知道为什么您只想检测 airpods 而没有其他蓝牙耳机。但我认为buildInMic 代表设备内部的内置麦克风,而不是蓝牙设备:P 如果您查看docs,您可以看到它:P

您没有要求其他蓝牙耳机,但作为答案的一部分,我将为您提供此代码,这应该适用于通过蓝牙连接到 iPhone 的非 MFI 耳机。

现在到 Airpod 部分。

您可能想使用ExternalAccessory.framework与Airpods等MFI蓝牙设备进行通信。~~

我还没有与EAAccessory 合作,但我相信你必须这样做:

  1. 创建EAAccessoryManager 的实例
  2. 使用该实例获取连接的设备
  3. 通过某个 ID 查找 airpods
  4. 弄清楚如何检查附件是否已连接,但这应该是小菜一碟。

同样重要的一步是将UISupportedExternalAccessoryProtocols 添加到您的info.plist 文件中

我有点累,所以如果你有任何问题,明天我会在这里写实现,如果没有人会更快的话。

好吧,很明显我的回答首先是完全错误的。

我今天了解到 apple's MFI devices 中没有列出 Airpods,因此 ExternalAccessorymanager 显然无法正常工作。如页脚中提到的答案所述,您需要做的就是向 AVSession 添加类别。

所以整个代码基本上都在这里 :D

 let session = AVAudioSession.sharedInstance()
        try! session.setCategory(.playAndRecord, mode: .default, options: .allowBluetooth)
        guard let availableInputs = session.availableInputs else { return }
        for input in availableInputs {
            if input.portType == .bluetoothHFP {
                // Do your stuff...
            }
        }

证明:

2019-01-04 02:32:13.462093+0100 Accessory games[24578:5411208] [avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port Butcher’s AirPods (type: BluetoothHFP)
(lldb) po availableInputs
▿ 2 elements
  - 0 : <AVAudioSessionPortDescription: 0x283b401b0, type = MicrophoneBuiltIn; name = iPhone Mikrofon; UID = Built-In Microphone; selectedDataSource = Vpředu>
  - 1 : <AVAudioSessionPortDescription: 0x283b40250, type = BluetoothHFP; name = Butcher’s AirPods; UID = 10:94:BB:5D:5F:F7-tsco; selectedDataSource = (null)>

(lldb) po availableInputs[1].portName
"Butcher’s AirPods"

(lldb) po availableInputs[1].portType
▿ AVAudioSessionPort
  - _rawValue : BluetoothHFP

(lldb) 

很抱歉造成误解并写出完全偏离主题的答案。但是,嘿,至少您对外部配件有所了解:)

您可能还想看看here

【讨论】:

  • 一切正常,但我在控制台AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port Pooja’s AirPods (type: BluetoothHFP)中不断收到此错误@
  • 这个错误是可以的,它只是在苹果端,你可以通过一些方法设置数据源
  • 这个只是告诉我airpods是否连接到iphone,我将如何判断airpods是否真的在某人的耳朵里
  • 这不是一个正确的答案。这项检查对任何蓝牙耳机都是肯定的,而不仅仅是Air Pods
  • @rommex 用 CoreBluetooth 写下解决方案?
猜你喜欢
  • 2015-03-03
  • 2011-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多