使用 Java 的蓝牙设备的电池电量

aKi*_*leR 6 java windows bluetooth bluetooth-lowenergy bluetooth-gatt

我正在尝试构建一个简单的 java 程序,它显示Windows中连接的蓝牙设备的电池状态。请参见下面的示例图像。

在此处输入图片说明

首先我从BlueCove开始,发现 BlueCove 只提供地址、简单名称等基本信息。经过进一步调查,发现读取特征的唯一方法是通过 BLE GATT 服务。因此开始基于在 GitHub Bluetooth-manager项目中找到的tinyb编写测试代码,结果出现异常

到目前为止,我已经尝试过这段代码

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();

    }
}
Run Code Online (Sandbox Code Playgroud)

马文

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();

    }
}
Run Code Online (Sandbox Code Playgroud)

例外

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
Run Code Online (Sandbox Code Playgroud)

我的问题 -> 有没有其他方法可以使用 JAVA 获取蓝牙设备的电池电量?

笔记 :

  1. android 和 python 中有很多可用的示例,但不适用于 Java / windows
  2. 还发现未回答的类似 C# 问题
    Get Bluetooth Device Battery Level

Bis*_*han 3

有两种类型的适配器,每种都有优点和缺点。

  • 通用适配器(由 TinyB 传输支持)。这些是最常见的适配器,您很可能已经拥有一个或者它已嵌入到您的 PC/笔记本电脑中。然而,它有一个主要缺点 - 它只能在基于Linux 的环境中工作,更准确地说,它需要在您的操作系统中安装Bluez软件。

  • BlueGiga 串行适配器(由 BlueGiga Transport 支持)。这是一种非常罕见的特殊类型的蓝牙适配器。最常见的一种是Bluegiga BLED112。选择 BlueGiga 适配器的主要标准是它必须支持BGAPI

下表显示了每种适配器类型支持的一些主要功能。 每种适配器类型支持的主要功能

笔记:

要使用bluetooth-manager-tinyb,您必须将 Bluez 软件升级到 5.43+。这是由于 Bluez 5.43v 中的 DBus API 发生了一些变化。(首选 Bluez 5.44v、Bluez 5.45v、Bluez 5.46v、Bluez 5.47v)

最后注意事项:

不支持 Windows 操作系统TinyB transport。对于 Windows 操作系统,您需要一个BlueGiga适配器。您只能在 Linux 环境中使用英特尔无线适配器。