mkogg.py: Fix 'self.get_mp4_metadata(self, source)'
[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 You can also initiate pairing from your computer with
96
97     $ simple-agent hci0 00:0A:0B:0C:0D:0E
98
99 Gentoo may need to be told that the cell phone is allowed to connect,
100 so create/edit `/var/lib/bluetooth/00:01:02:03:04:05/trusts`
101
102     $ cat /var/lib/bluetooth/00:01:02:03:04:05/trusts
103     00:0A:0B:0C:0D:0E [all]
104
105 which allows the phone to use any Bluetooth service provided by your
106 computer.  It's possible that you can accomplish the same result with
107 something like
108
109     $ sudo bluez-test-device trusted 00:0A:0B:0C:0D:0E yes
110
111 but I haven't been able to figure out the right syntax yet.
112
113
114 Pushing files from the phone
115 ============================
116
117 There are a number of OBEX push servers you can run on your box.  My
118 favorite so far is [ObexPushD][], which is easy to use, GPLv2
119 licensed, and Git versioned.  Can't beat that.  I've added an ebuild
120 (`app-mobilephone/obexpushd`) to my [[Gentoo overlay]] for easier
121 installation.
122
123
124 Pushing files to the phone
125 ==========================
126
127 Check for an OBEX Push service on the phone:
128
129     $ sdptool search --bdaddr 00:0A:0B:0C:0D:0E OPUSH
130     Searching for OPUSH on 00:0A:0B:0C:0D:0E ...
131     Service Name: Object Exchange
132     Service RecHandle: 0x10003
133     Service Class ID List:
134       "OBEX Object Push" (0x1105)
135     Protocol Descriptor List:
136       "L2CAP" (0x0100)
137       "RFCOMM" (0x0003)
138         Channel: 3
139       "OBEX" (0x0008)
140     Profile Descriptor List:
141       "OBEX Object Push" (0x1105)
142         Version: 0x0100
143
144 Push a file to the phone using `ussp-push` (`net-wireless/ussp-push`)
145
146     $ ussp-push 00:0A:0B:0C:0D:0E@3 LFILE RFILE
147
148 which pushes the local `LFILE` to the remote `RFILE`.
149
150 Note that phone's can be finicky about what they accept.  For example,
151 my Casio C721 is running BREW 3.x (see my [[Java ME]] post), and has a
152 number of issues:
153
154 * It attempts to detect file type by file extension (so you can't send
155   over a JPG with a `.jpeg` extension).  It pops up an "Unsupported
156   Media" warning if you try.
157 * It does not accept music in any of the following formats: (AAC
158   (`.aac`), MP3 (`.mp3`), MP4/AAC (`.mp4`, `.m4a`), WAV (`.wav`).
159 * It does accept JPGs with a `.jpg` extension.
160 * It does accept VCARDs (with a `.vcf` extension), but they must have
161   DOS endlines (e.g. via `unix2dos`).
162 * If you include multiple VCARDs in a single file, it enters all the
163   information under the name from the first VCARD.
164 * If you send a VCARD whos name matches an already existing contact,
165   it leaves the first contact unaltered and creates a new contact
166   storing the VCARD information.
167
168 Both images and VCARDs should get pushed in without directory names.
169 Once the phone guesses the file type from the extension, it seems to
170 save the information in the appropriate place automatically.
171
172
173 obexfs
174 ======
175
176 Note: my phone doesn't support the Bluetooth's File Transfer Profile
177 (FTP), so I haven't been able to test the following commands.
178
179 With `sys-fs/obexfs` you can mount a phone with
180
181     $ sudo mount -t fuse obexautofs /tmp/mnt
182
183 When you're done, unmount with
184
185     $ sudo umount /tmp/mnt
186
187 `obexautofs` scans for available devices, so your device must be
188 visible (discoverable).  If that's a pain, you can mount a specific
189 device with
190
191     $ sudo mount -t fuse "obexfs#-b00:0A:0B:0C:0D:0E -B3" /tmp/mnt
192
193 Depending on your kernel, you may need to load the `fuse` module
194 before doing all of that:
195
196     $ sudo modprobe fuse
197
198
199 Debugging
200 =========
201
202 Use `hcidump` (`net-wireless/bluez-hcidump`) to trace the bluetooth
203 stack for debugging.
204
205
206 References
207 ==========
208
209 A good deal of this information came from the more detailed [Gentoo
210 Bluetooth Guide][guide] and [Bluetooth Network Aggregation Point
211 Howto][nap].
212
213
214 [BlueZ]: http://www.bluez.org/
215 [ObexPushD]: http://www.gitorious.org/obexpushd
216 [guide]: http://www.gentoo.org/doc/en/bluetooth-guide.xml
217 [nap]: http://en.gentoo-wiki.com/wiki/Bluetooth_Network_Aggregation_Point