Tuesday, November 26, 2013

How to connect to Bluetooth headset/soundbar/speaker

Most modern laptops are equipped with the Bluetooth radio. It means that you can use a Bluetooth mouse and keyboard as your input device, and a Bluetooth headset, soundbar, or speaker for your sound output. This article gives an example of how to connect your Linux laptop to a Bluetooth soundbar.

Device model

My laptop is a DELL Vostro 1015 running Ubuntu 12.04.3 LTS ("precise"). The bluez Bluetooth stack is of version 4.98.

My soundbar is the Panasonic HTB450. According to the Owner's Manual, this model features Bluetooth V2.1 + EDR. Note that this model is not a Bluetooth Low Energy device. The method in this blog post has only been tested on non-Low-Energy Bluetooth devices.

Getting ready

  • Install the bluez-tools package on your Linux computer (assuming it is Debian-based):
    $ sudo apt-get install bluez-tools
    
  • Power up the Panasonic soundbar. Make sure that Bluetooth is selected as the audio source, and that the device is configured to be discoverable to other Bluetooth devices.
  • The soundbar and the Linux laptop must be within a "short" distance of each other (10 feet to be safe).

Connection procedure

The following assumes you have only one Bluetooth adapter in your Linux computer. If you have more than 1, use the hciconfig command to find out the name of the Bluetooth adapter to use for connection, and specify that device name as a parameter to the commands below.

  1. Discover the soundbar.

    Run the following command on Linux to scan for the soundbar. The key piece of data to jot down is the Bluetooth address of the soundbar (e.g., 00:0B:97:0F:C5:2D).

    $ bt-adapter -d
    Searching...
    [00:0B:97:0F:C5:2A]
      Name: SC-HTB450
      Alias: SC-HTB450
      Address: 00:0B:97:0F:C5:2D
      Icon: audio-card
      Class: 0x240404
      LegacyPairing: 0
      Paired: 0
      RSSI: -56
    Done
    

    You can also scan using the hcitool command:

    $ hcitool scan
    Scanning ...
     00:0B:97:0F:C5:2D    SC-HTB450
    
  2. Pair with the soundbar.

    This step is only necessary if the 2 devices have never been "paired" before.

    To pair, run the bt-device command with the Bluetooth address from step 1 as the parameter.

    $ bt-device -c 00:0B:97:0F:C5:2D
    Connecting to: 00:0B:97:0F:C5:2D
    Agent registered
    Agent released
    Done
    

    You can verify the result by listing the paired devices:

    $ bt-device -l
    Added devices:
    SC-HTB450 (00:0B:97:0F:C5:2D)
    
  3. Connect audio output to soundbar.
    $ bt-audio -c 00:0B:97:0F:C5:2D
    Connecting to an audio service
    Audio service is connected
    

    You can verify the connection by querying information about the Bluetooth device name (SC-HTB450). Note that the output contains a line with Connected equals 1 (meaning success!)

    $ bt-device -i SC-HTB450
    [00:0B:97:0F:C5:2A]
      Name: SC-HTB450
      Alias: SC-HTB450 [rw]
      Address: 00:0B:97:0F:C5:2D
      Icon: audio-card
      Class: 0x240404
      Paired: 1
      Trusted: 0 [rw]
      Blocked: 0 [rw]
      Connected: 1
      UUIDs: [AudioSink, AVRemoteControl]
    
  4. Make soundbar a trusted device.

    After the soundbar is turned off and then on again, the Bluetooth audio connection is NOT automatically reconnected. To make re-connection automatic, run the following command to designate the soundbar as a trusted device.

    $ bt-device --set  SC-HTB450 Trusted 1
    Trusted: 0 -> 1
    

    Note that if you reboot your laptop, you need to manually re-connect using bt-audio -c as shown above (but pairing can be skipped). This is true even when the Bluetooth device is designated as trusted.

Playback sound

The audio connection is now made between your laptop and the soundbar.

You can play your favourite sound track and the sound will come from the soundbar, instead of the built-in laptop speakers.

p.s. You may be interested in my 2 earlier posts on Bluetooth:

Thursday, November 14, 2013

How to modify the user-friendly Bluetooth local name

Each Bluetooth device has a user-friendly local name as well as an unique 48-bit hexadecimal device address (eg, C0:F8:DA:9D:CF:DF). The default local name may be too generic, eg, ubuntu-0, to identify you to the rest of the world. This post shows you how to assign a more descriptive local name.

To find out the current local name of the Bluetooth device, execute the following command:

$ hciconfig -a
hci0: Type: BR/EDR  Bus: USB
 BD Address: C0:F8:DA:9D:CF:DF  ACL MTU: 1022:8  SCO MTU: 121:3
 UP RUNNING PSCAN ISCAN 
    <..sniped>
 Link policy: RSWITCH HOLD SNIFF 
 Link mode: SLAVE ACCEPT 
 Name: 'ubuntu-0'
 Class: 0x6e0100
 Service Classes: Networking, Rendering, Capturing, Audio, Telephony
 Device Class: Computer, Uncategorized
 HCI Version: 3.0 (0x5)  Revision: 0x9999
 LMP Version: 3.0 (0x5)  Subversion: 0x9999
 Manufacturer: Atheros Communications, Inc. (69)

The name that other Bluetooth devices will see is ubuntu-0. Suppose you want to rename the device penguin. Execute the following command with root privilege:

$ sudo hciconfig hci0 name penguin

To verify that the name was modified:

$ hciconfig -a
hci0: Type: BR/EDR  Bus: USB
 BD Address: C0:F8:DA:9D:CF:DF  ACL MTU: 1022:8  SCO MTU: 121:3
 UP RUNNING PSCAN ISCAN 
    <..sniped>
 Link policy: RSWITCH HOLD SNIFF 
 Link mode: SLAVE ACCEPT 
 Name: 'penguin'
 Class: 0x6e0100
 Service Classes: Networking, Rendering, Capturing, Audio, Telephony
 Device Class: Computer, Uncategorized
 HCI Version: 3.0 (0x5)  Revision: 0x9999
 LMP Version: 3.0 (0x5)  Subversion: 0x9999
 Manufacturer: Atheros Communications, Inc. (69)

The local name was only temporarily modified to penguin. It will revert back to ubuntu-0 at the next system reboot or the next Bluetooth daemon restart.

To make the change permanent, manually edit the Bluetooth configuration file. The config file is located in /var/lib/bluetooth/ under the sub-directory named by the Bluetooth device address (see below).

$ cat /var/lib/bluetooth/C0\:F8\:DA\:9D\:CF\:DF/config 
name ubuntu-0
pairable yes
class 0x6e0100
onmode connectable
mode connectable

Modify the line name ubuntu-0 to name penguin, and save the file.

$ sudo vi /var/lib/bluetooth/C0\:F8\:DA\:9D\:CF\:DF/config 

Now, your new Bluetooth local name will persist even after a system reboot.

If you are interested in Bluetooth, please see my earlier post:

Thursday, November 7, 2013

Verify if a Linux computer has the Bluetooth hardware

Many laptops today come with a Bluetooth radio. For desktops however, most likely you need to go buy a Bluetooth USB dongle.

If you don't know whether your computer has the Bluetooth hardware, the following command will help you find out.

$ lsusb |grep Bluetooth
Bus 004 Device 003: ID 0cf3:3005 Atheros Communications, Inc. AR3011 Bluetooth
$ 

If the search returns a Bluetooth device, it indicates strongly that your computer has a Bluetooth chip in it.

Note that simply searching for Bluetooth in your kernel ring buffer is NOT conclusive. My Debian desktop computer does not have the Bluetooth radio, but dmesg returns Bluetooth nevertheless.

$ dmesg |grep -i Bluetooth
[   16.383573] Bluetooth: Core ver 2.16
[   16.383591] Bluetooth: HCI device and connection manager initialized
[   16.383593] Bluetooth: HCI socket layer initialized
[   16.383595] Bluetooth: L2CAP socket layer initialized
[   16.383600] Bluetooth: SCO socket layer initialized
[   16.404892] Bluetooth: RFCOMM TTY layer initialized
[   16.404900] Bluetooth: RFCOMM socket layer initialized
[   16.404902] Bluetooth: RFCOMM ver 1.11
[   18.273794] Bluetooth: BNEP (Ethernet Emulation) ver 1.3
[   18.273797] Bluetooth: BNEP filters: protocol multicast

Armed with the Bluetooth hardware, you are now ready to pair with a Bluetooth device.

If you are interested in Bluetooth, please see my other post:

Tuesday, November 5, 2013

Connect Android and Linux using USB

I have a Samsung Galaxy Tab 2 tablet, running Android 4.1.1 (aka Jelly Bean). I'd like to transfer some files from the tablet to my Linux server running Debian 7.2 (aka Wheezy). The Android tablet connects via Wi-Fi to the same LAN as the Linux server.

There are many ways to transfer files between the 2 devices. For example, you could install an Android app named AirDroid which lets you manage your Android device from a desktop web browser, including file transfer. In this post, I'll go another route, an arguably more direct and basic one. I'll connect the 2 using a USB cable.

  1. Connect the 2 devices using a USB cable.
  2. With the Android device, navigate to the home page.
  3. Swipe down from the top of the page.

    You will see a message "Connected as a media device. Touch for other USB options"

  4. Tap on the message.

    The resulting screen specifies how you want the Android device to connect: as a Media device(MTP) or a Camera (PTP).

    While Linux has some support for MTP, I found it much easier to connect as PTP.

  5. Tap on the Camera (PTP) checkbox.
  6. Swipe down from home page again, and you will see that the tablet is mounted as a camera.
  7. Reset the USB device under Linux.

    The simplest way is to unplug the USB cable from both ends, and plug it back in.

  8. Make sure that the Android device is mounted on your Linux server.

    On my Linux GNOME 3 desktop, I clicked on the Computer icon, and the Android device was detected (GT-P3113). Click the device to mount it.

  9. Run your favourite file manager application on Linux, and proceed to transfer files.

You can use the above method to transfer files if you happen to have (the right) USB cable around to connect the 2 devices. If cables is not your preference, you may want to look into the Android app Airdroid.