Sept. 26, 2009, 7:34 p.m.
posted by void
Make Bluetooth Connections
Create a personal-area Bluetooth network to exchange files, send messages, and share network connections. Bluetooth allows you to create short-range (usually 10-meter), low-bandwidth (1-2 megabits per second), ad hoc connections between devices. It's ideal for sending photos from your camera phone to your computer, or for using the wireless modem found in certain models of cell phones. To get started with Bluetooth, first make sure it's working. Some computers have built-in Bluetooth adapters. If you are using an external USB Bluetooth dongle, after you insert it you may need to run this command to reset the Bluetooth Host Controller Daemon: $ sudo killall -HUP hcid
Now, to make sure Bluetooth is really up and running, you need to make sure you have at least one device in the vicinity that is set as "discoverable" (this is a Bluetooth mode that allows devices to find one another). Check your device documentation for details on how to set discoverable mode. Some devices allow you turn on discoverable mode only for a limited time (for example, two minutes). So, make sure you execute the next command quickly. The hcitool utility lets you manage Bluetooth connections. Run the command hcitool scan to enumerate the discoverable devices in your vicinity. Here, hcitool has found three devices (my Nokia 3650 cell phone, my Mac, and my Palm Treo): bjepson@scoobuntu:~$ hcitool scan
Scanning ...
00:60:57:50:AB:9C BrianJ3650
00:14:51:89:4D:C7 Jepstone
00:07:E0:6A:FE:54 bjepsonPairingNow that you know Bluetooth is working, you can pair your Ubuntu system with one of the devices. Pairing creates a bond between the devices so that they can exchange data and connect to one another. First, initiate pairing from the device you want to use. Each device is different. On the Treo, for example, you need to tap the Bluetooth icon and click Setup Devices Palm Treo discovering nearby Bluetooth devices
Manual PairingIf that dialog doesn't appear, you will need to take matters into your own hands. Cancel the pairing procedure on your device (or wait for it to time out), and go back to your Ubuntu system. Edit /etc/bluetooth/pin (you need to use sudo to edit this, as in sudo vi /etc/bluetooth/pin) and change the PIN code there to the PIN you want to use as the one-time password for pairing devices (some devices allow only numeric PINs). Next, create a script called /usr/local/bin/bluepincat and make it executable: #!/bin/sh # file: /usr/local/bin/bluepincat echo -n "PIN:" cat /etc/bluetooth/pin Comment out the existing pin_helper line in /etc/bluetooth/hcid.conf (again, you need to use sudo to edit this file) and add the following: pin_helper /usr/local/bin/bluepincat; Next, reset the hcid daemon with sudo killall -HUP hcid and try to initiate pairing from your device again, this time using the PIN code that you put into /etc/bluetooth/pin as the passkey. Once you're done pairing devices, you should restore the original configuration and reset the hcid daemon again (this avoids the possibility of someone guessing your PIN and pairing with your computer without your knowledge or consent). Bluetooth File TransferSuppose you have something on your cell phone, such as a photo you took, that you want to send to your computer. If you install the obexserver package, you can receive files sent over Bluetooth: $ sudo apt-get install obexserver
To receive files, you need to register the OBEX (Object Exchange) push service. This needs to be done each time you restart your computer (or the Bluetooth subsystem): $ sdptool add --channel=10 OPUSH OBEX Object Push service registered Now, when you want to receive a file, start the obexserver and send the file from the other device. The obexserver will terminate after the transfer: $ obexserver Waiting for connection... ............................................................... ............................................................... ............................................................... ............................................................... ...................................................HEADER_LENGTH = 307254 put_done() Skipped header 05 put_done() Skipped header 42 put_done() Skipped header c0 Filename = screenshot0000.bmp Wrote /tmp/screenshot0000.bmp (307254 bytes) $ Connect to the InternetMany Bluetooth phones, including certain Treo models as well as many Nokia and Sony Ericsson phones, can act as a gateway between your computer and the Internet. In this configuration, the phone is commonly thought of as a "wireless modem," but in fact, the connection between your computer and the Internet is digital all the way. Still, because this configuration uses the same protocol (PPP) as most dial-up connections, it's easy and sometimes helpful to just think of it as a modem. To make the connection, you first need to set up a link between your Ubuntu system and the paired device. Edit (using sudo) /etc/bluetooth/rfcomm.conf and add an entry for your device. You'll need to specify your phone's device ID (which you can get with hciutil scan, as shown earlier): rfcomm0 {
bind yes;
# Bluetooth address of the device
device 00:07:E0:6A:FE:54;
# RFCOMM channel for the connection
channel 1;
# Description of the connection
comment "My Treo";
}Next, edit /etc/bluetooth/hcid.conf (using sudo), scroll to the bottom, and uncomment the auth and encrypt lines so they look like this: # Authentication and Encryption (Security Mode 3)
auth enable;
encrypt enable;Then, run the command sudo killall -HUP hcid to reset the hcid daemon. Next, use the command sudo rfcomm bind rfcomm0 to make the connection. Now you've got a modem you can use on /dev/rfcomm0. Next, you need to create some PPP scripts. The first one is a chat script. As root, create /etc/chatscripts/BluetoothPhone. You will need to replace APN with the Access Point Name that your cellular provider uses (contact your cellular provider to get this information, or check Opera's list of APNs at http://www.opera.com/products/mobile/docs/connect/): TIMEOUT 10 ABORT 'BUSY' ABORT 'NO ANSWER' ABORT 'ERROR' SAY 'Starting GPRS connect script\\n' # Get the modem's attention and reset it. "" 'ATZ' # E0=No echo, V1=English result codes OK 'ATE0V1' # Set Access Point Name (APN) SAY 'Setting APN\\n' OK 'AT+CGDCONT=1,"IP","APN"' # Dial the number ABORT 'NO CARRIER' SAY 'Dialing...\\n' OK 'ATD*99***1#' CONNECT '' The next is /etc/chatscripts/BluetoothPhone-Disconnect: "" "\\K" "" "+++ATH0" SAY "GPRS disconnected." Finally, you need a PPP peers script (/etc/ppp/peers/BluetoothPhone): /dev/rfcomm0 # Bluetooth modem 115200 # speed defaultroute # use the cellular network for the default route usepeerdns # use the DNS servers from the remote network nodetach # keep pppd in the foreground crtscts # hardware flow control lock # lock the serial port noauth # don't expect the modem to authenticate itself local # don't use Carrier Detect or Data Terminal Ready replacedefaultroute debug # Use the next two lines if you receive the dreaded messages: # # No response to n echo-requests # Serial link appears to be disconnected. # Connection terminated. # lcp-echo-failure 4 lcp-echo-interval 65535 connect "/usr/sbin/chat -V -f /etc/chatscripts/BluetoothPhone" disconnect "/usr/sbin/chat -V -f /etc/chatscripts/BluetoothPhone-Disconnect" With all these scripts in place and your rfcomm device connected using rfcomm bind, run the command sudo pppd call BluetoothPhone to connect to the Internet anywhere you have cellular service. When you are done, press Ctrl-C to terminate the connection. For more information on working with cellular data connections and Bluetooth, see Linux Unwired by Roger Weeks et al. (O'Reilly). Brian Jepson |
- Comment
Add Device.
