且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 Android 应用程序通过蓝牙连接到 Raspberry pi

更新时间:2023-01-04 22:58:13

快速解答

与您开始时的方式类似,蓝牙连接需要设备地址服务 UUID.

设备地址(例如:00:72:02:97:33:2C)可以从配对的设备(或通过允许发现)获取,有关更多信息,请参阅 Android 示例应用.

The device address (ex: 00:72:02:97:33:2C) can be obtained from paired devices (or by allowing for discovery), see Android example app for more on that.

UUID(例如:00001101-0000-1000-8000-00805F9B34FB)通常定义在使用特定标识符运行蓝牙服务的服务器部分.

The UUID (ex: 00001101-0000-1000-8000-00805F9B34FB) is typically defined on the server part where a Bluetooth service is running with a specific identifier.

现在,如果您想避免最后一部分而只想传达简单的数据,您可以使用蓝牙串行端口的默认设置.来自 Android 蓝牙文档:

Now if you want to avoid that last part and just want to communicate simple data you can rely on a default using the bluetooth serial port. From Android Bluetooth documentation:

提示:如果您连接的是蓝牙串口板,请尝试使用著名的 SPP UUID 00001101-0000-1000-8000-00805F9B34FB.然而如果您要连接到 Android 对等点,请生成自己的唯一的 UUID.

Hint: If you are connecting to a Bluetooth serial board then try using the well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB. However if you are connecting to an Android peer then please generate your own unique UUID.

更多信息

Android 文档 &示例代码

android 示例 BluetoothChat 是关于如何处理 2 个 android 设备之间的 2 路通信的一个很好的例子.

More information

Android Documentation & Sample code

The android sample BluetoothChat is a nice example on how to deal with 2 way communication between 2 android devices.

Android 蓝牙文档 https://developer.android.com/guide/topics/connectivity/bluetooth.html

蓝牙聊天示例 https://developer.android.com/samples/index.html.请注意,您只需选择 File > New > Sample Project 并在 Android Studio 中搜索 Bluetooth Chat,即可查看此项目.

The Bluetooth Chat sample https://developer.android.com/samples/index.html. Note that you can check this project out by just selecting File > New > Sample Project and searching for Bluetooth Chat in Android Studio.

在树莓派上启动这样的服务可以使用这个 Python 示例找到:http://blog.davidvassallo.me/2014/05/11/android-linux-raspberry-pi-bluetooth-communication/.只是在线提供的众多示例之一.此示例包含 Android 代码.

Dealing with starting such a service on a raspberry pi can be found using this Python example: http://blog.davidvassallo.me/2014/05/11/android-linux-raspberry-pi-bluetooth-communication/. Just one of the many samples available online. This example includes Android code.

如果你想使用 C++ 代码在树莓派上实现这样的事情,我可以推荐来自 http://people.csail.mit.edu/albert/bluez-intro/x502.html.

If you want to implement such a thing on the raspberry Pi using C++ code I can recommended the documentation from http://people.csail.mit.edu/albert/bluez-intro/x502.html.

该链接具有服务器和客户端的示例代码.由于这是使用 RFCOMM 规范和默认串行端口 UUID,因此无需指定任何 UUID.

That link has example code for both server and client. Since this is using RFCOMM spec and default serial port UUID there is no need to specify any UUID.

如果您研究如何从 android 连接到它,您可能最终会找到 BlueTerm android 应用程序.这是一个开源应用程序,因此您可以在 https 上找到所有资源://github.com/johnhowe/BlueTerm/tree/master/src/es/pymasde/blueterm.您可以在那里找到此串行端口服务的 UUID:

If you look into how to connect to this from android you will likely end up finding the BlueTerm android app. This is an open source app so you can find all sources at https://github.com/johnhowe/BlueTerm/tree/master/src/es/pymasde/blueterm. There you can find the UUID for this serial port service:

private static final UUID SerialPortServiceClass_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

完整的 BluetoothSerialService 类是管理蓝牙连接的好方法,包括处理返回 UI 的消息.

The complete BluetoothSerialService class is a good way of managing a bluetooth connection including handling messages back to UI.