The CASIDE Project: Investigating Investigating Cooperative Applications in Situated Display Environments

Last Update: 24 September 2007

Home News and Upcoming Conferences. Hermes
Door Displays
Climbing
Community
Domestic
Deployment
Java
Bluetooth
J2ME
Phone Support
Hermes
Photo Display
Hermes
Navigation
Hardware Publications
List
Private

Java Bluetooth HOWTO

Dan Fitton - df (at) comp.lancs.ac.uk - Lancaster University


This page outlines how to perform basic communication using Bluetooth, primarily between a mobile phone and a server using the Linux BlueZ stack with Java.

Contents

Java API's/Stacks
BlueZ OBEX
Basic Java OBEX
BlueZ Setup
Tested Bluetooth Hardware Devices
Device Discovery Compatibility
Bluetooth HID Profile

Java API's/Stacks

Presently there is no 'silver bullet' to provide Java Bluetooth comms, and current solutions fall into two categories:
1) Java API's with Bluetooth stacks built in.
Initially this seems the obvious choice, everything is written in Java sitting right on top of the Bluetooth hardware. Though this has two key disadvantages, a) no other applications can easily access Bluetooth comms  (imagine if your Java app had had the TCP/IP stack built in!), b)  there are relatively few implementations and they are not complete (no OBEX support etc)
2) Java API's which run on top of external stacks.
What I mean here is OS dependent Bluetooth stacks (i.e. BlueZ for Linux  and SP2 for Windows XP) with Java APIs making functionality available via JNI calls to the stack. These API's are inherently OS dependent, but potentially make all the features provided by the stack available.

The recent JSR-82 specification developed by Motorola in partnership with Sun provides a standardised set of Java packages (javax.bluetooth and javax.obex). It is an optional package for the MIDP (Mobile Information Device Profile) profile, which runs on top of the CLDC (Connection Limited Device Configuration) J2ME configuration. This seems to be supported by some of the recent MIDP 2.0 phones, and some J2SE stacks.

Name
License
OS
Java

javax.bluetooth

javax.obex

Comments

Java API's with Bluetooth stacks built in
Harald
Free
Any
Any
No
No
Runs under any version of Java supporting javax.comm. Only supports L2CAP
aveLink Commercial
Any
Any
Yes
Yes

JavaBluetooth
GNU
Any

Yes
No
Supports L2CAP
Java API's running on top of external Bluetooth stacks
avetanaBluetooth Commercial Windows XP, Linux, MacOS J2SE Yes Yes €25 for an unlimited license
Blue Cove
GNU
Windows XP
J2SE
Yes
No
Supports L2CAP and RFCOMM
JBlueZ
GNU
Linux
J2SE
No
No
Only supports basic discovery
Impronto DK
Commercial
Linux
J2SE
Yes
Yes
Free for educational use

The approach we current use to enable rapid prototyping of applications is the BlueZ stack, but using the Linux Bluetooth tools and small apps written C using the BlueZ libraries to enable functionality.

BlueZ - OBEX


There are two useful command line tools to enable OBEX push on the BlueZ stack, the first is OPD which can run as a daemon to continually listen and receive objects via Bluetooth and IrDA, and USSP-PUSH which you can use to push objects to remote devices. You can download both apps from here uncompress it using tar -zxvf [filename].

To build OPD change to the opd directory, type make and hit enter. To run it use something like:

opd --mode OBEX --channel 10 --path /tmp/opd --sdp --metadata

which will use Bluetooth, listen on channel 10, register the service with SPD, store received files in /tmp/opd and save extra data about the files you recive (i.e. the address of the Bluetooth device the files came from).

To build USSP-PUSH change to the client directory and run make, to push a file to a device, you first have to creating a binding to it using rfcomm, e.g.

1) Scan for devices in range:

#hcitool scan
Scanning ...
        xx:xx:xx:xx:xx:xx       Dan

2) Create an rfcomm binding to your device:

#rfcomm bind /dev/rfcomm0 xx:xx:xx:xx:xx:xx 9

Channel 9 seems to be the default channel or OBEX push on most mobile phones (e.g. Nokia), to see what services are provided by a device do 'sdptool browse xx:xx:xx:xx:xx:xx' and look for 'Service Name: OBEX File Transfer'. You will need to create your /dev/rfcommX nodes using something like:

#mknod /dev/rfcomm0 c 216 0 && mknod /dev/rfcomm1 c 216 1

3) Do the actual push (you specifiy the source file and remote file name):

#ussp-push /dev/rfcomm0 /home/jo/pics/test.jpg test.jpg

4) Release the rfcomm binding

#rfcomm release /dev/rfcomm0

Basic Java OBEX

The most basic way to use OBEX in Java in just by executing the command line BlueZ tools. This is obviously not a great approach but it does work. If you look at the code here you will see an example of how to this this, it executes a shell command then parses the output and error streams.

BlueZ Setup

The server side setup used in this HOWTO is (Gentoo) Linux, kernel 2.6.9/10, using the BlueZ stack. Compiling Bluetooth support into a kernel is pretty easy, in these examples we compile everything as modules, here is good starting point though YMMV:

Device Drivers ---> Networking Support --> Bluetooth subsystem support --->
 --- Bluetooth subsystem support
 <M>   L2CAP protocol support
 <M>   SCO links support
 <M>   RFCOMM protocol support
 [*]     RFCOMM TTY support
 <M>   BNEP protocol support
 [ ]     Multicast filter support
 [ ]     Protocol filter support
 <M>   HIDP protocol support
Bluetooth device drivers --->
<M> HCI USB driver
[ ]   SCO (voice) support
<M> HCI UART driver
[*]   UART (H4) protocol support
[*]   BCSP protocol support
[ ]     Transmit CRC with every BCSP packet
<M> HCI BCM203x USB driver
<M> HCI BlueFRITZ! USB driver
<M> HCI DTL1 (PC Card) driver
<M> HCI BT3C (PC Card) driver
<M> HCI BlueCard (PC Card) driver
<M> HCI UART (PC Card) device driver
<M> HCI VHCI (Virtual HCI device) driver


Under Gentoo building apps for Bluetooth support is as simple as:

#emerge bluez-utils bluez-sdp


Note: To emerge the bluez-sdp package you first need to comment out the 'hard' mask from /usr/portage/profile/package.mask, i.e. change the line 'net-wireless/bluez-sdp' to '#net-wireless/bluez-sdp'

Depeding on the distro you're using there are these apps are likely to be in your package management system somewhere, or they can be downloaded from the BlueZ web site.

If you have built Bluetooth support as modules you need to make sure they load on startup, so edit /etc/modules.autoload.d/kernel-2.6 and add the lines:

rfcomm

bcm203x
bfusb
hci_uart
hci_usb
hci_vhci

To add Bluetooth services to you startup scrips (in Gentoo), as root do :

#rc-update add bluetooth default

Your Bluetooth setup should be done, reboot (or loads the modules and start the script) then try:

#hciconfig

You should get something like:

hci0:   Type: USB
        BD Address: xx:xx:xx:xx:xx:xx ACL MTU: 192:8  SCO MTU: 64:8
        UP RUNNING PSCAN ISCAN AUTH ENCRYPT
        RX bytes:95 acl:0 sco:0 events:12 errors:0
        TX bytes:296 acl:0 sco:0 commands:12 errors:0

HCIConfig
works in a very similiar way to ifconfig for managing your BT interfaces, to discover nearby discoverable devices try:

#hcitool scan

If there are discoverable device nearby you will get something like:

Scanning ...
        xx:xx:xx:xx:xx:xx       iPAQ PocketPC
        xx:xx:xx:xx:xx:xx       Dan

(If something went wrong the Gentoo forums are a great place to start. If you are using a pcmcia Bluetooth device you might have to add an extra device entry in your card configuration database - /etc/pcmcia/config - to load the right module)

The PIN for pairing is stored in /etc/bluetooth/pin and is 1234 by default, security related stuff is configured from /etc/bluetooth/hcid.conf, the default HCId options should be ok. The following device configuration works fine for most applications:

lm accept;
lp rswitch,hold,sniff,park;
auth enable;
encrypt enable;

Depending on what you want to achieve, the 'local device class' (as the hcid.conf labells it) or Device-Specific Inquiry Access Code (DIAC) can be a crucial part of your Bluetooth config. For example, some older Bluetooth phones have difficult discovering an OBEX Push service unless it is included as part of your service class. The DIAC specifies the type of Bluetooth device, with a major and minor device class (e.g. 'Computer' and 'Laptop' respectively), then a service class (e.g. OBEX). The web site here should provide all the information you need, we now run through an example of creating a device class for a mobile phone with OBEX support:

Initially we create a 23 bit binary number for our DIAC, then convert it to hex for our configuration file. Our DIAC always starts with 0 0 for the first two bits (it is little endian). Major device is bits 2-7 (counting from 0), for a 'Cellular' device bit 2 needs to be set, so far we have the first octet:

0 0 0 0 0 1 0 0

Minor device is bits 8 - 15, and 'Phone' needs bit 9 set:

0 0 0 1 0     0 0 0 0 0 1 0 0

The major service class is bits 16-23, and for OBEX bit 20 needs to be set, giving us our 3 octets:
 
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
1
0
0

Now convert this binary value to hex (using you Windows/Gnome/etc claculator in scentific mode :))

000100000000001000000100 > 100204

This value becomes 0x100204 in our hcid.conf file. If you just want to get your Bluetooth stack up and running quickly some basic DIAC codes are:

0x104 - Workstation
0x108 - Server
0x10c - Laptop

To restart the BT stack after making changes to your hcid.conf file just do:

# /etc/init.d/bluetooth restart

You should now be able to discover your BT server from your mobile phone now and pair using the PIN from your configuration file.

Tested Bluetooth Hardware Devices

Anycom CF Bluetooth  CC3040 (LSE039/1) - Works fine
Cambridge Silicon Radio (10) built-in USB laptop device (Vaio Z1 series) - Works fine

Device Discovery Compatibility

Mfr
Model
Discovers Server
Discovered By Server
OBEX push (image)
OBEX push (vcf)
OBEX receive (image)
OBEX receive (vcf)

SE T630
SE T610
Nokia 7610
SE P800
HP iPAQ H38700

 

Bluetooth HID Profile

Several of the newest mobile phones coming out in 2005 are providing support for the Bluetooth Human Interface Device(HID) Profile. The HID Profile defines a mechanism through which Bluetooth-enabled input devices such as keyboards, mice, or even phones, etc.. can communicate with a computer (or other device). Configuration files on the device define its behaviour, by mapping particular keys or joystick movements to particular events or actions on the remote device, typically a computer. Setting up to use your device with your computer is pretty easy too.

Windows XP SP2 and Mac OS X already support the HID profile, so just go to Bluetooth settings in your phone and you should find the remote control functionality there. Please note , that after you have paired your phone with your PC you will need to enable the HID drivers for the device, by going to Bluetooth Settings -> Devices, and right click on the device to see its services. You should see the HID drivers there and you need to make sure that box is ticked.

For Linux users, there is some information on setting up HID and some instructions on how to integrate it into applications(for developers). If you are using Gentoo, follow the instructions above to install the Bluez stack and then edit your /etc/conf.d/bluetooth file and enable hidd, by changing HIDD_ENABLE=false to HIDD_ENABLE=true. Then restart bluetooth using /etc/init.d/bluetooth restart and you should now be able to use your phone as a remote!

If you are having trouble using your device, or need more information, you might want to try these:

A (probably not complete) list of phones supporting the HID profile includes:

  • SonyEricsson K700i
  • SonyEricsson K750i
  • SonyEricsson S700i
  • SonyEricsson V800/Z800
  • Nokia N70
  • Nokia N90
  • Nokia N91



The EPSRC funded CASIDE Project (EP/C005589):Investigating Investigating Cooperative Applications in Situated Display Environments - http://www.caside.lancs.ac.uk/

eXTReMe Tracker