I am trying build a simple java program which shows battery status of connected Bluetooth devices in windows. see bellow sample image.
First I started with BlueCove and realized BlueCove only provides basic information such as address, simple name etc.. after further investigation and found out only way to read the characteristic is via BLE GATT service. therefore started writing a test code based on tinyb which was found in GitHub Bluetooth-manager project and it resulted a Exception
so far I have tried this code
import org.sputnikdev.bluetooth.URL;
import org.sputnikdev.bluetooth.manager.CharacteristicGovernor;
import org.sputnikdev.bluetooth.manager.impl.BluetoothManagerBuilder;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
public class B2 {
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
new BluetoothManagerBuilder()
.withTinyBTransport(true)
.withBlueGigaTransport("^*.$")
.build()
.getCharacteristicGovernor(new URL("/XX:XX:XX:XX:XX:XX/F7:EC:62:B9:CF:1F/"
+ "0000180f-0000-1000-8000-00805f9b34fb/00002a19-0000-1000-8000-00805f9b34fb"), true)
.whenReady(CharacteristicGovernor::read)
.thenAccept(data -> {
System.out.println("Battery level: " + data[0]);
}).get();
}
}
Maven
<dependencies>
<dependency>
<groupId>org.sputnikdev</groupId>
<artifactId>bluetooth-manager</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>org.sputnikdev</groupId>
<artifactId>bluetooth-manager-tinyb</artifactId>
<version>1.3.3</version>
</dependency>
</dependencies>
Exception
Exception in thread "main" java.lang.IllegalStateException: java.lang.IllegalStateException: Native libraries for TinyB transport could not be loaded.
at org.sputnikdev.bluetooth.manager.impl.BluetoothManagerBuilder.loadTinyBTransport(BluetoothManagerBuilder.java:225)
at org.sputnikdev.bluetooth.manager.impl.BluetoothManagerBuilder.build(BluetoothManagerBuilder.java:190)
at B2.main(B2.java:15)
Caused by: java.lang.IllegalStateException: Native libraries for TinyB transport could not be loaded.
at org.sputnikdev.bluetooth.manager.impl.BluetoothManagerBuilder.loadTinyBTransport(BluetoothManagerBuilder.java:218)
... 2 more
My question - > is there any other way to obtain Bluetooth device battery level using JAVA ?
Note :
Click on Devices. Click on Bluetooth & other devices. Under the “Mouse, keyboard, & pen” section, check the battery level indicator on the right side for the Bluetooth device.
getIntValue(BluetoothGattCharacteristic. FORMAT_UINT8, 0) in broadcastUpdate is the battery value of the BLE device. It gives battery level as true.
Step 1: Pair and connect a Bluetooth device to your Android phone. Step 2: Open up the Settings app and go to “Connect Devices.” Step 3: Tap on the word “Bluetooth” (not the switch next to it) and you will see complete list of all connected devices and their battery level.
There are two types of adapters and each of them has pros and cons.
Generic adapter (Supported by the TinyB Transport). These are the most common adapters, most likely you have one already or it is embedded into your PC/Laptop. However, there is a major drawback - it works only in Linux based environments, to be more precise, it requires Bluez software installed in your OS.
BlueGiga serial adapter (Supported by the BlueGiga Transport). This a special type of bluetooth adapters that is quite rare. One of the most common is Bluegiga BLED112. The main criteria in selecting a BlueGiga adapter is that it must support BGAPI.
The following table shows some major features supported by each adapter type.
Note:
To use bluetooth-manager-tinyb, You must upgrade your Bluez software to 5.43+. This is due to some changes in the DBus API in Bluez 5.43v. (Bluez 5.44v, Bluez 5.45v, Bluez 5.46v, Bluez 5.47v preferred)
Final Note:
Windows OS is not supported by TinyB transport
. For Windows OS you will need to get a BlueGiga
adapter. You can use your Intel Wireless adapter in Linux environment only.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With