Skip to content

Instantly share code, notes, and snippets.

@hkskoglund
Last active December 26, 2020 23:09
Show Gist options
  • Save hkskoglund/2fe5b771ebb886ad5acb to your computer and use it in GitHub Desktop.
Save hkskoglund/2fe5b771ebb886ad5acb to your computer and use it in GitHub Desktop.
bluetooth battery service uuid 0x2A19 polling with gatttool
#!/usr/bin/bash
# prints in CSV format for import into visualization tools
hextodec ()
{
hci_test_device_batterylevel=$((16#$1))
}
if [ -z "$1" ]
then
hci_test_device=EC:7E:45:9D:24:C0
else
hci_test_device=$1
fi
if [ -z "$2" ]
then
hci_polling_interval=60
else
hci_polling_interval=$2
fi
echo Polling battery service \(UUID 0x2A19\) from $hci_test_device each $hci_polling_interval seconds >&2
echo Date,Battery Level
while true
do
#read battery level
read_level=$(timeout --preserve-status 10s sudo gatttool -b $hci_test_device -t random --char-read -u 0x2a19 | cut -d ' ' -f 5)
if [ ! -z "$read_level" ]
then
hextodec $read_level
echo $(date +'%F %T'),$hci_test_device_batterylevel
else
# in case for example; connect error: Function not implemented (38)
echo Problems reading battery level with gatttool >&2
fi
sleep $hci_polling_interval
#read HR data for 1 minute
# HR at byte 40-41 (format in https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml)
# Output without filtering:
# Characteristic value was written successfully
# Notification handle = 0x000e value: 10 ca 30 01 30 01 30 01 30 01
# sudo timeout 60s gatttool -b EC:7E:45:9D:24:C0 -t random --char-write-req -a 0xf -n 0100 --listen | awk -v blevel="$BLEVEL" --non-decimal-data '{ printf "%s,%d,%d\n", strftime("%F %H:%M:%S"), "0x",blevel $7 }' | tee -a miobattery.log
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment