I'm making a swift app that shows the battery level of all connected bluetooth devices. I've got three devices connected: Mysz(name surname), Magic Keyboard (name), i12; where Mysz is just Magic Mouse.
Using this script I manage to get only the first two devices:
set batteryInfo to do shell script "ioreg -r -l -k 'BatteryPercent'" set deviceList to paragraphs of batteryInfo set currentDevice to "" set currentBatteryLevel to "" set batteryInfo to "" repeat with a in deviceList if a contains "Product" and a does not contain "ProductID" then set currentDevice to text ((offset of "=" in a) + 3) through -2 of a end if if a contains "BatteryPercent" then set currentBatteryLevel to text ((offset of "=" in a) + 2) through -1 of a set batteryInfo to batteryInfo & currentDevice & ": " & currentBatteryLevel & "%\n" end if end repeat if (length of batteryInfo) > 0 then set batteryInfo to text 1 through -2 of batteryInfo end if return batteryInfo This script return this:
Magic Mouse: 70% Magic Keyboard: 75% Using the MacOS GUI I see all three devices with listed battery levels beside them. Does anyone know how to retrieve the battery level of all connected bluetooth devices? I'm using MacOS 13.7.6
ioregcommand I was looking for a way to execute it from my Swift code.CoreBluetooth.swifttag reaches more Bluetooth experts than theapplescripttag. Have you tried stackoverflow.com/search?q=%5Bswift%5D+bluetooth+battery or stackoverflow.com/search?q=%5Bswift%5D+ioreg ?