Search:  
Gentoo Wiki

TIP_bluetooth_headset_daemon_and_init_script

This article is part of the Tips & Tricks series.
Terminals / Shells Network X Window System Portage System Filesystems Kernel Other

Contents

Introduction

This TIP is here to bring you an init script starting btsco (or btsco2) and let you use your bluetooth headset. Cause btsco will exit if your headset is not switched on when the init script starts, there is a bash script that will act as a daemon, waiting for your headset to be switched on and then automatically start btsco.

If you want to use btsco2 this is not necessary, cause it will keep running even if your headset is switched of. In this case you only need the init script.


Requirements

In order to use this you will need alsa, bluetooth and your headset to be properly configured. Please follow these documentations to reach this:


Installation

Headset daemon

Note: This is not necessary if you are going to use btsco2.


First we will get the bash script that will act as a daemon waiting for the headset being switched on.

Just copy/paste this file as root:

jane ~#
vi /usr/local/bin/connect-to-headset.shImage:CursorOFF.gif
File: /usr/local/bin/connect-to-headset.sh
 
#!/bin/bash

source /etc/conf.d/headset

tail --pid $$ -fn 0 /var/log/messages | while read SYSLOG_IN
do
        echo ${SYSLOG_IN} | grep -e 'hcid.*link_key_request' &> /dev/null
        if [ $? -eq "0" ]
        then
                SYSLOG_IN="$(echo $SYSLOG_IN | sed 's/.*dba=//' | sed 's/)//')"
                if [ "${SYSLOG_IN}" == "${BD_ADDRESS}" ]
                then
                        btsco -f $BD_ADDRESS &> /dev/null
                fi
        fi
done
  


Now make the script executable:

jane ~#
chmod +x /usr/local/bin/connect-to-headset.shImage:CursorOFF.gif


Note: For this script to work you will also need the config file created later.


Init script

Second we need the init script that lauches btsco(2) (and the just created script):

jane ~#
vi /etc/init.d/headsetImage:CursorOFF.gif
File: /etc/init.d/headset
 
#!/sbin/runscript
# Copyright 1999-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

depend()
{
        need bluetooth alsasound
}

start()
{
        ebegin "Connecting to your bluetooth headset"

        
        if [ "${USE_BTSCO2}" == "TRUE" ]
        then
                start-stop-daemon --start --background --quiet \
                        --make-pidfile --pidfile /var/run/btsco2 \
                        --exec /usr/bin/btsco2 -- $BD_ADDRESS
        else
                start-stop-daemon --start --background --quiet \
                        --make-pidfile --pidfile /var/run/btsco \
                        --exec /usr/bin/btsco -- $BD_ADDRESS

                start-stop-daemon --start --background \
                        --make-pidfile --pidfile /var/run/connect-to-headset.sh \
                        --exec /usr/local/bin/connect-to-headset.sh
        fi

        eend $?
}

stop()
{
        ebegin "Killing bluetooth headset daemons"
        start-stop-daemon --stop --pidfile /var/run/btsco2
        start-stop-daemon --stop --pidfile /var/run/btsco
        start-stop-daemon --stop --pidfile /var/run/connect-to-headset.sh
        killall -9 btsco &> /dev/null
        eend 0
}

restart()
{
        svc_stop
        svc_start
}
  


Now make the init script executable:

jane ~#
chmod +x /etc/init.d/headsetImage:CursorOFF.gif


In addition, the init script and the daemon script, both require a config file. This file should provide the hardware address of your headset. Btsco will only be started for devices using this address. Please follow the headset guide Bluetooth headset#Configure the system if you haven't yet. You can also choose whether you want to use btsco or btsco2.

jane ~#
vi /etc/conf.d/headsetImage:CursorOFF.gif
File: /etc/conf.d/headset
 
#
# Config file for /etc/init.d/headset and connect-to-headset.sh scripts
#

# Your headsets hardware address as reported by 'hcitool inq'
BD_ADDRESS="00:0F:F6:15:2D:E0"

# Comment out if you want to use btsco, otherwise btsco2 is used
USE_BTSCO2="TRUE"
  

Usage

That's all we need for now :)

If you want the init script to be started with your system (*very* recommended). Please type:

jane ~#
rc-update add headset defaultImage:CursorOFF.gif

To start it right now, just type:

jane ~#
/etc/init.d/headset startImage:CursorOFF.gif

... and switch on your headset. After a few seconds you will be able to use it.


Note: Sometimes my headset needs some time to calm down before I can use it. (Maybe yours too; so please be patient)
Retrieved from "http://www.gentoo-wiki.info/TIP_bluetooth_headset_daemon_and_init_script"

Last modified: Fri, 05 Sep 2008 05:29:00 +0000 Hits: 5,819