Skip to content

Instantly share code, notes, and snippets.

@fourdollars
Last active December 22, 2015 10:29
Show Gist options
  • Save fourdollars/6459580 to your computer and use it in GitHub Desktop.
Save fourdollars/6459580 to your computer and use it in GitHub Desktop.
A little script to debug Bluetooth issue.
#!/bin/bash
# -*- coding: utf-8; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2; -*-
# vim:fileencodings=utf-8:expandtab:tabstop=2:shiftwidth=2:softtabstop=2
#
# Copyright (C) 2013 Shih-Yuan Lee (FourDollars) <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# sudo service bluetooth stop
# sudo /usr/sbin/bluetoothd -n -d |& ts %.s |& tee bluetoothd.log
set -e
check_cmd ()
{
if sh -c "$1" > /dev/null 2>&1; then
true
else
if [ -n "$2" ]; then
sh -c "$2"
fi
if which gst-launch >/dev/null 2>&1; then
gst-launch audiotestsrc ! decodebin ! autoaudiosink
fi
false
fi
}
set_alarm ()
{
echo '0' > /sys/class/rtc/rtc0/wakealarm
echo '+5' > /sys/class/rtc/rtc0/wakealarm
check_cmd "grep \"alarm_IRQ.: yes\" /proc/driver/rtc" "cat /proc/driver/rtc"
pm-suspend
}
while getopts dl:n:p:s: name; do
case "$name" in
(d)
DUMP='yes'
;;
(l)
LOOP="$OPTARG"
;;
(n)
NETWORK="$OPTARG"
;;
(p)
IP="$OPTARG"
;;
(s)
SCAN="$OPTARG"
;;
esac
done
while :; do
echo -en "\r$((++loop))... "
set_alarm
check_cmd "rfkill list | grep Bluetooth" "echo There is no Bluetooth device from rfkill list."
if [ -n "$DUMP" ]; then
if [ -f "hcidump.$((loop - 2)).raw" ]; then
rm hcidump.$((loop - 2)).raw
fi
hcidump -w hcidump.$loop.raw >/dev/null 2>&1 &
fi
[ -n "$SCAN" ] && check_cmd "hcitool scan --refresh | grep $SCAN" "echo hcitool can not find $SCAN"
[ -n "$NETWORK" ] && check_cmd "nmcli con up id \"$NETWORK\"" "echo can not connect to \"$NETWORK\""
[ -n "$IP" ] && check_cmd "ping -c 1 $IP" "echo ping $IP failed."
[ -n "$LOOP" ] && [ $loop -eq $LOOP ] && exit
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment