このブログはURLが変更になりました

新しいブログはこちら→ https://matsuu.hatenablog.com/

HAL経由でバッテリー情報を取得する

私のノート(DELL Inspiron mini 9)では/proc/acpiからバッテリー情報が取得できない。調べてみるとHAL経由であれば取得できることが分かった。

シェルスクリプトで確認するならこんな感じ。

#!/bin/bash

bat_udi="$(hal-find-by-property --key info.category --string battery)"
if [ -n "${bat_udi}" ]; then
  charge_level=$(hal-get-property --key battery.charge_level.percentage --udi ${bat_udi})
  echo "バッテリ残量: ${charge_level}%"
fi

ac_udi="$(hal-find-by-property --key info.category --string ac_adapter)"
if [ -n "${ac_udi}" ]; then
  ac_present="$(hal-get-property --key ac_adapter.present --udi ${ac_udi})"
  if [ "${ac_present}" = "true" ]; then
    echo "AC電源使用中"
  else 
    echo "バッテリー使用中"
  fi
fi

要はこういうことです。

# バッテリ残量
hal-get-property --key battery.charge_level.percentage --udi $(hal-find-by-property --key info.category --string battery)
# ACアダプタ
hal-get-property --key ac_adapter.present --udi $(hal-find-by-property --key info.category --string ac_adapter)