PDA

View Full Version : How to activate bluetooth when a call is incoming?


thhart
2011-08-24, 19:30
Anybody has an idea how to activate bluetooth automatically when calls are incoming. I would like to have bluetooth only activated during calls to work with headset. This is for security and battery power reasons.
How can I do this? Is there an event triggered in the system, so I can write a simple activation script?

I am quite new to this device hacking, so sorry for a probably dummy question. A simple pointer would be helpful as well of course. ;-)

Have a nice day
Thomas

JonWW
2011-08-24, 20:08
To switch bluetooth ondbus-send --system --type=method_call --dest=org.bluez $(dbus-send --system --print-reply --dest=org.bluez / org.bluez.Manager.ListAdapters | awk -F'"' '/at/ {print $2}') org.bluez.Adapter.SetProperty string:Powered variant:boolean:true
To switch bluetooth offdbus-send --system --type=method_call --dest=org.bluez $(dbus-send --system --print-reply --dest=org.bluez / org.bluez.Manager.ListAdapters | awk -F'"' '/at/ {print $2}') org.bluez.Adapter.SetProperty string:Powered variant:boolean:false
Use dbus-monitor to monitor the system and trigger an event when you get/make a calldbus-monitor --help
For example this will catch when the keyboard is opened or closed.
dbus-monitor --monitor --system type='signal',interface='org.freedesktop.Hal.Devic e',path='/org/freedesktop/Hal/devices/platform_slide',member='Condition'|while read LINE
do
echo "$LINE"|grep -o "cover"
done
The only problem is will the bluetooth connect to your headset in time before the caller hangs up.

thhart
2011-08-24, 22:07
The only problem is will the bluetooth connect to your headset in time before the caller hangs up.

The pairing is quite fast and I have found out I have enough time to turn on the headset and get it ready within 10 seconds.

Thanks for you suggestion - will test it soon...

Thomas

lidow
2011-08-25, 08:03
Here is also dbus-script event that will switch on/off bluetooth during call:

asN900:~# cat /etc/dbus-scripts.d/bluetooth-during-call
/home/user/bin/bton.sh * * com.nokia.policy.telephony call_request
/home/user/bin/btoff.sh * * com.nokia.policy.telephony call_ended

Where bton.sh and btoff.sh are the examples posted by JonWW

lidow
2011-08-25, 14:09
I wanted to have this:

* bluetooth to be on only during a call
* when switch on, force connect to my BT handsfree if available
* when call is over, switch off bluetooth only if bluetooth is not started manually

These are the scripts to achieve it:

/etc/dbus-scripts.d/bluetooth-during-call
/home/user/bin/bton.sh * * com.nokia.policy.telephony call_request
/home/user/bin/btoff.sh * * com.nokia.policy.telephony call_ended

/home/user/bin/bton.sh
#!/bin/sh

adapter=$(dbus-send --print-reply --system --dest=org.bluez / org.bluez.Manager.DefaultAdapter| sed -ne "s/^.*object path //p"|sed -e 's/"//g')

if ( dbus-send --system --print-reply --type=method_call --dest=org.bluez ${adapter} org.bluez.Adapter.GetProperties | sed -n '/string "Powered"/,/variant/ p' | grep -q false )
then
dbus-send --system --type=method_call --dest=org.bluez $adapter org.bluez.Adapter.SetProperty string:Powered variant:boolean:true
touch /tmp/bt.autopowered
fi


devmac=00:1C:EF:75:D4:B5

device=$(dbus-send --print-reply --system --dest=org.bluez ${adapter} org.bluez.Adapter.FindDevice string:${devmac}|sed -ne "s/^.*object path //p"|sed -e 's/"//g')
dbus-send --system --print-reply --type=method_call --dest=org.bluez ${device} org.bluez.Audio.Connect

!!!NOTE: change devmac to proper value for your BT handsfree

/home/user/bin/btoff.sh
#!/bin/sh

if [[ -f /tmp/bt.autopowered ]]
then
dbus-send --system --type=method_call --dest=org.bluez $(dbus-send --system --print-reply --dest=org.bluez / org.bluez.Manager.ListAdapters | awk -F'"' '/at/ {print $2}') org.bluez.Adapter.SetProperty string:Powered variant:boolean:false
rm /tmp/bt.autopowered
fi


TODO:

* disable WLAN during calls, as there are sound distortions while both BT and WLAN are enabled. Do not happen always, but I could not determine the pattern when/what is causing it.

dr_frost_dk
2011-08-25, 18:59
I think this should help.

change in file /etc/bluetooth/audio.conf

from:
HFP=true

to:
HFP=false

restart the phone

Hmmm, just notice that this is for hfp/hsp. While you want it for a2dp.
Try, but most likely will not help. Anyway it is not so hard to disconect BT during call.

no it is not hard to disconnect, but lets say that the phone is 10secs away, now the remaining time left to answer is shorter....

And lets continue in your thread, you where first :D

lidow
2011-08-25, 19:05
what means 10secs away.
it disconnects almost immediatly.

infact this is not my thread, it is started by thhart.

ashes
2011-08-25, 19:12
@dr_frost_dk

I am not sure the answer to your issue, but I can provide one datapoint. When my n900 is connected to the Sony bluetooth headunit in my car it switches automatically between playing audio with A2DP/AVRCP to HSP/HFP at the start and end of a call. I have not modified n900 stock settings in this regard, so perhaps keeping a BT headset around may be a crude solution.

lidow
2011-08-25, 19:14
you should:

* install dbus-script
* create file
/etc/dbus-scripts.d/no-a2dp-during-call
/home/user/bin/a2dpoff.sh * * com.nokia.policy.telephony call_request
/home/user/bin/a2dpon.sh * * com.nokia.policy.telephony call_ended

* create both scripts

/home/user/bin/a2dpoff.sh
#!/bin/sh

adapter=$(dbus-send --print-reply --system --dest=org.bluez / org.bluez.Manager.DefaultAdapter| sed -ne "s/^.*object path //p"|sed -e 's/"//g')

devmac=00:1C:EF:75:D4:B5

device=$(dbus-send --print-reply --system --dest=org.bluez ${adapter} org.bluez.Adapter.FindDevice string:${devmac}|sed -ne "s/^.*object path //p"|sed -e 's/"//g')
dbus-send --system --print-reply --type=method_call --dest=org.bluez ${device} org.bluez.Audio.Disconnect

/home/user/bin/a2dpon.sh
#!/bin/sh

adapter=$(dbus-send --print-reply --system --dest=org.bluez / org.bluez.Manager.DefaultAdapter| sed -ne "s/^.*object path //p"|sed -e 's/"//g')

devmac=00:1C:EF:75:D4:B5

device=$(dbus-send --print-reply --system --dest=org.bluez ${adapter} org.bluez.Adapter.FindDevice string:${devmac}|sed -ne "s/^.*object path //p"|sed -e 's/"//g')
dbus-send --system --print-reply --type=method_call --dest=org.bluez ${device} org.bluez.Audio.Connect

lidow
2011-08-25, 19:17
@dr_frost_dk

I am not sure the answer to your issue, but I can provide one datapoint. When my n900 is connected to the Sony bluetooth headunit in my car it switches automatically between playing audio with A2DP/AVRCP to HSP/HFP at the start and end of a call. I have not modified n900 stock settings in this regard, so perhaps keeping a BT headset around may be a crude solution.

So try with the setting before the script...

dr_frost_dk
2011-08-25, 19:26
ok guys ill try, but a bit strapped for time right now, will give feedback at some point, thanks :)

svg
2013-01-02, 01:00
Hello!
I cannot figure out how to use the script that turns the BT on/off when a call is incoming. The steps that i followed
1.Installed Dbus-scripts
2.Created lidow's /etc/dbus-scripts.d/bluetooth-during-call, edited it "/home/user/bin/bton.sh * * com.nokia.policy.telephony call_request
/home/user/bin/btoff.sh * * com.nokia.policy.telephony call_ended" and made it executable
3.Created bton.sh and btoff.sh at /home/user/bin and edited them with the code provided by JonWW, then made them executable
4.killall dbus-scripts
5.receive call and... nothing happens :confused:
6.if i run dbus-scripts --debug and receive call script works
I have 0 programming experience and read as much as i could understand about this matter... bottom line is I'm stuck. Can someone tell me what should i do or point me in the right direction (wiki's or other material) so that i can get this thing working for me?:(
Thank you! :)