File:  [NetBSD Developer Wiki] / wikisrc / Attic / bluetooth.mdwn
Revision 1.2: download - view: text, annotated - select for diffs
Sat Feb 16 12:08:37 2013 UTC (7 years, 8 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?!).

    1: Bluetooth is available since version 4 of NetBSD.
    3: **Contents**
    5: [[!toc]]
    8: #  Background
   10: 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.
   12: 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.
   14: 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.
   16: 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.
   18: Isochronous (SCO) Audio connections may be created on a baseband radio link using either the BTPROTO_SCO socket interface, or the [btsco(4)]( 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.
   20: Support for Human Interface Devices, which operate using the USB HID protocol over a pair of L2CAP channels is provided by the [bthidev(4)]( driver. Currently, keyboards and mice are catered for, and attach to [wscons(4)]( as normal.
   22: Service Discovery in the Bluetooth environment is made available by [libsdp(3)]( and the [sdpd(8)]( 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)]( program.
   24: 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)]( 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)]( 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)]( or [sysutils/btpin-qt](
   27: #  Requirements
   29: The relevant device declarations are included in the GENERIC kernel configurations for the following architectures:
   31:   * alpha
   32:   * amd64
   33:   * i386
   34:   * sparc64
   36: 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.
   38: 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]].
   40: 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.
   43: #  Hardware
   45: If Bluetooth is configured in your kernel, you should see Bluetooth controllers showing up in your boot messages, for example:
   47:     bt3c0 at pcmcia0 function 0: <3COM, 3CRWB60-A, Bluetooth PC Card>
   48:     ubt0 at uhub1 port 4 configuration 1 interface 0
   49:     ubt0: Cambridge Silicon Radio Bluetooth USB Adapter, rev 2.00/19.58, addr 4
   50:     ubt1 at uhub1 port 2 configuration 1 interface 0
   51:     ubt1: Broadcom Belkin Bluetooth Device, rev 1.10/0.01, addr 5
   53: Some PCMCIA and Cardbus devices will attach as serial devices, and they need to be configured to use the [btuart(4)]( line discipline to interface to the Bluetooth protocol stack.
   56: #  Userland configuration
   58: Please edit your [rc.conf(5)]( file and ensure it contains:
   60:     # configure bluetooth controllers
   61:     btconfig=YES
   62:     btconfig_args="up pscan switch class 0x02010c"
   64:     # configure bluetooth devices
   65:     btdevctl=YES
   67:     # start bluetooth key daemon
   68:     bthcid=YES
   70:     # start service discovery daemon
   71:     sdpd=YES
   73: _i had to add 'auth' to the `btconfig_args` line to get my phone to pair._ --[[Jasper]] 22:50, 15 September 2006 (CEST)
   76: _sdpd runs as (user:group) _sdpd:_sdpd, but these are not present in 4.0 -- PR [bin/37125]( [[Shattered]] 14:53, 10 April 2008 (CEST)
   78: which should be ok. Reboot or enter:
   80:     /etc/rc.d/btconfig start
   81:     /etc/rc.d/bthcid start
   82:     /etc/rc.d/sdpd start
   84: and you should be able to see that your device is configured, e.g:
   86:     % btconfig -vvvv ubt0
   87:         ubt0: bdaddr 00:08:1b:8d:ba:6d flags 0x3<UP,RUNNING>
   88:         num_cmd = 1
   89:         num_acl = 8, acl_mtu = 384
   90:         num_sco = 8, sco_mtu = 64
   91:         class: [0x02010c] Laptop Computer <Networking>
   92:         name: "CSR - bc4"
   93:         voice: [0x0060]
   94:         Input Coding: Linear PCM [16-bit, pos 0], 2's complement
   95:         Air Coding: CVSD
   96:         pin: variable
   97:         options: -iscan pscan -auth -encrypt switch -hold sniff -park
   98:         ptype: [0xcc18] DM1 DH1 DM3 DH3 DM5 DH5 2-DH1 3-DH1 2-DH3 3-DH3 2-DH5 3-DH5
   99:         page timeout: 5120 ms
  100:         features: <3 slot> <5 slot> <encryption> <slot offset> <timing accuracy>
  101:         <role switch> <hold mode> <sniff mode> <park mode> <RSSI> <channel quality>
  102:         <SCO link> <HV2> <HV3> <mu-Law log> <A-Law log> <CVSD data> <paging parameter>
  103:         <power control> <transparent SCO> <broadcast encryption> <EDR ACL 2Mbps>
  104:         <EDR ACL 3Mbps> <enhanced inquiry scan> <interlaced inquiry scan>
  105:         <interlaced page scan> <RSSI with inquiry result> <EV3 packets>
  106:         <EV4 packets> <EV5 packets> <AFH capable slave> <AFH class slave>
  107:         <3 slot EDR ACL> <5 slot EDR ACL> <AFH capable master> <AFH class master>
  108:         <EDR eSCO 2Mbps> <EDR eSCO 3Mbps> <3 slot EDR eSCO> <extended features>
  111: #  HID configuration
  113: **Very important**: [bthcid(8)]( must be running.
  115: **Important**: [sysutils/hcidump]( is very useful.
  117: Place device into discoverable mode, and peform an inquiry: 
  119:     % btconfig ubt0 inquiry
  120:     Device Discovery from device: ubt0 .... 1 response
  121:       1: bdaddr 00:0a:95:10:8d:8c (unknown)
  122:        : name "Apple Wireless Mouse"
  123:        : class [0x002580] Peripheral Mouse <Limited Discoverable>
  124:        : page scan rep mode 0x01
  125:        : page scan period mode 0x00
  126:        : page scan mode 0x00
  127:        : clock offset 30537
  129: 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: 
  131:     00:0a:95:10:8d:8c	mouse
  133: 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):
  135:     % btpin -a mouse -p 0000
  136:     % btdevctl -a mouse -d ubt0 -s hid
  137:     local bdaddr: 00:04:76:e1:4a:f5
  138:     remote bdaddr: 00:0a:95:10:8d:8c
  139:     link mode: auth
  140:     device type: bthidev
  141:     control psm: 0x0011
  142:     interrupt psm: 0x0013
  143:     Collection page=Generic_Desktop usage=Mouse
  144:     Collection page=Generic_Desktop usage=Pointer
  145:       Input id=2 size=1 count=1 page=Button usage=Button_1 Variable, logical range 0..1
  146:       Input id=2 size=7 count=1 page=0x0000 usage=0x0000 Const, logical range 0..1
  147:       Input id=2 size=8 count=1 page=Generic_Desktop usage=X Variable Relative, logical range -127..127
  148:       Input id=2 size=8 count=1 page=Generic_Desktop usage=Y Variable Relative, logical range -127..127
  149:     Feature id=71 size=8 count=1 page=0x0006 usage=0x0020 Variable NoPref, logical range 0..100
  150:     End collection
  151:     End collection
  153: Then try to attach it to the system:
  155:     % btdevctl -a mouse -d ubt0 -s hid -A
  157: which should attach the mouse to the system and attempt to open a bluetooth connection for it. You should see something like: 
  159:     bthidev0 at bthub0 remote-bdaddr 00:0a:95:10:8d:8c link-mode auth
  160:     btms0 at bthidev0 reportid 2: 1 button.
  161:     wsmouse2 at btms0 mux 0
  162:     bthidev0: connected
  164: on the console, and the mouse should work.
  166: 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:
  168:     % btpin -a keyboard -r
  169:     PIN: 3537
  170:     % btdevctl -a keyboard -d ubt0 -s hid -A -v
  174:     local bdaddr: 00:04:76:e1:4a:f5
  175:     remote bdaddr: 00:0a:95:45:a4:a0
  176:     link mode: encrypt
  177:     device type: bthidev
  178:     control psm: 0x0011
  179:     interrupt psm: 0x0013
  180:     Collection page=Generic_Desktop usage=Keyboard
  181:       Input id=1 size=1 count=1 page=Keyboard usage=Keyboard_LeftControl Variable, logical range 0..1
  182:       Input id=1 size=1 count=1 page=Keyboard usage=Keyboard_LeftShift Variable, logical range 0..1
  183:       Input id=1 size=1 count=1 page=Keyboard usage=Keyboard_LeftAlt Variable, logical range 0..1
  184:       Input id=1 size=1 count=1 page=Keyboard usage=Keyboard_Left_GUI Variable, logical range 0..1
  185:       Input id=1 size=1 count=1 page=Keyboard usage=Keyboard_RightControl Variable, logical range 0..1
  186:       Input id=1 size=1 count=1 page=Keyboard usage=Keyboard_RightShift Variable, logical range 0..1
  187:       Input id=1 size=1 count=1 page=Keyboard usage=Keyboard_RightAlt Variable, logical range 0..1
  188:       Input id=1 size=1 count=1 page=Keyboard usage=Keyboard_Right_GUI Variable, logical range 0..1
  189:       Input id=1 size=8 count=1 page=0x0000 usage=0x0000 Const, logical range 0..1
  190:      Output id=1 size=1 count=1 page=LEDs usage=Num_Lock Variable, logical range 0..1
  191:      Output id=1 size=1 count=1 page=LEDs usage=Caps_Lock Variable, logical range 0..1
  192:      Output id=1 size=1 count=1 page=LEDs usage=Scroll_Lock Variable, logical range 0..1
  193:      Output id=1 size=1 count=1 page=LEDs usage=Compose Variable, logical range 0..1
  194:      Output id=1 size=1 count=1 page=LEDs usage=Kana Variable, logical range 0..1
  195:      Output id=1 size=3 count=1 page=0x0000 usage=0x0000 Const, logical range 0..1
  196:       Input id=1 size=8 count=6 page=Keyboard usage=No_Event, logical range 0..255
  197:       Input id=1 size=1 count=1 page=Consumer usage=Eject Variable Relative, logical range 0..1
  198:       Input id=1 size=1 count=1 page=Consumer usage=Mute Variable Relative, logical range 0..1
  199:       Input id=1 size=1 count=1 page=Consumer usage=Volume_Up Variable, logical range 0..1
  200:       Input id=1 size=1 count=1 page=Consumer usage=Volume_Down Variable, logical range 0..1
  201:       Input id=1 size=1 count=4 page=0x0000 usage=0x0000 Const, logical range 0..1
  202:     End collection
  204: And you see something like this on the console: 
  206:     bthidev0 at bthub0 remote-bdaddr 00:0a:95:45:a4:a0 link-mode encrypt
  207:     btkbd0 at bthidev0 reportid 1
  208:     wskbd1 at btkbd0 mux 1
  209:     wskbd1: connecting to wsdisplay0
  210:     bthidev0: connected
  212: By default, on NetBSD systems that support it, [btdevctl(8)]( will request authentication for the baseband link, or encryption if the HID is a keyboard.
  215: #  Serial connections
  217: **See [rfcomm_sppd(8)](**
  219: Set the phone into discoverable mode:
  221:     % btconfig ubt0 inq
  223: add alias to `/etc/bluetooth/hosts`, then:
  225:     % btpin -a phone -p 334323
  226:     % sdpquery -a phone browse
  228: will make the initial connection and cause [bthcid(8)]( to store link keys. At this point, you should be paired with the phone and can do:
  230:     % rfcomm_sppd -a phone -s DUN -t /dev/ttyp9
  231:     % cu -l /dev/ttyp9
  233: and find yourself connected to the modem directly. In order to use PPP, you would add the line:
  235:     pty "rfcomm_sppd -a phone -s DUN"
  237: to your `/etc/ppp/options` file in place of the normal tty declaration and starting [pppd(8)]( will cause the link to be created as required.
  239: 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)]( will be cached by [bthcid(8)]( 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)]( for details.
  242: #  SCO Audio Headsets
  244: **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)]( and [btconfig(8)](
  246: 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)]( device to the system:
  248:     % btconfig ubt0 inquiry
  249:     [...]
  250:     % btpin -a headset -p 0000
  251:     % btdevctl -a headset -d ubt0 -s hset -A
  253: which should produce something like this: 
  255:     btsco0 at bthub0 remote-bdaddr 00:07:a4:23:10:83
  256:     audio1 at btsco0: full duplex
  258: on the system console. In order to utilise the audio device, you will need to open a control connection with [bthset(1)]( which conveys volume information to the mixer device. Using -v will let you see what is going on: 
  260:     % bthset -m /dev/mixer1 -v
  262: and you should now be able to transfer 8khz samples to/from /dev/audio1 using [audioplay(1)]( and [audiorecord(1)]( 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.
  264:     % audiorecord -d /dev/audio1
  266:     ^C
  267:     % audioplay -d /dev/audio
  268: [gosh, do I really sound like that?] 
  269:     % audioplay -d /dev/audio1
  270: [in the headset!] 
  272: 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! 
  275: #  SCO Audio Handsfree 
  277: **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)]( and [btconfig(8)](
  279: The method shown above for SCO Audio Headsets also works to give [audio(4)]( 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)](
  281: 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](
  284: #  OBEX Push/Ftp
  286: See the [comms/obexftp]( and [comms/obexapp]( packages in pkgsrc.
  288: #  Why doesn't it work, eh?
  290:   1. A NetBSD version of the hcidump program is available in [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.
  291:   2. the [bthcid(8)]( program should be running, and logs debugging messages with facility "daemon.debug". See [syslog.conf(5)]( for details.
  292:   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)
  293:   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`
  294:   5. to use SCO Audio with USB Bluetooth controllers, the MTU will need to be set. See the CAVEAT section of [ubt(4)]( for details.
  295:   6. Isochronous data is not well supported in some Belkin/Broadcom USB Bluetooth dongles.
  296:   7. Isochronous data is not supported on [ehci(4)]( USB host controllers.
  297:   8. SCO audio does not work really well with the [bt3c(4)]( device
  298:   9. some older Belkin/Broadcom USB devices need firmware loaded before they will work. See the [sysutils/bcmfw]( package.
  300: #  References
  302:   * [Bluetooth Imported.](
  303:   * [bthfp source.](

CVSweb for NetBSD wikisrc <> software: FreeBSD-CVSweb