Skip to content

Instantly share code, notes, and snippets.

@SingingBush
Last active April 1, 2024 20:16
Show Gist options
  • Save SingingBush/50c3fba41ec0642a73b842e8d5802e9d to your computer and use it in GitHub Desktop.
Save SingingBush/50c3fba41ec0642a73b842e8d5802e9d to your computer and use it in GitHub Desktop.
A short article about the lack a standard API for Bluetooth in Java SE

Bluetooth (or the lack of) on Java

Java SE has never had support for bluetooth, the closest thing to a standard is JABWT (Java APIs for Bluetooth Wireless Technology) defined in JSR-82 which is actually for Java ME (Micro Edition).

JSR-82 provides the specification for the javax.bluetooth and javax.obex packages and would allow for code somewhat like:

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import java.io.IOException;

...

try {
    final LocalDevice localDevice = LocalDevice.getLocalDevice();
    final DiscoveryAgent agent = localDevice.getDiscoveryAgent();

    agent.startInquiry(DiscoveryAgent.GIAC, new DiscoveryListener() {
        @Override
        public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {                    
            try {
                System.out.println(String.format(
                        "Found device %s %s %s",
                        btDevice.getFriendlyName(false),
                        btDevice.getBluetoothAddress(),
                        btDevice.isTrustedDevice() ? "TRUSTED" : "NOT TRUSTED"
                ));
            } catch (final IOException e) {
                System.out.println(String.format(
                        "Found device %s %s",
                        btDevice.getBluetoothAddress(),
                        btDevice.isTrustedDevice() ? "TRUSTED" : "NOT TRUSTED"
                ));
            }
        }

        @Override
        public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
            // ignore
        }

        @Override
        public void serviceSearchCompleted(int transID, int respCode) {
            // ignore
        }

        @Override
        public void inquiryCompleted(int discType) {

        }
    });
} catch (final BluetoothStateException e) {
    e.printStackTrace();
}

An installation of Java ME SDK will contain jsr082_1.1.jar within the lib folder but the regular JDK does not include this.

There have been some attempts to provide a working implementation of JSR-82 for Java SE:

  • Avetana (see also their sourceforge page which has been inactive for over a decade)
  • BlueCove went from Sourceforge to Google Code (with final commit 10th June 2011) and has since been exported to numerous Github accounts.

For a long time BlueCove was the only real option for Java SE devs to get bluetooth working with Java. Although there are a fair few forks of the project nothing has been maintained enough to make it a viable solution. The most recent commits I could find were forked off from hcarver/bluecove, the latest I found being in 2017 on vkorecky/bluecove. However when I cloned that version running mvn package with JDK 8 failed to build.

Fortunately there are alternative projects that aim to bring Bluetooth to Java:

These newer project do not use the API defined in JSR-82 but that's a good thing. JSR-82 was approved in 2000 (when Java was at version 1.3) with much input from Motorola. Although last looked at in 2010 the API has not changed much. In an ideal world a completely new standard API would be born out of the work done by Intel for tinyb and also Google with their android.bluetooth package.

It's about time the JDK had a java.bluetooth package as standard.

@SOLR4189
Copy link

SOLR4189 commented Jan 6, 2021

Wow, it's the amazing summary! I'm trying to find the simplest solution more than two days already. Thank you!

@SingingBush
Copy link
Author

Cheers, I put these notes together as I was planning on writing an article about the whole experience. I spent considerable time trying to get something useful working. I wanted a cros-platform solution for a desktop application.

I really feel that there should be a new JSR for a standard API that gets implemented by all JVM vendors. Or at least a joint venture by companies that want Bluetooth on Java to work together on a library that can be published on maven central.

@SOLR4189
Copy link

SOLR4189 commented Jan 6, 2021

Actually I try to create Java Bluetooth Server on Raspberry Pi 0w. As I understand there is no option for it in tinyb (intel-iot-devkit/tinyb#16) or bluez-dbus. Maybe you know some option for it with MIT/Apache 2.0 license preferably?

@SingingBush
Copy link
Author

Sorry no. I think you will need to find a non-Java option

@hakanai
Copy link

hakanai commented Jan 22, 2024

Now that Android has defined Bluetooth support in androidx.bluetooth, you would think that implies a desktop implementation of it could exist.

I too have been trying to use Java with Bluetooth many times to date, and have looked at most of the options out there. It just seems like one of those protocols which gets bad support, which is weird, because I keep hearing there are so many devices using it.

@FirokOtaku
Copy link

It's 2024, even HTML5 has Web Bluetooth API support coming, seems still we can hardly find an actively maintainced Bluetooth framework for Java. 🤣🤣

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment