Bluetooth Device Battery Status Mac

Bluetooth Device Battery Status Mac

In This article you will come to know how you can use command line to check battery status of bluetooth device like keyboard and mouse.
The command we will use is “ioreg”, shows Input/Output Kit registry.

For Keyboard: Open terminal, copy and paste the below command.
ioreg -c AppleBluetoothHIDKeyboard | grep -i batterypercent
bluetooth_keyboard_battery_status

For Mouse: Open terminal, copy and paste the below command.
ioreg -c BNBMouseDevice | grep -i batterypercent
bluetooth_mouse_battery_status

To make life Easy I have written a Python script to show Information:
python bluetooth_battery_notification.py
python_program_bluetooth_device_battery_status

#AUTHOR KUMAR SHUBUHAM
import subprocess

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'



def mouse_battery_status():
    print bcolors.OKBLUE + "Mouse Battery Status" + bcolors.ENDC
    child1 = subprocess.Popen(["ioreg", "-c", "BNBMouseDevice"], stdout=subprocess.PIPE)
    child2 = subprocess.Popen(["grep", "-i", "battery"], stdin=child1.stdout, stdout=subprocess.PIPE)
    status = filter(lambda x:x!="", map(str.strip, child2.communicate()[0].split("|")))
    mouseBatteryPanic = status[0].replace('"', "")
    mouseBatteryPercent = status[-2].replace('"', "")
    mouseBatteryLow = status[-4].replace('"', "")
    print "{}\n{}\n{}\n\n".format(mouseBatteryPanic, mouseBatteryPercent, mouseBatteryLow)

def keyboard_battery_status():
    print bcolors.OKBLUE + "Keyboard Battery Status" + bcolors.ENDC
    child1 = subprocess.Popen(["ioreg", "-c", "AppleBluetoothHIDKeyboard"], stdout=subprocess.PIPE)
    child2 = subprocess.Popen(["grep", "-i", "battery"], stdin=child1.stdout, stdout=subprocess.PIPE)
    status = filter(lambda x:x!="", map(str.strip, child2.communicate()[0].split("|")))
    keyboardBatteryPanic = status[0].replace('"', "")
    keyboardBatteryPercent = status[-1].replace('"', "")
    keyboradBatteryLow = status[-4].replace('"', "")
    print "{}\n{}\n{}\n\n".format(keyboardBatteryPanic, keyboardBatteryPercent, keyboradBatteryLow)
if __name__ == '__main__':
   print bcolors.HEADER + "\n\n Welcome to Bluetooth Device battery Status Portal\n\n" + bcolors.ENDC
   keyboard_battery_status()
   mouse_battery_status()

Copy and paste the above code in any text editor and save it as “bluetooth_battery_notification.py”. open Terminal and type: python bluetooth_battery_notification.py
Note: you must be in the directory in terminal where bluetooth_battery_notification.py is saved.

Plz Share Share on FacebookShare on Google+Share on LinkedInTweet about this on TwitterEmail this to someoneShare on RedditShare on TumblrDigg thisBuffer this pagePrint this pagePin on PinterestShare on StumbleUponFlattr the author
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *