17

I want to get the name of the AirPlay or Bluetooth device my iPhone is connected to. I have somewhat achieved this with the following code...

let currentRoute = AVAudioSession.sharedInstance().currentRoute
for output in currentRoute.outputs {
    print("Connected port name: \(output.portName)")
}

But, whenever my iPhone is connected to an AirPlay device such as a HomePod or Apple TV, the portName returned is just "Speaker". I would like to get the name of the HomePod or Apple TV. Whenever my device is connected to Bluetooth, though, this works. For example, whenever I am connected to my AirPods, the name "Jacob's AirPods" is returned. Whenever I am connected to another Bluetooth device, the correct name is returned.

I believe that getting the name of the AirPlay device should be possible because Apple does it in their Apple Music app and system-wide. Like bellow...

Sceenshot of AirPlay 2 window on iOS 12

Any suggestions?

Jacob Cavin
  • 2,169
  • 3
  • 19
  • 47
  • 1
    Only Apple is able to always retrieve the name of a wireless audio device, and the external API currently does not always return the personalized name of an audio device. If this is a feature you would like to see feel free to file a bug with [Feedback Assistant](https://feedbackassistant.apple.com/welcome). – DaveAMoore Sep 06 '19 at 03:31
  • @DaveAMoore Well it’s strange because every once in a while, I will receive the name of the AirPlay device – Jacob Cavin Sep 06 '19 at 03:32
  • 2
    @JacobCavin have you had a look at this [Get name of AirPlay device using AVPlayer](https://stackoverflow.com/q/13044894/4056108) it has some promising answers – chirag90 Sep 06 '19 at 10:56

1 Answers1

-4

To obtain the name of the AirPlay device your iPhone is connected to, you can use the MultipeerConnectivity framework in iOS. This framework allows you to discover and connect to nearby devices, including AirPlay devices, and retrieve their names.

Here's an example of how you can achieve this:

import MultipeerConnectivity

class AirPlayDeviceManager: NSObject, MCNearbyServiceBrowserDelegate {
let serviceType = "airplay"

var browser: MCNearbyServiceBrowser?
var discoveredDevices: [MCPeerID] = []

override init() {
    super.init()
    browser = MCNearbyServiceBrowser(peer: MCPeerID(displayName: UIDevice.current.name), serviceType: serviceType)
    browser?.delegate = self
}

func startBrowsing() {
    browser?.startBrowsingForPeers()
}

func stopBrowsing() {
    browser?.stopBrowsingForPeers()
}

// MARK: MCNearbyServiceBrowserDelegate

func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String: String]?) {
    discoveredDevices.append(peerID)
}

func browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {
    if let index = discoveredDevices.firstIndex(of: peerID) {
        discoveredDevices.remove(at: index)
    }
}
}

// Usage

let airPlayDeviceManager = AirPlayDeviceManager()
airPlayDeviceManager.startBrowsing()

// Wait for some time to allow the browser to discover devices
// You can then access the discovered devices using      `airPlayDeviceManager.discoveredDevices`

This code sets up an instance of MCNearbyServiceBrowser with a custom service type of "airplay". It then implements the necessary delegate methods to track discovered and lost AirPlay devices. You can start browsing for devices by calling airPlayDeviceManager.startBrowsing(). After some time, the discovered AirPlay devices will be available in the discoveredDevices array of the AirPlayDeviceManager instance.

Please note that this approach relies on the MultipeerConnectivity framework, which may have limitations or behavior changes in different iOS versions. Make sure to test and adapt the code accordingly.

Brian Trzupek
  • 4,982
  • 1
  • 16
  • 19