From 29ddbc0639dd1defb839e79a224f77f8f2e4760b Mon Sep 17 00:00:00 2001 From: Berstanio Date: Sat, 21 Oct 2023 21:58:20 +0200 Subject: [PATCH] fix(ios): Check for null on GCController.battery (#40) https://developer.apple.com/documentation/gamecontroller/gccontroller/3563986-battery?language=objc --- .../src/com/badlogic/gdx/controllers/IosController.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/gdx-controllers-ios/src/com/badlogic/gdx/controllers/IosController.java b/gdx-controllers-ios/src/com/badlogic/gdx/controllers/IosController.java index b777a4a..71384e6 100644 --- a/gdx-controllers-ios/src/com/badlogic/gdx/controllers/IosController.java +++ b/gdx-controllers-ios/src/com/badlogic/gdx/controllers/IosController.java @@ -20,6 +20,7 @@ import org.robovm.apple.gamecontroller.GCControllerDirectionPad; import org.robovm.apple.gamecontroller.GCControllerElement; import org.robovm.apple.gamecontroller.GCControllerPlayerIndex; +import org.robovm.apple.gamecontroller.GCDeviceBattery; import org.robovm.apple.gamecontroller.GCExtendedGamepad; import org.robovm.apple.gamecontroller.GCGamepad; import org.robovm.apple.gamecontroller.GCHapticsLocality; @@ -415,9 +416,13 @@ public ControllerMapping getMapping() { @Override public ControllerPowerLevel getPowerLevel() { if (Foundation.getMajorSystemVersion() >= 14) { - switch (controller.getBattery().getBatteryState()) { + GCDeviceBattery battery = controller.getBattery(); + if (battery == null) + return ControllerPowerLevel.POWER_UNKNOWN; + + switch (battery.getBatteryState()) { case Discharging: - float batteryLevel = controller.getBattery().getBatteryLevel(); + float batteryLevel = battery.getBatteryLevel(); if (batteryLevel <= 0.05f) { return ControllerPowerLevel.POWER_EMPTY; } else if (batteryLevel <= 0.20f) {