Check the battery level of connected bluetooth headphones from the command line
On my iMac(macOS 10.12.6), none of solutions mentioned above works. After reading the man page of ioreg. The solution that works on my computer is:

// the -r option is used to reduce output
// the -l option is used to display properties for all object
// by default some object properties are not shown
// the -c specify device class, the class could be specific or general
ioreg -c AppleDeviceManagementHIDEventService -r -l
ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i battery

// instead of using device io class, you could also use a property key to query
// you need to know one of target device's property key(-k), for example it is BatteryPercent for Bluetooth keyboard and mouse
// any device with this property will be listed
ioreg -r -l -k "BatteryPercent"
ioreg -r -l -k "BatteryPercent" | grep -i battery


There are some other device classes to try for the -c argument: AppleHSBluetoothDevice  AppleHSBluetoothHIDDriver AppleMultitouchMouseHIDEventDriver AppleHIDKeyboardEventDriverV2, they give different output.