GCController.battery for PlayStation controllers

Hello-

I am trying to monitor the battery level of my DualShock or DualSense controller, but I can't seem to get readings.

Here is the code I'm using:

Code Block
...
@objc dynamic var myController: GCController?
...
func handleControllerDidConnectNotification(_ notification: Notification) {
for controller in GCController.controllers() {
self.myController = controller
self.myController!.addObserver(self, forKeyPath: "battery", options: [.new, .old, .initial, .prior], context: nil)
}
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
// Set breakpoint here
}


The observer gets an initial battery level, but it's nothing:

- value : <GCDeviceBattery - battery=0.0, state=-1>

...and no other updates get reported. Am I doing something wrong, or are PS controllers not supported for battery level and battery state?

DidConnect notification handler should only process the controller it was called for. Can you try replacing the loop for controller in GCController.controllers() { self.myController = controller with self.myController = notification.object and see what's being reported?

GCController.battery for PlayStation controllers
 
 
Q