Bluetooth

Method 1: Command Line Utilities

Prerequisites

  1. OS2008 Linux distribution
  2. apt-get install bluez-utils-test
  3. apt-get install screen
  4. on N810: set Control Panel > BlueTooth > set BlueTooth on. Consider turning Visible on.

Finding Bluetooth addresses: your own and others

hcitool is used to configure Bluetooth connections and send some special command to Bluetooth devices. hcitool is standard on any Linux distribution with bluez-utils installed.

Commands

dev Display local BT devices, and their addresses.
inq Inquire remote devices. For each discovered device, Bluetooth device address, clock offset and class are printed.
scan Inquire remote devices. For each discovered device, device name are printed. It normally takes 10 seconds. Sometimes more, sometimes less!

SDP: Service Discovery Protocol

sdptool is the standard Bluez utility

sdptool browse returns all discoverable devices and all their advertised services

sdptool browse <btaddr> returns only the advertised services of a particular device

sdptool search <service> returns only devices advertising a particular service

sdptool can also be used to add/remove services from your own list of provided services.

sdptool is very verbose: sdp-query is a N810-specific alternative.

sdp-query <btaddr> is similar to sdptool browse <btaddr>, but its output is more concise: 1 line per service.

RFCOMM: serial-port style connection between 2 devices

RFCOMM is the abbreviation for Radio Frequency Communication. The Bluetooth protocol RFCOMM is a simple set of transport protocols, made on top of the L2CAP protocol, providing emulated RS-232 serial ports (up to sixty simultaneous connections of a BlueTooth device at a time).

Must be root.

On one device:

rfcomm listen <dev-name> [channel]

<dev-name> is the device to use. Traditionally rfcomm0, rfcomm1, etc. The device will appear as /dev/rfcomm0 or whatever.

[channel] is optional, default 1. Values of 1 to 60 are valid.

On the other device:

rfcomm connect <dev-name> <bt-addr> [channel]

<dev-name> is the device to use, does not need to match the listener. <bt-addr> is the address of the listener. *Important* [channel] must match the listener. If it does not, the connector will think the channel is open, but the listener will not. So far I don't know how to figure out which channel to use… sdp-query does not provide this information. Yikes!

If you are running these commands in a shell, you'll see a connection message when the second device successfully connects. Both sides can then use their /dev/rfcomm* devices as serial ports (eg run screen /dev/rfcomm0 to send text across manually). Break the connection by pressing Ctrl-C in the shell that started the connection or by killing the rfcomm process (either side).

rfcomm will tell you about any currently open connections.

This method has been tested between two N810s, and between an N810 and a ThinkPad T60p running Ubuntu 6.06.

RFCOMM variations

Permanent link between two devices

Not tested.

This would be appropriate if you were attaching a peripheral permanently to the device via RFCOMM (eg a keyboard, GPS, etc).

Do this by editing /etc/bluetooth/rfcomm.conf and using the rfcomm bind and rfcomm unbind functionality.

PAN/BNEP: networking over Bluetooth

BNEP is a networking-over-Bluetooth protocol that allows IP packets to be transported over bluetooth. This makes all sorts of things easy because you instantly can use FTP, SCP, SSH, etc…

Tested between 2 N810s. Tried it with a laptop and an N810, no luck so far.

The “listener/connector” and “server/client” roles are orthogonal. The procedure shown here is for “listener-server, connector-client”.

On the server: pand -s -r GN

On the client: pand -c <btaddr> to connect to a specific server, pand -Q10 -d GN to search for 10*1.28 seconds for any GN-role listeners.

Once the connection is made, bnep0 is the interface that can be configured as usual with ifconfig, or make your setup permanent by modifying /etc/network/interfaces.

On either side: pand -l tells you about any active connections. pand -k <btaddr> kills the connection with a specific address. pand -K kills all PAN connections.

PAN variations

Listener-client, connector-server

On the client: pand -s -r PANU

On the server: pand -c <btaddr> -r GN

bnep0 appears and can be configured as usual.

The other server role: NAP

GN is the “Group Ad-hoc Network Controller”. Its cousin is NAP, “Network Access Point”. Supposedly you can just replace GN by NAP in any of the instructions above and it should work.

We're not entirely sure what the difference between the two roles is. According to the documentation, GN is for when you want to connect more than 2 devices together so that they can all communicate with each other, whereas NAP is for when the server will act as a gateway to the Internet (or larger network, whatever).

NAP functionality has not been tested.

More than 2 devices in a GN ad-hoc network

If you want to connect 3 or more devices so that they all talk to each other, have each device individually connect to the GN device. Supposedly up to 7 devices can connect together. This functionality hasn't been tested.

Note that the GN node will have interfaces bnep0bnep7 popping up. This could be inconvenient. There's documentation on combining all the interfaces into a single “bridged” interface for easy configuration, but we haven't tried it.

More info: http://bluez.sourceforge.net/contrib/HOWTO-PAN

OBEX: file transfer

OBEX is a file transport protocol for various wireless transport layers (IRDA, bluetooth). OBEX is a quick and easy way to get files to & from your machine, particularly your Windows machine or your photo printer. There is an OBEX server already running on the N810, but as of yet we don't know how to access the OBEX functionality for other purposes (eg sending files through the command line).

Method 2: Python via PyBluez

advantages:

  • don't need to be root
  • more elegant than command line tools

Assuming you have set up a Python 2.5 environment as in pubsub:

apt-get install python2.5-bluez

Now you have access to the bluetooth module. (Note: I had trouble with the repository documented in pubsub. Instead, I manually added http://repository.nrcc.noklab.com, using repo: bora and type: extras).

API: http://pybluez.googlecode.com/svn/www/docs-0.7/index.html

Homepage: http://pybluez.googlecode.com

Albert Huang wrote a book called “Bluetooth Essentials for Programmers”. All the Python examples use PyBluez.

Free, earlier version of book (not as comprehensive): http://people.csail.mit.edu/albert/bluez-intro/

Examples from the book: http://www.btessentials.com/examples/examples.html

Method 3: Python via D-Bus

Untested so far. There are multiple D-Bus interfaces to the Bluetooth module. org.bluez/org/bluez/hci0 is the generic one that is probably present on all Debian systems. Maemo 2.x seems to provide some Nokia-specific ones at com.nokia.bt* (btcond, btsdp, btsearch, etc).

Start by getting yourself set up with Python access to D-Bus:

apt-get install python2.5-dbus

Then read about D-Bus and how it works with Python:

http://dbus.freedesktop.org/doc/dbus-tutorial.html

http://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html

Then read some examples of accessing Bluez on Maemo via D-Bus:

http://maemo.org/community/wiki/BluetoothGPS/

http://maemo.org/development/documentation/apis/2-x/osso-gwconnect.html

Bonus D-Bus stuff

D-Bus is a good way to do IPC. There are good examples here: http://cgit.freedesktop.org/dbus/dbus-python/tree/examples

We've had success with using python's pickle capabilities to send Python objects through D-Bus as serialized strings. Be aware: what you get out of a D-Bus call is a String not a str, and String does not react well to pickling.

Well-written D-Bus objects support introspection. This is great for figuring out how to work with a strange or underdocumented resource. Set up the D-Bus proxy object as usual, but then connect to the interface at org.freedesktop.DBus.Introspectable and run the Introspect() routine. You will get a big XML string with all the methods, signals, etc. (Note that if you write a D-Bus service as in the Python example, it will automatically support introspection. Hooray!)

Example:

import dbus

bus = dbus.SystemBus()

bt_obj = bus.get_object("org.bluez", "/org/bluez/hci0")
bt_intro_iface = dbus.Interface(bt_obj, "org.freedesktop.DBus.Introspectable")
ispect = bt_intro_iface.Introspect()

print ispect

Links of interest

 
shortproject/bluetooth.txt · Last modified: 2008/05/05 00:13 by tinka
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki