Battery Level of Bluetooth devices using Java
Asked Answered
S

1

7

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
Syriac answered 29/12, 2020 at 12:31 Comment(0)
C
3

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.

Cranky answered 4/1, 2021 at 7:27 Comment(2)
is there any other way to get data from Generic adapter for windows ? maybe from some other langue ? because i have found this third party app which works in windows for Generic adapterSyriac
@aKilleR Try these. Bluetooth Battery Monitor, How to Read the Battery Level of Bluetooth LE DevicesCranky

© 2022 - 2024 — McMap. All rights reserved.