Skip to content

Instantly share code, notes, and snippets.

@nfedera
Created September 4, 2019 14:14
Show Gist options
  • Save nfedera/3a2cc4677647c9c3b4c8ffd7f29ecc7b to your computer and use it in GitHub Desktop.
Save nfedera/3a2cc4677647c9c3b4c8ffd7f29ecc7b to your computer and use it in GitHub Desktop.
Bluez4 helper script
#!/bin/bash
BTADAPTER=""
function set_default_adapter() {
RV=$(dbus-send --system --dest=org.bluez --print-reply / org.bluez.Manager.DefaultAdapter)
if [ $? -ne 0 ]; then
return -1
fi
BTADAPTER=$(echo $RV | sed 's/^.*"\(.*\)".*$/\1/')
return 0
}
if ! set_default_adapter ; then
echo "error: cannot get default bluetooth adapter"
exit 1
fi
if [ "$1" == "scan" ]; then
hcitool scan
elif [ "$1" == "list" ]; then
for mac in `dbus-send --system --dest=org.bluez --print-reply $BTADAPTER org.bluez.Adapter.ListDevices | grep dev_ | sed 's/.*dev_\(.*\)\"/\1/' | tr _ :`; do
cat /var/lib/bluetooth/*/names | grep $mac
done
elif [ "$1" == "names" ]; then
cat /var/lib/bluetooth/*/names
elif [ "$1" == "trusts" ]; then
cat /var/lib/bluetooth/*/trusts
elif [ "$1" == "props" ]; then
if [ "$2" == "" ]; then
echo "usage: $1 props 11:22:33:44:55:66"
exit 1
fi
BTDEV=$(echo $2 | tr '[:lower:]' '[:upper:]' | tr : _)
dbus-send --system --dest=org.bluez --print-reply $BTADAPTER/dev_$BTDEV org.bluez.Device.GetProperties
elif [ "$1" == "remove" ]; then
if [ "$2" == "" ]; then
echo "usage: $1 remove 11:22:33:44:55:66"
exit 1
fi
BTDEV=$(echo $2 | tr '[:lower:]' '[:upper:]' | tr : _)
if ! dbus-send --system --dest=org.bluez --print-reply $BTADAPTER org.bluez.Adapter.RemoveDevice objpath:$BTADAPTER/dev_$BTDEV >/dev/null 2>&1 ; then
echo "failed to remove $2"
exit 1
fi
echo "success"
elif [ "$1" == "pair" ]; then
if [ "$2" == "" ]; then
echo "usage: $1 pair 11:22:33:44:55:66"
exit 1
fi
echo "pairing with $2 ..."
if ! /hacks/bin/agent 0000 "$2" ; then
echo failed
exit 1
fi
echo "success, setting $2 as trusted ..."
BTDEV=$(echo $2 | tr '[:lower:]' '[:upper:]' | tr : _)
if ! dbus-send --system --dest=org.bluez --print-reply $BTADAPTER/dev_$BTDEV org.bluez.Device.SetProperty string:Trusted variant:boolean:true >/dev/null 2>&1 ; then
echo "failed"
exit 1
fi
echo "ok"
elif [ "$1" == "ping" ]; then
if [ "$2" == "" ]; then
echo "usage: $1 ping 11:22:33:44:55:66"
exit 1
fi
l2ping "$2"
else
echo "usage: $0 [scan|list|names|trusts|props|remove|pair|ping]"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment