February 26, 2007

CLI Magic: Access your Bluetooth phone via the command line

Author: Duane Odom

Recently, I upgraded my cell phone to a Motorola RAZR v3 from T-Mobile, a Bluetooth-enabled device. I wanted to copy files to this device using my Laptop running Debian testing (Etch) using command line tools. I found what I needed in a package of Bluetooth tools and daemons called BlueZ.

After I installed bluez-utils with apt-get, I found a utility installed with the package named sdptool that speaks the Bluetooth Service Discovery Protocol. SDP provides for the discovery of services available on Bluetooth devices. Some common services available on cell phones are Dial-Up Networking for PPP connections, LAN for network connections, and OPUSH (Object PUSH) for sending files to a phone. The OPUSH service seemed to best fit my needs.

My phone does not broadcast its available services unless prompted to (for security reasons), so in the menu options of the phone, I enabled broadcast mode and ran sdptool search OPUSH to see if my phone provided Object PUSH and on which channel it was provided. My results were as follows:

Inquiring ...
Searching for OPUSH on 00:11:22:33:44:55 ...
Service Name: OBEX Object Push
Service Description: OBEX Object Push
Service Provider: /a/mobile/system/cl.gif
Service RecHandle: 0x10008
Service Class ID List:
 "OBEX Object Push" (0x1105)
Protocol Descriptor List:
 "L2CAP" (0x0100)
 "RFCOMM" (0x0003)
   Channel: 8
 "OBEX" (0x0008)
Language Base Attr List:
 code_ISO639: 0x656e
 encoding:    0x6a
 base_offset: 0x100
 code_ISO639: 0x6672
 encoding:    0x6a
 base_offset: 0xc800
 code_ISO639: 0x6573
 encoding:    0x6a
 base_offset: 0xc803
 code_ISO639: 0x7074
 encoding:    0x6a
 base_offset: 0xc806
Profile Descriptor List:
 "OBEX Object Push" (0x1105)
   Version: 0x0100

The line "Searching for OPUSH on 00:11:22:33:44:55 ..." told me that the command found my phone and that its network address was "00:11:22:33:44:55". The next line of interest was "Channel: 8" which told me that the OPUSH service was available on my phone on channel 8 via the RFCOMM protocol.

Now I was ready to copy a test file over to the phone. I used an application called obex_test from the bluez-utils package to push the test file to the phone. I provided the obex_test application with the address of my phone and the channel on which the OPUSH service was available with the command obex_test -b 00:11:22:33:44:55 8. After running the command, I was provided with a prompt. I typed the command c to connect to the phone, then issued the command p to give a put request
and typed the path to the file to be uploaded -- /pics/tux.jpg -- and the name of the file as I wanted it to appear on the phone -- tux.jpg. The phone then prompted me to accept the incoming transfer. When it finished, I issued the command q to quit the session. The output of the session is shown below.

duane@thinkpod:~> obex_test -b 00:11:22:33:44:55 8
Using Bluetooth RFCOMM transport
OBEX Interactive test client/server.
> c
Connect OK!
Version: 0x10. Flags: 0x00
> p
PUT file (local, remote)> /pics/tux.jpg tux.jpg
name=/pics/tux.jpg, size=15811
Going to send 15811 bytes
Made some progress...
Made some progress...
Made some progress...
PUT successful!
> q

Using the obex_test application to start a new interactive session every time I wanted to copy a new file to my phone would be too tedious. After a quick search, I found a script called obexsend.sh that serves as wrapper for the obex_test application, thus simplifying the process of moving files to the phone. The obexsend.sh script takes the network address of the phone, the channel of the OPUSH service, and the path of the file to be pushed as its parameters. The command obexsend.sh 00:11:22:33:44:55 8 /pics/tux.jpg accomplishes the same task as the interactive obex_test session above but makes executing and scripting this task much easier.

Users who are interested in transferring files from their phone to their computer using the command line may be out of luck. I had no problem telling my phone to send a file to my computer, but I was unsuccessful in finding a way to accept the file except by using a GUI -- specifically the gnome-bluetooth package. Once that package is installed, you can click on Applications -> Accessories -> Bluetooth File Sharing in the GNOME menu. You should see a Bluetooth icon appear in your system tray. When you tell your phone to transfer to your computer, a window should appear on your desktop asking you to accept the file. Tip: The accept window did not appear on top of the other windows on my desktop and it wasn't listed in the window list. I suggest minimizing all windows on your desktop before you perform the transfer so that the transfer accept window is easily visible.

If you find BlueZ useful, another interesting tool you might want to consider is btscanner, which scans for Bluetooth devices in range and extracts as much information as possible from a device without the requirement to pair with it. Also check out hcitool, which is included in the bluez-utils package. It provides information about the connection to a phone, such as link quality and signal strength, and allows you to configure things like connection
encryption and authorization. As always, you should read the man page for each command.

Click Here!