tvos 15 - Siri Remote not returning in the array: GCController.controllers()

in swiftUI & tvOS 15, when calling the GCController.controllers() to get the list of controllers connected to the apple tv,

import GameController

 ...

let siriRemoteAsGameController = GCController.controllers().first

the Siri Remote is not registered as the first controller, in fact it is not registered at all ! up until tvOS 15 (14.7 for example) it was working even if i register for notification the connect event isn't dispatched for the already connected Siri remote

NotificationCenter.default.addObserver(forName: .GCControllerDidConnect, object: nil, queue: .main) { note in

        print("GCControllerDidConnect")
        if let detectedGCController = note.object as? GCController {
            print("Controller Detected")
        }
    }

GCController.startWirelessControllerDiscovery(completionHandler: {})

i cannot find a change in that area according to Appel's $#itty documentation

any help would be appreciated

Answered by robnotyou in 689396022

As a very simple test, I tried this:

struct ContentView: View {
    var body: some View {
        Button("Query controllers") {
            print("controllers: \(GCController.controllers())")
        }
    }
}

The first time I clicked "Query controllers", it returned:

controllers: []

Clicking "Query controllers" again, it returned the siri remote:

controllers: [<GCController 0x283618620 ('Siri Remote (2nd Generation)' - 0xa6f1756e8a7cb7bb)>]

So perhaps you have to have some controller interaction, to populate GCController.controllers()?

tvOS 15.0
Xcode 13.0

Accepted Answer

As a very simple test, I tried this:

struct ContentView: View {
    var body: some View {
        Button("Query controllers") {
            print("controllers: \(GCController.controllers())")
        }
    }
}

The first time I clicked "Query controllers", it returned:

controllers: []

Clicking "Query controllers" again, it returned the siri remote:

controllers: [<GCController 0x283618620 ('Siri Remote (2nd Generation)' - 0xa6f1756e8a7cb7bb)>]

So perhaps you have to have some controller interaction, to populate GCController.controllers()?

tvOS 15.0
Xcode 13.0

thanks @robnotyou

based on your answer i added a first call and the second call (after interaction with remote) worked on first call:

import GameController

struct ContentView2: View {
  var body: some View {
    let a = print("controllers: \(GCController.controllers())")
     
    Button("Query controllers") {
      print("controllers: \(GCController.controllers())")
    }
  }
}

i think there is some initialization missing (dam apple and it's useless docs)

for now this workaround will do

Hi there!

In iOS/tvOS 15, the GameController framework delays enumerating connected game controllers until the application first accesses GCController.controllers() or begins listening for the GCControllerDidConnectNotification. This enumeration process is asynchronous. So if the first call to GCController.controllers() will likely return no results. But the GCControllerDidConnectNotification should be posted shortly after - once for each found controller. Further calls to GCController.controllers() will return all of the connected game controllers.

We recommend apps always listen for the GCControllerDidConnectNotification and GCControllerDidDisconnectNotification in addition to calling GCController.controllers().

tvos 15 - Siri Remote not returning in the array: GCController.controllers()
 
 
Q