简体   繁体   English

如何使用Java访问Raspberry Pi 3上的BLE?

[英]How to access BLE on Raspberry Pi 3 using Java?

The Raspberry Pi 3 includes BLE support. Raspberry Pi 3包括BLE支持。 I confirmed it works by 我确认它有效

sudo hcitool lescan sudo hcitool lescan

which returned the MAC and BLE 'complete local name' for neighboring advertisers. 它为邻近的广告商返回了MAC和BLE的“完整本地名称”。

How does one access this programmatically, in Java? 如何以Java编程方式访问此命令?

To use BLE on Raspberry Pi 3 you have to update bluez (the bluetooth core in Raspbian linux) then use the bluez D-Bus interface to interact with it. 要在Raspberry Pi 3上使用BLE,你必须更新bluez(Raspbian linux中的蓝牙核心)然后使用bluez D-Bus接口与它进行交互。

I'm looking for writing my own java lib, but it's very difficult because there are few documentation about D-Bus in java and about bluez. 我正在寻找自己的java lib,但这很难,因为关于Java中的D-Bus和bluez的文档很少。

For bluez there are only the sample code in the last distribution. 对于bluez,最后一个发行版中只有示例代码。

For now i have write a simple script that update the bluez version to the latest: https://gist.github.com/tongo/94338cebf4e6367d353439bca8d0a376 现在我写了一个简单的脚本,将bluez版本更新为最新版本: https//gist.github.com/tongo/94338cebf4e6367d353439bca8d0a376

I have also found a blog post that talk about d-bus,java and bluez: http://smartspacestuff.blogspot.it/2016/02/i-got-figurin-out-dbus-bluez.html 我还发现了一篇关于d-bus,java和bluez的博客文章: http ://smartspacestuff.blogspot.it/2016/02/i-got-figurin-out-dbus-bluez.html

It was useful, but not very clear for me. 这对我很有用,但不是很清楚。

I hope this can help. 我希望这可以提供帮助。

If you found other documentation post it. 如果您发现其他文档发布。

You can make this very simple by simply executing a command through Runtime and reading the output with a BufferedReader. 只需通过Runtime执行命令并使用BufferedReader读取输出,就可以使这变得非常简单。

Executing the command: 执行命令:

Process p;
p = Runtime.getRuntime().exec(command);
p.waitFor();

Full code: 完整代码:

 package your.package.rpicommand;

 import java.io.BufferedReader;
 import java.io.InputStreamReader;

 public class ExecuteShellCommand {

public static void main(String[] args) {

    ExecuteShellCommand obj = new ExecuteShellCommand();

    String domainName = "google.com";

    //in mac oxs
    String command = "ping -c 3 " + domainName;

    //in windows
    //String command = "ping -n 3 " + domainName;

    String output = obj.executeCommand(command);

    System.out.println(output);

}

private String executeCommand(String command) {

    StringBuffer output = new StringBuffer();

    Process p;
    try {
        p = Runtime.getRuntime().exec(command);
        p.waitFor();
        BufferedReader reader =
                        new BufferedReader(new InputStreamReader(p.getInputStream()));

                    String line = "";
        while ((line = reader.readLine())!= null) {
            output.append(line + "\n");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return output.toString();

}

}

I wish I could of coded this for you but there is already plenty of example on the internet about this. 我希望我可以为你编写这个,但在互联网上已经有很多关于这个的例子。

Although this is one way of doing this, you should use BlueCove or some library to scan through the Bluetooth devices. 虽然这是一种方法,但您应该使用BlueCove或某些库来扫描蓝牙设备。

Source: https://www.mkyong.com/java/how-to-execute-shell-command-from-java/ 资料来源: https//www.mkyong.com/java/how-to-execute-shell-command-from-java/

I don't think there is a clear or easy answer available at this time. 我认为目前没有明确或简单的答案。 Bluetooth integration requires native components that are not part of a standard JDK. 蓝牙集成需要不属于标准JDK的本机组件。

The most common library used for using Bluetooth with Java on Linux is BlueCove . 用于在Linux上使用蓝牙和Java的最常用库是BlueCove BlueCove provides extra native libraries for working with Bluetooth on linux: BlueCove-GPL or BlueCove-bluez (experimental). BlueCove提供额外的本机库,用于在Linux上使用蓝牙: BlueCove-GPLBlueCove-bluez (实验性)。 However, you will likely need to compile one of these yourself for use on your RPi. 但是,您可能需要自己编译其中一个以便在RPi上使用。 Methods for doing do will be dependent on your distribution and will require some significant knowledge of linux, compiling native code, etc. A quick google search shows some working examples of this for previous RPi versions. 这样做的方法将取决于你的发行版,并需要一些关于linux的重要知识,编译本机代码等。快速谷歌搜索显示了以前的RPi版本的一些工作示例。 It's unclear if it will work with the BLE on RPi 3 though. 目前还不清楚它是否适用于RPi 3上的BLE。

Another might be to try using " Camel Bluetooth Component ", which is wrapper over Bluecove and expects libbluetooth-dev and blueman to be installed. 另一种可能是尝试使用“ Camel Bluetooth Component ”,它是Bluecove的包装器,并期望安装libbluetooth-dev和blueman。 But again, not clear if it will work with RPi 3. 但同样,不清楚它是否适用于RPi 3。

If unable to get a true integration working, another option might be to simply make external Process calls out from Java to the command line Bluetooth utilities that you know already work. 如果无法获得真正的集成工作,另一个选择可能是简单地将外部Process调用从Java调用到您已知的工作的命令行Bluetooth实用程序。 It depends on your use-case if this an option, but I suspect could be sufficient for many BLE specific use cases. 如果这是一个选项,这取决于你的用例,但我怀疑对于许多BLE特定用例可能已经足够了。

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

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