如何检测哪种蓝牙设备音频从中发出

问题描述 投票:0回答:2

我有两个连接到我的iPad的BluetoothHFP蓝牙设备(bluetoothA2DP和bluetoothLE),我需要检测当前正在获取音频的设备。下面是我用来检测可用蓝牙的代码:

let currentRoute = audioSession.currentRoute
 for description in currentRoute.outputs {
                        if convertFromAVAudioSessionPort(description.portType) == convertFromAVAudioSessionPort(AVAudioSession.Port.bluetoothA2DP) {
                            //Do Something
                            break
                        }else if convertFromAVAudioSessionPort(description.portType) == convertFromAVAudioSessionPort(AVAudioSession.Port.bluetoothHFP) {
                            //Do Something
                            break
                        }else if convertFromAVAudioSessionPort(description.portType) == convertFromAVAudioSessionPort(AVAudioSession.Port.bluetoothLE){
                            //Do Something
                            break
                        }
                    }

我可以使用什么来找出当前有哪个BluetoothHFP设备正在获取音频?

ios swift bluetooth avaudiosession
2个回答
0
投票

您可以使用description.portNamedescription.uid来区分相同类型的端口。请注意,该名称不保证是唯一的(来自设备)。 UID是系统分配的,不能保证稳定。仅保证与owningPortUID属性一致,并且在任何给定时间都是唯一的。

目前(iOS 13),UID基于硬件MAC地址并且是稳定的。格式为aa:bb:cc:dd:ee:ff-tacl(MAC地址后跟-tacl)。这已经有很长时间了(至少从iOS 8起我就一直在使用这个事实),但是没有得到保证,这正是苹果公司已知会在不知不觉中进行更改的事情。


-1
投票

您需要更改outputDataSources,就像您覆盖它一样,现在它只包含.Speaker选项在文档中,您可以找到解决方案,

[如果您的应用使用playAndRecord类别,请使用AVAudioSession.PortOverride.speaker选项导致音频被路由到内置扬声器和麦克风,而不管其他设置。该更改仅在当前路线之前一直有效更改,或者您再次使用AVAudioSession.PortOverride.none选项。

因此音频被路由到内置扬声器,此更改仅在当前路由更改或您使用.noneOption再次调用此方法时才有效。

除非将附件插入耳机插孔(这会激活物理开关以将语音引导到耳机,否则除非将附件强行引导到耳机,否则不可能将声音直接引导到耳机。

因此,当您想切换回耳机时,这应该起作用。如果没有连接耳机,则会将输出设备切换到设备顶部的小扬声器输出,而不是大扬声器。

let session: AVAudioSession = AVAudioSession.sharedInstance()
        do {
            try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
            try session.overrideOutputAudioPort(AVAudioSession.PortOverride.none)
            try session.setActive(true)
        } catch {
            print("Couldn't override output audio port")
        }
© www.soinside.com 2019 - 2024. All rights reserved.