Extension Attribute for Bluetooth Mouse & Keyboard levels?

cjinphilly
New Contributor

To create a smartgroup for notification?

11 REPLIES 11

Jason_Lieble
New Contributor
New Contributor

That's a great idea! Apparently on Lion there is a bug for the battery level reporting incorrectly on the mouse....I'm sure getting this command into a managed preference is possible one way or another:

For Apple keyboard:
ioreg -n "AppleBluetoothHIDKeyboard" | grep -i "batterypercent"

This would be a good topic for a feature request to post here on JAMF Nation.

chrisbarker
New Contributor

This only works if the recon runs while the device is connected, otherwise OS X doesn't know about it, so you can't query the battery status at all.

Here is one for the mouse, it returns an integer, value 111 if no device/battery was found.

#!/bin/sh

mouseLevel=`ioreg -n "BNBMouseDevice" | grep -i "batterypercent" | awk '{print $9}'`

#Check for greater than 0 string length, ie, there is such a device connected
if [ ${mouseLevel} > 0 ]; then
  echo "<result>$mouseLevel</result>"
else
  echo "<result>111</result>"
fi

Here is one for the keyboard:

#!/bin/sh

keyLevel=`ioreg -n "AppleBluetoothHIDKeyboard" | grep -i "batterypercent" | awk '{print $10}'`

#Check for greater than 0 string length, ie, there is such a device connected
if [ ${keyLevel} > 0 ]; then
  echo "<result>$keyLevel</result>"
else
  echo "<result>111</result>"
fi

stevewood
Honored Contributor II
Honored Contributor II

Anyone have any idea why this would work on some machines, but not all? I've got 4 conference rooms that have machines in them with Bluetooth mice and keyboards. I can get the ioreg commands to work for 2 of the 4 machines.

On the machines that do not report back a percentage, I've gone on and made sure OS X is seeing the devices (i.e. they are connected), and even seen the battery percentage in the Bluetooth menu. However the ioreg commands do not work.

All 4 machines are 10.6.8.

Any ideas?

Steve

sean
Valued Contributor

What happens on the machines if you run

ioreg -n "BNBMouseDevice" | egrep "o BNBM|BatteryPercent"

stevewood
Honored Contributor II
Honored Contributor II

Comes back blank. If I do:

ioreg | grep -i bnbmousedevice

or even:

ioreg | grep BNBMouseDevice

I get nothing. I've also tried searching for the Apple Keyboard:

ioreg | grep AppleBluetoothHIDKeyboard

And get nothing returned. It's like those values/classes do not exist in ioreg.

The same commands return values on the 2 machines that do work.

Steve

sean
Valued Contributor

How about

ioreg -l | grep -i mouse

failing that, does it show up in system profiler

system_profiler SPBluetoothDataType

stevewood
Honored Contributor II
Honored Contributor II

So yes, the word "mouse" shows up in ioreg (as expected) and I can see the following entry:

AppleBluetoothHIDMouse <class AppleBluetoothHIDMouse

However I still do not get a battery status using this:

ioreg -c AppleBluetoothHIDMouse | grep -i "batterystatus"

And, System Profiler shows the Bluetooth info, but there is no battery status in there.

I'm still curious as to why the original ioreg commands work on some machines, but not others.

sean
Valued Contributor

Cool, getting somewhere now. Looks like this mouse reports differently in ioreg. The class is AppleBluetoothHIDMouse and not BNBMouseDevice

Why did you change from "batterypercent" to "batterystatus"?

Try

ioreg -c "AppleBluetoothHIDMouse" | grep -i battery

Hopefully then you will see a list of battery entries, including the elusive percentage

stevewood
Honored Contributor II
Honored Contributor II

Hey, lookey there, there's some battery info.... :-)

I changed it because I was going from memory and had the wrong term.

Thanks for the help Sean!

sean
Valued Contributor

Try the below instead. If you need different device names, then add them in the declared array and they will automatically be added.

If you want different items to report (I've given some examples) then add a new 'theKey' followed by the function name 'ioreg'

#!/bin/bash
declare -a myDevices=(
                'BNBMouseDevice' 
                'AppleBluetoothHIDKeyboard' 
                'AppleBluetoothHIDMouse' 
                );

function doioreg {
        ioreg -c "${myDevices[$myCount]}" | grep -i ""$theKey"" | cut -d "=" -f 2
}

myCount=0
arrayLength=`echo ${#myDevices[@]}`

until [ $myCount -eq $arrayLength ]
do
                theKey=product
                doioreg
                theKey=batterypercent
                doioreg
                theKey=DeviceName
                doioreg
                let myCount+=1
done

If you want a quick way to limit the output of ioreg to just the class you asked for, this will do it. Just change the class appropriately

ioreg -c "BNBMouseDevice" | grep """

Sean

sean
Valued Contributor

Just tidied it up and bit and made it more flexible, better output and enclosed in result for you. Second array so you can grab any keys you like. Easy to change for any class and keys in ioreg. Hope it's useful.

#!/bin/bash
echo -n "<result>"
declare -a myDevices=(
                'BNBMouseDevice' 
                'AppleBluetoothHIDKeyboard' 
                'AppleBluetoothHIDMouse' 
                );

declare -a theKeys=(
                'product' 
                'batterypercent' 
                'devicename' 
                );

function doioreg {
        theValue=`ioreg -c "${myDevices[$myCount]}" | grep -i ""$theKey"" | cut -d "=" -f 2`
        if [ "$theValue" ]
        then
                echo "$theKey:$theValue"
        fi
}

myCount=0
arrayLength=`echo ${#myDevices[@]}`

until [ $myCount -eq $arrayLength ]
do
        echo "${myDevices[$myCount]}"
        insideCount=0
        theKeyArrayLength=`echo ${#theKeys[@]}`

        until [ $insideCount -eq $theKeyArrayLength ]
        do
                theKey="${theKeys[$insideCount]}"
                doioreg
                let insideCount+=1
        done
        let myCount+=1
done
echo "</result>"