简体   繁体   English

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

[英]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 .我正在尝试构建一个简单的 java 程序,该程序显示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.首先,我从BlueCove开始,并意识到 BlueCove 仅提供地址、简单名称等基本信息。经过进一步调查,发现读取特征的唯一方法是通过 BLE GATT 服务。 therefore started writing a test code based on tinyb which was found in GitHub Bluetooth-manager project and it resulted a Exception因此开始编写基于在GitHub蓝牙管理器项目中找到的 tinyb 的测试代码,并导致异常

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 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?我的问题-> 还有其他方法可以使用 JAVA 获取蓝牙设备的电池电量吗?

Note:笔记:

  1. There are plenty of examples available in android and python but not for Java / windows android 和 python 中有很多示例,但不适用于 Java / Z0F4137ED1502B5045D6083A25
  2. Also found unanswered similar C# question还发现未回答的类似 C# 问题
    Get Bluetooth Device Battery Level 获取蓝牙设备电池电量

There are two types of adapters and each of them has pros and cons.有两种类型的适配器,每种都有优点和缺点。

  • Generic adapter (Supported by the TinyB Transport).通用适配器(由 TinyB Transport 支持)。 These are the most common adapters, most likely you have one already or it is embedded into your PC/Laptop.这些是最常见的适配器,很可能您已经拥有一个,或者它嵌入到您的 PC/笔记本电脑中。 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.但是,有一个主要缺点 - 它仅适用于基于Linux的环境,更准确地说,它需要在您的操作系统中安装Bluez软件。

  • BlueGiga serial adapter (Supported by the BlueGiga Transport). BlueGiga 串行适配器(受 BlueGiga Transport 支持)。 This a special type of bluetooth adapters that is quite rare.这是一种非常罕见的特殊类型的蓝牙适配器。 One of the most common is Bluegiga BLED112 .最常见的一种是Bluegiga BLED112 The main criteria in selecting a BlueGiga adapter is that it must support BGAPI .选择 BlueGiga 适配器的主要标准是它必须支持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+.要使用bluetooth-manager-tinyb ,您必须将 Bluez 软件升级到 5.43+。 This is due to some changes in the DBus API in Bluez 5.43v.这是由于 Bluez 5.43v 中的 DBus API 发生了一些变化。 (Bluez 5.44v, Bluez 5.45v, Bluez 5.46v, Bluez 5.47v preferred) (Bluez 5.44v、Bluez 5.45v、Bluez 5.46v、Bluez 5.47v 首选)

Final Note:最后注:

Windows OS is not supported by TinyB transport . TinyB TinyB transport不支持 Windows 操作系统。 For Windows OS you will need to get a BlueGiga adapter.对于 Windows 操作系统,您需要获得BlueGiga适配器。 You can use your Intel Wireless adapter in Linux environment only.您只能在 Linux 环境中使用您的英特尔无线适配器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM