Add bluetooth post.
[blog.git] / posts / Bluetooth.mdwn
1 I've been setting up a USB-bluetooth adapter so I can transfer files
2 between my phone and computer.  You'll need kernel support (in
3 `menuconfig` look in `Networking ---> Bluetooth subsystem support`).
4
5 With a proper kernel, after plugging in your adapter you should see
6 something like
7
8     $ lsmod
9     Module                  Size  Used by
10     sco                     6484  2 
11     bnep                    7889  2 
12     rfcomm                 25356  4 
13     l2cap                  27398  16 bnep,rfcomm
14     btusb                   8762  2 
15     bluetooth              42079  9 sco,bnep,rfcomm,l2cap,btusb
16     ...
17     $ lsusb
18     Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
19     Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
20     Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
21     Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
22     Bus 003 Device 010: ID 0a5c:4500 Broadcom Corp. BCM2046B1 USB 2.0 Hub (part of BCM2046 Bluetooth)
23     Bus 003 Device 011: ID 0a5c:4502 Broadcom Corp. Keyboard (Boot Interface Subclass)
24     Bus 003 Device 012: ID 0a5c:4503 Broadcom Corp. Mouse (Boot Interface Subclass)
25     Bus 003 Device 013: ID 0a5c:2154 Broadcom Corp. 
26
27 Once you've got the kernel working, you should install [BlueZ][], the
28 userspace Bluetooth stack.  On Gentoo, you'll want to enable the
29 `test-programs` USE flag (which brings in additional stuff like
30 `simple-agent`).  Then run
31
32     $ sudo emerge -av net-wireless/bluez
33
34 This brings in a bunch of tools, among them `hcitool` and `hciconfig`.
35 Check that BlueZ finds your adapter with:
36
37     $ hcitool dev
38     Devices:
39             hci0    00:01:02:03:04:05
40
41 Put your phone in "discovery mode", and then detect it from the
42 computer with:
43
44     $ hcitool scan
45     Scanning ...
46             00:0A:0B:0C:0D:0E       Grayhat
47
48 Verify communication with:
49
50     $ sudo l2ping 00:0A:0B:0C:0D:0E
51     Ping: 00:0A:0B:0C:0D:0E from 00:01:02:03:04:05 (data size 20) ...
52     20 bytes from 00:0A:0B:0C:0D:0E id 200 time 69.85ms
53     20 bytes from 00:0A:0B:0C:0D:0E id 201 time 9.97ms
54     20 bytes from 00:0A:0B:0C:0D:0E id 202 time 56.86ms
55     20 bytes from 00:0A:0B:0C:0D:0E id 203 time 39.92ms
56     4 sent, 4 received, 0% loss
57
58
59 Pairing
60 =======
61
62 Make computer discoverable with:
63
64     $ sudo hciconfig hci0 piscan
65
66 which adds ISCAN to hciconfig's output.
67
68 To setup pairing, you should setup a pair pin by editing
69
70     $ cat /var/lib/bluetooth/00:01:02:03:04:05/pincodes
71     00:0A:0B:0C:0D:0E 12345
72     AA:AA:AA:AA:AA:AA 67890
73     $ /etc/init.d/bluetooth restart
74
75 where `00:01:02:03:04:05` is the address for your adapter (from
76 `hcitool dev`) and the file contains `addr pin` lines.  You can get the
77 addresses of the pair devices with `hcitool scan` (discussed above).
78 After you edit the `pincodes` file, you'll have to restart the
79 bluetooth daemon.
80
81     $ sudo /etc/init.d/bluetooth restart
82
83 If you want to use `simple-agent` instead of mucking about in
84 `/var/lib/bluetooth`, usage looks something like
85
86     $ simple-agent
87     Agent registered
88     RequestPinCode (/org/bluez/16306/hci0/dev_00_0A_0B_0C_0D_0E)
89     Enter PIN Code: 12345
90
91 where you start `simple-agent` and then initiate pairing from the
92 phone.  After entering a pin on the phone, you will be prompted to
93 re-enter the pin by `simple-agent`.
94
95 Gentoo may need to be told that the cell phone is allowed to connect,
96 so create/edit `/var/lib/bluetooth/00:01:02:03:04:05/trusts`
97
98     $ cat /var/lib/bluetooth/00:01:02:03:04:05/trusts
99     00:0A:0B:0C:0D:0E [all]
100
101 which allows the phone to use any Bluetooth service provided by your
102 computer.  It's possible that you can accomplish the same result with
103 something like
104
105     $ sudo bluez-test-device trusted 00:0A:0B:0C:0D:0E yes
106
107 but I haven't been able to figure out the right syntax yet.
108
109
110 Pushing files from the phone
111 ============================
112
113 There are a number of OBEX push servers you can run on your box.  My
114 favorite so far is [ObexPushD][], which is easy to use, GPLv2
115 licensed, and Git versioned.  Can't beat that.  I've added an ebuild
116 (`app-mobilephone/obexpushd`) to my [[Gentoo overlay]] for easier
117 installation.
118
119
120 Pushing files to the phone
121 ==========================
122
123 Check for an OBEX Push service on the phone:
124
125     $ sdptool search --bdaddr 00:0A:0B:0C:0D:0E OPUSH
126     Searching for OPUSH on 00:0A:0B:0C:0D:0E ...
127     Service Name: Object Exchange
128     Service RecHandle: 0x10003
129     Service Class ID List:
130       "OBEX Object Push" (0x1105)
131     Protocol Descriptor List:
132       "L2CAP" (0x0100)
133       "RFCOMM" (0x0003)
134         Channel: 3
135       "OBEX" (0x0008)
136     Profile Descriptor List:
137       "OBEX Object Push" (0x1105)
138         Version: 0x0100
139
140 Push a file to the phone using `ussp-push` (`net-wireless/ussp-push`)
141
142     $ ussp-push 00:0A:0B:0C:0D:0E@3 LFILE RFILE
143
144 which pushes the local `LFILE` to the remote `RFILE`.
145
146 Note that phone's can be finicky about what they accept.  For example,
147 my Casio C721 is running BREW 3.x (see my [[Java ME]] post), and has a
148 number of issues:
149
150 * It attempts to detect file type by file extension (so you can't send
151   over a JPG with a `.jpeg` extension).  It pops up an "Unsupported
152   Media" warning if you try.
153 * It does not accept music in any of the following formats: (AAC
154   (`.aac`), MP3 (`.mp3`), MP4/AAC (`.mp4`, `.m4a`), WAV (`.wav`).
155 * It does accept JPGs with a `.jpg` extension.
156 * It does accept VCARDs (with a `.vcf` extension), but they must have
157   DOS endlines (e.g. via `unix2dos`).
158 * If you include multiple VCARDs in a single file, it enters all the
159   information under the name from the first VCARD.
160 * If you send a VCARD whos name matches an already existing contact,
161   it leaves the first contact unaltered and creates a new contact
162   storing the VCARD information.
163
164 Both images and VCARDs should get pushed in without directory names.
165 Once the phone guesses the file type from the extension, it seems to
166 save the information in the appropriate place automatically.
167
168
169 obexfs
170 ======
171
172 Note: my phone doesn't support the Bluetooth's File Transfer Profile
173 (FTP), so I haven't been able to test the following commands.
174
175 With `sys-fs/obexfs` you can mount a phone with
176
177     $ sudo mount -t fuse obexautofs /tmp/mnt
178
179 When you're done, unmount with
180
181     $ sudo umount /tmp/mnt
182
183 `obexautofs` scans for available devices, so your device must be
184 visible (discoverable).  If that's a pain, you can mount a specific
185 device with
186
187     $ sudo mount -t fuse "obexfs#-b00:0A:0B:0C:0D:0E -B3" /tmp/mnt
188
189 Depending on your kernel, you may need to load the `fuse` module
190 before doing all of that:
191
192     $ sudo modprobe fuse
193
194
195 Debugging
196 =========
197
198 Use `hcidump` (`net-wireless/bluez-hcidump`) to trace the bluetooth
199 stack for debugging.
200
201
202 References
203 ==========
204
205 A good deal of this information came from the more detailed [Gentoo
206 Bluetooth Guide][guide] and [Bluetooth Network Aggregation Point
207 Howto][nap].
208
209
210 [BlueZ]: http://www.bluez.org/
211 [ObexPushD]: http://www.gitorious.org/obexpushd
212 [guide]: http://www.gentoo.org/doc/en/bluetooth-guide.xml
213 [nap]: http://en.gentoo-wiki.com/wiki/Bluetooth_Network_Aggregation_Point