如何使用 Android 应用程序通过蓝牙连接到 Raspberry pi [英] How to connect to Raspberry pi with an android app over bluetooth

查看:48
本文介绍了如何使用 Android 应用程序通过蓝牙连接到 Raspberry pi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用应用通过蓝牙将智能手机连接到树莓派.

I'm having trouble connecting my smartphone to my raspberry pi over bluetooth using an app.

我的情况:

我正在开发一个基于树莓派的蓝牙可控应用程序.我能够通过蓝牙连接到树莓派,并使用应用程序蓝牙终端"通过 RFCOMM 套接字发送和接收数据.树莓派一直在监听 RFCOMM 连接.

I'm developing a bluetooth controllable application based on a raspberry pi. I'm able to connect to the raspberry pi over bluetooth and send and receive data over an RFCOMM socket using the app "bluetooth terminal". The raspberry pi is constantly listening for RFCOMM connection.

我的目标:

我想开发一个应用程序,用户可以在其中通过蓝牙连接树莓派.应用程序应打开 RFCOMM 套接字,以便它可以与 pi 通信.

I want to develop an app in which the user can connect with the raspberry pi over bluetooth. The app should open the RFCOMM socket so it can communicate with the pi.

我的问题:

我的应用程序无法连接到 raspberry pi,因为我不知道 raspberry pi 的 UUID,我认为这可能是问题所在.

My app is not able to connect to the raspberry pi and since i don't know the UUID of the raspberry pi, i think that might be the problem.

我的代码:

我对 Java 编程很陌生,所以如果你看到任何奇怪的东西,请纠正我.这是我尝试连接的方法.

I'm quite new to java programming so correct me if you see anything strange. This is the method which i'm trying to connect with.

        public void BTConnect() {

    final UUID MY_UUID = UUID.fromString("0000110E-0000-1000-8000-00805F9B34FB");
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    BluetoothSocket socket = null;
    String RPi_MAC = "XX:XX:XX:XX:XX:XX";

    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    // If there are paired devices
    if (pairedDevices.size() > 0) {

        // Loop through paired devices
        for (BluetoothDevice device : pairedDevices) {
            if (device.getAddress().equals(RPi_MAC)) {
                try {
                    socket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
                } catch (IOException e0) {
                    Log.d("BT_TEST", "Cannot create socket");
                    e0.printStackTrace();
                }

                try {
                    socket.connect();
                } catch (IOException e1) {
                    try {
                        socket.close();
                        Log.d("BT_TEST", "Cannot connect");
                        e1.printStackTrace();
                    } catch (IOException e2) {
                        Log.d("BT_TEST", "Socket not closed");
                        e2.printStackTrace();
                    }
                }
            }
        }
    }
}

当我点击按钮连接时,这是android studio的输出:

When I click the button to connect, this is the output of android studio:

   W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
   D/BT_TEST: Cannot connect
   W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
   W/System.err:     at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:599)
   W/System.err:     at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:610)
   W/System.err:     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:333)
   W/System.err:     at com.example.gebruiker.soundslikepi.MainActivity.BTConnect(MainActivity.java:80)
   W/System.err:     at com.example.gebruiker.soundslikepi.MainActivity$1.onClick(MainActivity.java:39)
   W/System.err:     at android.view.View.performClick(View.java:4856)
   W/System.err:     at android.view.View$PerformClick.run(View.java:19956)
   W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
   W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
   W/System.err:     at android.os.Looper.loop(Looper.java:211)
   W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5373)
   W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
   W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
   W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
   W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

总结:

因此,为了能够从 Android 应用程序连接到 raspberry pi,我应该:

So, to be able to connect with the raspberry pi from an android app, do I:

  • 需要知道 raspberry pi 的 UUID 是什么,如果是,我如何知道?
  • 需要在我的 Android 应用中使用不同的方法吗?

我真的很想知道如何解决这个问题,所以任何帮助将不胜感激.

I would really like to know how to fix this problem, so any help would be appreciated.

推荐答案

快速解答

与您开始时的方式类似,蓝牙连接需要设备地址服务 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.

这篇关于如何使用 Android 应用程序通过蓝牙连接到 Raspberry pi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆