Skip to content

Instantly share code, notes, and snippets.

@SingingBush
Last active October 22, 2024 09:13
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.

@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. 🤣🤣

@EliasZ
Copy link

EliasZ commented Oct 18, 2024

@SingingBush I'm wondering, did you ever get something to work with a modern JDK and on regular hardware (ex. Apple Silicon)?

@SingingBush
Copy link
Author

No, but now that Java has the FFM (JEP 442: Foreign Function and Memory) API (since JDK 21), if some person or organisation were to work on an open-source bluetooth library for Java, then this would probably be the best way to integrate with existing native libraries on each operating system.

As stated before I would love for there to be a java.bluetooth module in the JDK but I don't think there's enough interested parties willing to put the time in. I did play around with the FFM API a while back but nowhere near enough to be able to create a cross-platform project that's production ready.

@EliasZ
Copy link

EliasZ commented Oct 22, 2024

@SingingBush Yeah I think it's very low on the list of priorities if even on the list. I chose to settle for noble as this works natively without much effort. It's not Java, but at least it makes it a lot easier to programmatically tinker with Bluetooth.

Looking at the source code it seems to be calling platform specific OS stuff for interacting with Bluetooth so perhaps something similar can be done in Java if anyone ever wants to try.

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