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:
- tinyb (Tiny Bluetooth LE Library) aims to provide a modern and easy to use Bluetooth LE API. TinyB exposes the BLE GATT API for C++, Java and other languages, using BlueZ over DBus. See example code here.
- bluez-dbus (Linux only)
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.
@SingingBush I'm wondering, did you ever get something to work with a modern JDK and on regular hardware (ex. Apple Silicon)?