File:  [NetBSD Developer Wiki] / wikisrc / bluetooth.mdwn
Revision 1.2: download - view: text, annotated - select for diffs
Sat Feb 16 12:08:37 2013 UTC (14 months ago) by wiki
Branches: MAIN
CVS tags: HEAD
web commit by jdf: Remove superfluous newlines in the source code (did come in with the import?!).

Bluetooth is available since version 4 of NetBSD.

**Contents**

[[!toc]]


#  Background

Bluetooth is a digital radio protocol used for short range and low power communications. NetBSD 4.0 introduced support for the Bluetooth protocol stack, and some integration of device profiles into the NetBSD device framework.

The lower layers of the Bluetooth protocol stack pertaining to actual radio links between devices are handled inside the Bluetooth Controller, which communicates with the Host computer using the Host Controller Interface (HCI) protocol which can be accessed via a raw packet BTPROTO_HCI socket interface.

The lowest useable protocol is the Link Layer Control and Adaptation Protocol (L2CAP), which can be accessed via a BTPROTO_L2CAP socket interface. This provides sequential packet access to remote devices, with up to 64k channels per radio link. When an L2CAP channel is opened, the protocol or service that is required is identified by a Protocol/Service Multiplexer (PSM) value.

One available protocol is RFCOMM, which provides up to 30 emulated serial connections multiplexed over a single L2CAP channel. This streamed data protocol can be accessed using the BTPROTO_RFCOMM socket interface though it is planned to also provide a direct tty interface later. Typically, many services use RFCOMM links to provide a control interface using the AT command set.

Isochronous (SCO) Audio connections may be created on a baseband radio link using either the BTPROTO_SCO socket interface, or the [btsco(4)](http://netbsd.gw.com/cgi-bin/man-cgi?btsco+4+NetBSD-current) audio device driver. While the specification says that up to three such links can be made between devices, the current Bluetooth code can only handle one with any dignity.

Support for Human Interface Devices, which operate using the USB HID protocol over a pair of L2CAP channels is provided by the [bthidev(4)](http://netbsd.gw.com/cgi-bin/man-cgi?bthidev+4+NetBSD-current) driver. Currently, keyboards and mice are catered for, and attach to [wscons(4)](http://netbsd.gw.com/cgi-bin/man-cgi?wscons+4+NetBSD-current) as normal.

Service Discovery in the Bluetooth environment is made available by [libsdp(3)](http://netbsd.gw.com/cgi-bin/man-cgi?libsdp+3+NetBSD-current) and the [sdpd(8)](http://netbsd.gw.com/cgi-bin/man-cgi?sdpd+8+NetBSD-current) daemon (both ported from FreeBSD), which allows programs to register services and makes the information available to remote devices performing queries. Limited querying can be done with the [sdpquery(1)](http://netbsd.gw.com/cgi-bin/man-cgi?sdpquery+1+NetBSD-current) program.

Security on Bluetooth links is supplied by encryption and authentication options. In NetBSD-4.0, these can only be applied to all baseband links that a controller makes, by [btconfig(8)](http://netbsd.gw.com/cgi-bin/man-cgi?btconfig+8+NetBSD-current). For NetBSD-current versions after 21 April 2007, L2CAP and RFCOMM connections may additionally specify a minimum link mode for the baseband link which is applied during connection establishment. When authentication is requested, a PIN is presented by each side, generally entered by the operator (some limited input devices have a fixed PIN). The controller uses this PIN to generate a Link Key and reports this to the host computer and will ask for it to authenticate subsequent connections. On NetBSD, the [bthcid(8)](http://netbsd.gw.com/cgi-bin/man-cgi?bthcid+8+NetBSD-current) daemon is responsible for storing these and responding to Link Key requests, and also provides an interface to allow users to specify a PIN with a PIN client, such as [btpin(1)](http://netbsd.gw.com/cgi-bin/man-cgi?btpin+1+NetBSD-current) or [sysutils/btpin-qt](http://pkgsrc.se/sysutils/btpin-qt).


#  Requirements

The relevant device declarations are included in the GENERIC kernel configurations for the following architectures:

  * alpha
  * amd64
  * i386
  * sparc64

For other architectures, you will need to add the device declarations to your kernel config file. Bluetooth should work on any architecture that has USB and/or PCMCIA available.

Please notice that you will need NetBSD 4.0 or higher to use bluetooth. Since 4.0 is tagged BETA as of August 31, you will need a fairly current kernel. To build a current kernel, please read [[How to build NetBSD-current]].

If you are looking to purchase a Bluetooth controller for use with NetBSD, the better devices are USB based, and a v2.0 device is better than v1.2. The Class of the device indicates the power of the radio - Class 1 is 100m and Class 2 is 10m.


#  Hardware

If Bluetooth is configured in your kernel, you should see Bluetooth controllers showing up in your boot messages, for example:

    bt3c0 at pcmcia0 function 0: <3COM, 3CRWB60-A, Bluetooth PC Card>
    ubt0 at uhub1 port 4 configuration 1 interface 0
    ubt0: Cambridge Silicon Radio Bluetooth USB Adapter, rev 2.00/19.58, addr 4
    ubt1 at uhub1 port 2 configuration 1 interface 0
    ubt1: Broadcom Belkin Bluetooth Device, rev 1.10/0.01, addr 5

Some PCMCIA and Cardbus devices will attach as serial devices, and they need to be configured to use the [btuart(4)](http://netbsd.gw.com/cgi-bin/man-cgi?btuart+4+NetBSD-current) line discipline to interface to the Bluetooth protocol stack.


#  Userland configuration

Please edit your [rc.conf(5)](http://netbsd.gw.com/cgi-bin/man-cgi?rc.conf+5+NetBSD-current) file and ensure it contains:

    # configure bluetooth controllers
    btconfig=YES
    btconfig_args="up pscan switch class 0x02010c"
    
    # configure bluetooth devices
    btdevctl=YES
    
    # start bluetooth key daemon
    bthcid=YES
    
    # start service discovery daemon
    sdpd=YES

_i had to add 'auth' to the `btconfig_args` line to get my phone to pair._ --[[Jasper]] 22:50, 15 September 2006 (CEST)


_sdpd runs as (user:group) _sdpd:_sdpd, but these are not present in 4.0 -- PR [bin/37125](http://www.NetBSD.org/cgi-bin/query-pr-single.pl?number=37125)_ [[Shattered]] 14:53, 10 April 2008 (CEST)

which should be ok. Reboot or enter:

    /etc/rc.d/btconfig start
    /etc/rc.d/bthcid start
    /etc/rc.d/sdpd start

and you should be able to see that your device is configured, e.g:

    % btconfig -vvvv ubt0
        ubt0: bdaddr 00:08:1b:8d:ba:6d flags 0x3<UP,RUNNING>
        num_cmd = 1
        num_acl = 8, acl_mtu = 384
        num_sco = 8, sco_mtu = 64
        class: [0x02010c] Laptop Computer <Networking>
        name: "CSR - bc4"
        voice: [0x0060]
        Input Coding: Linear PCM [16-bit, pos 0], 2's complement
        Air Coding: CVSD
        pin: variable
        options: -iscan pscan -auth -encrypt switch -hold sniff -park
        ptype: [0xcc18] DM1 DH1 DM3 DH3 DM5 DH5 2-DH1 3-DH1 2-DH3 3-DH3 2-DH5 3-DH5
        page timeout: 5120 ms
        features: <3 slot> <5 slot> <encryption> <slot offset> <timing accuracy>
        <role switch> <hold mode> <sniff mode> <park mode> <RSSI> <channel quality>
        <SCO link> <HV2> <HV3> <mu-Law log> <A-Law log> <CVSD data> <paging parameter>
        <power control> <transparent SCO> <broadcast encryption> <EDR ACL 2Mbps>
        <EDR ACL 3Mbps> <enhanced inquiry scan> <interlaced inquiry scan>
        <interlaced page scan> <RSSI with inquiry result> <EV3 packets>
        <EV4 packets> <EV5 packets> <AFH capable slave> <AFH class slave>
        <3 slot EDR ACL> <5 slot EDR ACL> <AFH capable master> <AFH class master>
        <EDR eSCO 2Mbps> <EDR eSCO 3Mbps> <3 slot EDR eSCO> <extended features>


#  HID configuration

**Very important**: [bthcid(8)](http://netbsd.gw.com/cgi-bin/man-cgi?bthcid+8+NetBSD-current) must be running.

**Important**: [sysutils/hcidump](http://pkgsrc.se/sysutils/hcidump) is very useful.

Place device into discoverable mode, and peform an inquiry: 

    % btconfig ubt0 inquiry
    Device Discovery from device: ubt0 .... 1 response
      1: bdaddr 00:0a:95:10:8d:8c (unknown)
       : name "Apple Wireless Mouse"
       : class [0x002580] Peripheral Mouse <Limited Discoverable>
       : page scan rep mode 0x01
       : page scan period mode 0x00
       : page scan mode 0x00
       : clock offset 30537

which should get you the BDADDR of the device. for ease of use you might want to add this to `/etc/bluetooth/hosts` so you can refer to it by name: 

    00:0a:95:10:8d:8c	mouse

And now, you should be able to query the device. You will likely need to pair with it first which will happen the first time you connect, so set the PIN (which is probably 0000 for limited input devices but see the manual to be sure):

    % btpin -a mouse -p 0000
    % btdevctl -a mouse -d ubt0 -s hid
    local bdaddr: 00:04:76:e1:4a:f5
    remote bdaddr: 00:0a:95:10:8d:8c
    link mode: auth
    device type: bthidev
    control psm: 0x0011
    interrupt psm: 0x0013
    Collection page=Generic_Desktop usage=Mouse
    Collection page=Generic_Desktop usage=Pointer
      Input id=2 size=1 count=1 page=Button usage=Button_1 Variable, logical range 0..1
      Input id=2 size=7 count=1 page=0x0000 usage=0x0000 Const, logical range 0..1
      Input id=2 size=8 count=1 page=Generic_Desktop usage=X Variable Relative, logical range -127..127
      Input id=2 size=8 count=1 page=Generic_Desktop usage=Y Variable Relative, logical range -127..127
    Feature id=71 size=8 count=1 page=0x0006 usage=0x0020 Variable NoPref, logical range 0..100
    End collection
    End collection

Then try to attach it to the system:

    % btdevctl -a mouse -d ubt0 -s hid -A

which should attach the mouse to the system and attempt to open a bluetooth connection for it. You should see something like: 

    bthidev0 at bthub0 remote-bdaddr 00:0a:95:10:8d:8c link-mode auth
    btms0 at bthidev0 reportid 2: 1 button.
    wsmouse2 at btms0 mux 0
    bthidev0: connected

on the console, and the mouse should work.

Keyboards are only a little more complex, in that you will need to enter the PIN onto the keyboard before you can pair. This will be done at the point where you are opening the first connection, eg:

    % btpin -a keyboard -r
    PIN: 3537
    % btdevctl -a keyboard -d ubt0 -s hid -A -v

< ENTER PIN ON BLUETOOTH KEYBOARD NOW >

    local bdaddr: 00:04:76:e1:4a:f5
    remote bdaddr: 00:0a:95:45:a4:a0
    link mode: encrypt
    device type: bthidev
    control psm: 0x0011
    interrupt psm: 0x0013
    Collection page=Generic_Desktop usage=Keyboard
      Input id=1 size=1 count=1 page=Keyboard usage=Keyboard_LeftControl Variable, logical range 0..1
      Input id=1 size=1 count=1 page=Keyboard usage=Keyboard_LeftShift Variable, logical range 0..1
      Input id=1 size=1 count=1 page=Keyboard usage=Keyboard_LeftAlt Variable, logical range 0..1
      Input id=1 size=1 count=1 page=Keyboard usage=Keyboard_Left_GUI Variable, logical range 0..1
      Input id=1 size=1 count=1 page=Keyboard usage=Keyboard_RightControl Variable, logical range 0..1
      Input id=1 size=1 count=1 page=Keyboard usage=Keyboard_RightShift Variable, logical range 0..1
      Input id=1 size=1 count=1 page=Keyboard usage=Keyboard_RightAlt Variable, logical range 0..1
      Input id=1 size=1 count=1 page=Keyboard usage=Keyboard_Right_GUI Variable, logical range 0..1
      Input id=1 size=8 count=1 page=0x0000 usage=0x0000 Const, logical range 0..1
     Output id=1 size=1 count=1 page=LEDs usage=Num_Lock Variable, logical range 0..1
     Output id=1 size=1 count=1 page=LEDs usage=Caps_Lock Variable, logical range 0..1
     Output id=1 size=1 count=1 page=LEDs usage=Scroll_Lock Variable, logical range 0..1
     Output id=1 size=1 count=1 page=LEDs usage=Compose Variable, logical range 0..1
     Output id=1 size=1 count=1 page=LEDs usage=Kana Variable, logical range 0..1
     Output id=1 size=3 count=1 page=0x0000 usage=0x0000 Const, logical range 0..1
      Input id=1 size=8 count=6 page=Keyboard usage=No_Event, logical range 0..255
      Input id=1 size=1 count=1 page=Consumer usage=Eject Variable Relative, logical range 0..1
      Input id=1 size=1 count=1 page=Consumer usage=Mute Variable Relative, logical range 0..1
      Input id=1 size=1 count=1 page=Consumer usage=Volume_Up Variable, logical range 0..1
      Input id=1 size=1 count=1 page=Consumer usage=Volume_Down Variable, logical range 0..1
      Input id=1 size=1 count=4 page=0x0000 usage=0x0000 Const, logical range 0..1
    End collection

And you see something like this on the console: 

    bthidev0 at bthub0 remote-bdaddr 00:0a:95:45:a4:a0 link-mode encrypt
    btkbd0 at bthidev0 reportid 1
    wskbd1 at btkbd0 mux 1
    wskbd1: connecting to wsdisplay0
    bthidev0: connected

By default, on NetBSD systems that support it, [btdevctl(8)](http://netbsd.gw.com/cgi-bin/man-cgi?btdevctl+8+NetBSD-current) will request authentication for the baseband link, or encryption if the HID is a keyboard.


#  Serial connections

**See [rfcomm_sppd(8)](http://netbsd.gw.com/cgi-bin/man-cgi?rfcomm_sppd+8+NetBSD-current)**

Set the phone into discoverable mode:

    % btconfig ubt0 inq

add alias to `/etc/bluetooth/hosts`, then:

    % btpin -a phone -p 334323
    % sdpquery -a phone browse

will make the initial connection and cause [bthcid(8)](http://netbsd.gw.com/cgi-bin/man-cgi?bthcid+8+NetBSD-current) to store link keys. At this point, you should be paired with the phone and can do:

    % rfcomm_sppd -a phone -s DUN -t /dev/ttyp9
    % cu -l /dev/ttyp9

and find yourself connected to the modem directly. In order to use PPP, you would add the line:

    pty "rfcomm_sppd -a phone -s DUN"

to your `/etc/ppp/options` file in place of the normal tty declaration and starting [pppd(8)](http://netbsd.gw.com/cgi-bin/man-cgi?pppd+8+NetBSD-current) will cause the link to be created as required.

Phones are more complex than HIDs naturally, and you may not need to provide a PIN until later in the process (some wait until a RFCOMM channel is opened before requesting authentication for instance), although the PIN you specify with [btpin(1)](http://netbsd.gw.com/cgi-bin/man-cgi?btpin+1+NetBSD-current) will be cached by [bthcid(8)](http://netbsd.gw.com/cgi-bin/man-cgi?bthcid+8+NetBSD-current) for 5 minutes or until used. For NetBSD-current systems after 21 April 2007, it is possible to specify the minimum link mode requirements, see [rfcomm_sppd(1)](http://netbsd.gw.com/cgi-bin/man-cgi?rfcomm_sppd+1+NetBSD-current) for details.


#  SCO Audio Headsets

**important**: When using SCO Audio with USB devices, you will need to enable isochronous data, and calculate the MTU that the device will use, see [ubt(4)](http://netbsd.gw.com/cgi-bin/man-cgi?ubt+4+NetBSD-current) and [btconfig(8)](http://netbsd.gw.com/cgi-bin/man-cgi?btconfig+8+NetBSD-current)

Discover the BDADDR and pair with the headset as normal (likely the PIN is "0000", but check your manual to be sure). Attach the [btsco(4)](http://netbsd.gw.com/cgi-bin/man-cgi?btsco+4+NetBSD-current) device to the system:

    % btconfig ubt0 inquiry
    [...]
    % btpin -a headset -p 0000
    % btdevctl -a headset -d ubt0 -s hset -A

which should produce something like this: 

    btsco0 at bthub0 remote-bdaddr 00:07:a4:23:10:83
    audio1 at btsco0: full duplex

on the system console. In order to utilise the audio device, you will need to open a control connection with [bthset(1)](http://netbsd.gw.com/cgi-bin/man-cgi?bthset+1+NetBSD-current) which conveys volume information to the mixer device. Using -v will let you see what is going on: 

    % bthset -m /dev/mixer1 -v

and you should now be able to transfer 8khz samples to/from /dev/audio1 using [audioplay(1)](http://netbsd.gw.com/cgi-bin/man-cgi?audioplay+1+NetBSD-current) and [audiorecord(1)](http://netbsd.gw.com/cgi-bin/man-cgi?audiorecord+1+NetBSD-current). Adjusting the mixer values should work when playing though you may find that when opening a connection the headset will reset the volume to the last known settings. Note that you need to leave _bthset_ running while you use audiorecord or audioplay, so either use two terminal windows or a trailing '&' on the previous command to move it to the background.

    % audiorecord -d /dev/audio1 voice.au
< TALK NONSENSE NOW >
    ^C
    % audioplay -d /dev/audio voice.au
[gosh, do I really sound like that?] 
    % audioplay -d /dev/audio1 voice.au
[in the headset!] 

You can play with mpg123 and sox to play music (though only in mono), or it should hopefully be fairly easy to make the headset work with a VoIP application, please let me know if you get that working! 


#  SCO Audio Handsfree 

**important**: When using SCO Audio with USB devices, you will need to enable isochronous data, and calculate the MTU that the device will use, see [ubt(4)](http://netbsd.gw.com/cgi-bin/man-cgi?ubt+4+NetBSD-current) and [btconfig(8)](http://netbsd.gw.com/cgi-bin/man-cgi?btconfig+8+NetBSD-current)

The method shown above for SCO Audio Headsets also works to give [audio(4)](http://netbsd.gw.com/cgi-bin/man-cgi?audio+4+NetBSD-current) access to the Handsfree profile on a mobile phone (where the phone initiates audio connections), though in practice the [audio(4)][47] device does not quite handle the concept of 'connection lost' which we need. You can configure the 'HF' service with [btdevctl(8)](http://netbsd.gw.com/cgi-bin/man-cgi?btdevctl+8+NetBSD-current).

Alternatively there is a partial implementation of a handsfree profile program that opens a control connection to an Audio Gateway (ie a phone) and copies audio data to and from the system audio device as appropriate. Currently it is shell only and really it would be better with a graphical interface. See [comms/bthfp](http://pkgsrc.se/comms/bthfp)


#  OBEX Push/Ftp

See the [comms/obexftp](http://pkgsrc.se/comms/obexftp) and [comms/obexapp](http://pkgsrc.se/comms/obexapp) packages in pkgsrc.

#  Why doesn't it work, eh?

  1. A NetBSD version of the hcidump program is available in [sysutils/hcidump](http://pkgsrc.se/sysutils/hcidump) and can be very useful to snoop on packets in and out at the device level to find out what is happening when things are not going right.
  2. the [bthcid(8)](http://netbsd.gw.com/cgi-bin/man-cgi?bthcid+8+NetBSD-current) program should be running, and logs debugging messages with facility "daemon.debug". See [syslog.conf(5)](http://netbsd.gw.com/cgi-bin/man-cgi?syslog.conf+5+NetBSD-current) for details.
  3. Dave Tyson has done some excellent testing and found that some devices fail to work unless you set the packet type to 0x0018, which indicates DM1 and DH1 packets only. (I've put that on my list to investigate further, it should not be so)
  4. for device configuration to persist across reboots, you need to add an entry to the `/etc/bluetooth/btdevctl.conf` file and set btdevctl=YES in `/etc/rc.conf`
  5. to use SCO Audio with USB Bluetooth controllers, the MTU will need to be set. See the CAVEAT section of [ubt(4)](http://netbsd.gw.com/cgi-bin/man-cgi?ubt+4+NetBSD-current) for details.
  6. Isochronous data is not well supported in some Belkin/Broadcom USB Bluetooth dongles.
  7. Isochronous data is not supported on [ehci(4)](http://netbsd.gw.com/cgi-bin/man-cgi?ehci+4+NetBSD-current) USB host controllers.
  8. SCO audio does not work really well with the [bt3c(4)](http://netbsd.gw.com/cgi-bin/man-cgi?bt3c+4+NetBSD-current) device
  9. some older Belkin/Broadcom USB devices need firmware loaded before they will work. See the [sysutils/bcmfw](http://pkgsrc.se/sysutils/bcmfw) package.

#  References

  * [Bluetooth Imported.](http://mail-index.netbsd.org/current-users/2006/06/19/0022.html)
  * [bthfp source.](http://homepages.rya-online.net/plunky/bthfp-0.1.tar.gz)

CVSweb for NetBSD wikisrc <wikimaster@NetBSD.org> software: FreeBSD-CVSweb