Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Battery Level of Bluetooth devices using Java

I am trying build a simple java program which shows battery status of connected Bluetooth devices in windows. see bellow sample image.

enter image description here

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 :

  1. There are plenty of examples available in android and python but not for Java / windows
  2. Also found unanswered similar C# question
    Get Bluetooth Device Battery Level
like image 557
aKilleR Avatar asked Dec 29 '20 12:12

aKilleR


People also ask

How do you check battery percentage on Bluetooth?

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.

How do you get the battery level after connect to the BLE device?

getIntValue(BluetoothGattCharacteristic. FORMAT_UINT8, 0) in broadcastUpdate is the battery value of the BLE device. It gives battery level as true.

How do I see battery on Android Bluetooth?

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.


1 Answers

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

like image 190
Bishan Avatar answered Sep 28 '22 16:09

Bishan