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

查看:120
本文介绍了如何使用蓝牙上的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.

我的问题:

我的应用程序无法连接到树莓派,并且由于我不知道树莓派的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应用程序连接树莓派,请执行以下操作:

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

  • 需要知道树莓派的UUID是什么,如果是的话,如何找到?
  • 需要在我的android应用中使用其他方法吗?

我真的很想知道如何解决此问题,因此将不胜感激.

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

推荐答案

快速解答

类似于您从蓝牙连接开始的方式,它既需要设备地址,又需要服务UUID .

设备地址( ex: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( ex: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 .请注意,您可以通过仅选择文件">新建">示例项目",然后在Android Studio中搜索蓝牙聊天"来检出该项目.

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示例在树莓派上启动此类服务:

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 ++代码在树莓派上实现这种功能,我可以推荐

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天全站免登陆