个人工具

UbuntuHelp:BluetoothHeadset

来自Ubuntu中文

跳转至: 导航, 搜索

Purpose

This guide should help you redirect music, audio and media players to your Bluetooth headset. To make this work properly all audio will have to be redirected through the PulseAudio Server Thanks to HyRax1 on the Overclockers forum.

Prerequisites

  • Bluetooth 1.2 compliant (or better) adapter to your PC. This can be in-built, such as on a modern notebook PC, or a USB dongle. You will NOT have success with Bluetooth adapters that are only compliant with the 1.0 or 1.1 specification.
  • Ubuntu Jaunty 9.04.
  • Internet access or other such access to the Ubuntu repositories to install extra software from.
  • Suitable Bluetooth headset (before purchasing please research your choice, to ensure that it is indeed supported and works in Ubuntu)

Instructions

  1. Plug in or enable your Bluetooth adapter. Your Bluetooth adapter will be automatically detected and drivers loaded - there is nothing for you to do manually here.
  2. Turn on your Bluetooth headset.
  3. Switch your headset into pairing mode (refer to your headset's manual).
  4. While the headset is in pairing mode, left click the Bluetooth icon in your system tray and choose Setup new device from the menu. Follow the wizard prompts to seek out and pair your headset.
  5. Once paried, open a terminal, and type in the following:
hcitool scan
  

Your PC will now scan for local Bluetooth devices and your headset should appear in the resulting list after a few seconds (along with any other Bluetooth-enabled devices that are in range). The output will look something like:

$ hcitool scan
Scanning ...
	00:11:22:AA:BB:CC	Nokia N95
	00:33:44:DD:EE:FF	BT81
  

In this example, hcitool has found a Nokia mobile phone and a Bluetooth headset and is displaying the MAC addresses for both of them. NOTE: If your headset does not appear you probably already have it paired with something else, like your mobile phone. In that case switch the headset to pairing mode and then run the scan again.

  1. Highlight and copy the MAC address of your headset.
  2. Then type in the following:
sudo gedit ~/.asoundrc
  

Note the period before asoundrc. This will create a new hidden text file called .asoundrc in the root of your Home directory and open GEdit so you can add to it. The file is hidden because of the leading period.

  1. In the text editor, type in the following, replacing the MAC address with the one you copied earlier:
pcm.btheadset {
	type bluetooth
	device 00:33:44:DD:EE:FF
	profile “auto”
}
  

10. Save the file and exit GEdit 11. Enable sound drivers by typing the following into a terminal:

sudo hciconfig hci0 voice 0x0060
  

This will enable your Bluetooth adapter to carry Bluetooth audio. 12. Now we need to tell PulseAudio that your Bluetooth headset exists:

pactl load-module module-alsa-sink device=btheadset
pactl load-module module-alsa-source device=btheadset
  

Note: This enables your Bluetooth headset for PulseAudio only temporarily. When you reboot, the PulseAudio configuration for Bluetooth will be lost. For future convenience, create a bash script with the above commands in it and create a launcher on your desktop to run the commands when you double-click on the launcher icon. Due to needing to have the headset paired BEFORE you run these commands, you can NOT have these commands run automatically during system startup. It will cause PulseAudio to fail. Caution: The second pactl command may cause unusual undesired system behaviour. Since the second command only exists to setup the microphone on your headset, if you do not have one or don't intend to use the microphone, you may omit this line. 13. Once pairing has completed, we can now test to see if we can send audio to the headset. In your terminal, type in the following:

$ aplay -D btheadset -f s16_le /usr/share/sounds/ubuntu/stereo/dialog-question.wav
  

For Ubuntu 9.10 Karmic Koala use this:

$ aplay -D btheadset -f s16_le /usr/share/sounds/alsa/Front_Center.wav
  

This will attempt direct communication with your headset. Within a second or so you should suddenly hear the familiar Ubuntu "login ready" drum sound play through your headset! If you didn't hear it first time try the command a second time as there may be a delay between "activating" your headset and playing sound. Unfortunately, only aplay will play anything through your headset at the moment. All other sounds such as from rhythmbox or totem are still coming through your speakers. Unless the application in question can redirect audio to another detected device it will always play through the standard-out. To fix this we need to make use of the PulseAudio Server which can redirect all output to another device. The PulseAudio Server is already installed by default in Jaunty, so we just need to install some tools to manipulate it. 14. Go back to your terminal and type in the following:

$ sudo apt-get install paprefs paman padevchooser
  

This will install the PulseAudio Preferences app, the PulseAudio Manager app and the PulseAudio Device Chooser app. 15. Once installed open the PulseAudio Device Chooser by going to Applications | Sound & Video | PulseAudio Device Chooser. This will add a black microphone jack icon to your system tray. 16. Open the PulseAudio Manager by left-clicking on the jack icon and choosing Manager. 17. If not already connected, click on the Connect button to connect to your local PulseAudio server. When connected, you will see details about it listed. 18. Click the Devices tab and under Sinks you should see an entry for alsa_output.btheadset. This is picked up directly from your .asoundrc file. 19. Now go to the Sample Cache tab. You are shown a list of sounds. Choose a WAV file from this list (it won't play any other format). At the bottom is a Playback on drop-down. Choose alsa_output.btheadset from this list and click on the Play button. You should hear the Ubuntu login sound through your headset. This proves to us that PulseAudio can play through your Bluetooth headset (but this is NOT the redirection - this is just a test). 20. Close the PulseAudio Manager 21. Left-click on the mic jack icon in your system tray. 22. Go to Default Sink and then choose Other from the sub-menu. In the new window that opens type in:

alsa_output.btheadset
  

Click OK 23. Test the redirection by playing an audio or video file. You should now hear sound through your Bluetooth headset. NOTE: Existing audio streams at the time of changing the sink will continue to play through whatever they were playing through until stopped and started again. 24. To switch back to your speakers simply click on the mic jack icon, in your system tray, choose Default Sink and then choose Default from the sub-menu. The next audio stream played will go back through your speakers. 25. To make the PulseAudio Device Chooser start automatically on startup, click on the mic jack icon again, choose Preferences from the menu and then click on Start applet on Session Login in the window.

Known Issues

  • This does not work with Skype, despite the "btheadset" device being listed as a Sound Device option within Skype. You will get errors when it tries to playback or record audio via the headset and it will in fact kill the PulseAudio server forcing you to restart PulseAudio or restart Ubuntu to get it running again.
  • You cannot have your headset auto-pair and be auto-configured with PulseAudio upon startup (yet?). You will need to pair first, then run the two "pactl" commands in step 10 manually or via a script launcher. You cannot have these commands auto-run on startup or PulseAudio will hang or crash (because the pairing with your headset has not been established yet).
  • The Sound Recorder is unable to lock onto the headset for recording audio (in fact, it goes nuts when trying to record).

Possible autorun script

Create a file called autorun_bt.headset:

gedit autorun_bt.headset
  

Put the following into the file:

echo "Enable sound drivers by typing the following into a terminal"
clear
gksudo hciconfig hci0 voice 0x0060
sleep 1

echo "Now we need to tell PulseAudio that your Bluetooth headset exists"
pactl load-module module-alsa-sink device=btheadset
sleep 8
pactl load-module module-alsa-source device=btheadset
sleep 2

echo "Ready"
sleep 2
  

Save the file and exit GEdit Type the following to make the file executable:

chmod a+x autorun_bt.headset
  

Double click on the file. Alternatively, run the script from a terminal:

./autorun_bt.headset
  

The script do a "sleep" or delay between the "pactl" instructions, due to the fact that the audio buffer may be working and we need to delay this. PulseAudio will crash if you run the second instruction too quickly, requiring you to kill it to start again. Instructions 21-25 are not automated, you will need to set up your headset as the default sink every time you re-connect your Bluetooth headset.