Android 和 Linux (RPi) 之间的蓝牙连接在第一次写入操作时丢失 [英] Bluetooth connection between Android and Linux (RPi) lost on first write action

查看:19
本文介绍了Android 和 Linux (RPi) 之间的蓝牙连接在第一次写入操作时丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直致力于一个项目,其中运行 Android(API 级别 = 14)的设备必须通过蓝牙连接到运行 Linux(具体来说:Raspberry Pi)的服务器.建立连接后,应用程序将加密的 XML 字符串发送到 RPi.RPi 必须解密这个字符串,解析 XML 并执行相应的操作.操作的结果发送回 Android 设备.

So I have been working on a project in which a device running Android (API level = 14) must connect to a server running Linux (to be specific: a Raspberry Pi) via Bluetooth. When a connection is established, the app sends an encrypted XML string to the RPi. The RPi must decrypt this string, parse the XML and perform the corresponding action. The result of the action is send back to the Android device.

到目前为止,我已经设法在应用程序和 RPi(运行最新版本的 Bluez 包).RPi 有一个来自 Targus 的蓝牙 4.0 加密狗.我遇到的问题是,当我尝试从应用程序向 RPi 发送字符串时.届时蓝牙插座似乎已关闭.Logcat 给出消息Connection reset by peer.

So far, I have managed to create a connection between the app and the RPi (which runs the latest version of the Bluez package). The RPi has a Bluetooth 4.0 dongle from Targus. The point where I'm stuck at, is when I try to send a string from the app to the RPi. The Bluetooth socket appears to be closed by then. Logcat gives the message Connection reset by peer.

用于创建socket的代码如下:

The code used to create the socket is as follows:

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device, 1);

Logcat 输出如下:

Logcat output is as follows:

06-20 14:29:42.224: DEBUG/RPiService(24273): ---------- [ CONNECTION ESTABLISHED ] ----------
06-20 14:29:42.224: DEBUG/RPiService(24273): connected, Socket Type:Secure
06-20 14:29:42.229: DEBUG/RPiService(24273): create ConnectedThread: Secure
06-20 14:29:43.734: DEBUG/RPiService(24273): setState() 2 -> 3
06-20 14:29:43.739: DEBUG/RPiService(24273): Connection reset by peer
06-20 14:29:43.744: WARN/System.err(24273): java.io.IOException: Connection reset by peer
06-20 14:29:43.754: WARN/System.err(24273): at android.bluetooth.BluetoothSocket.writeNative(Native Method)
06-20 14:29:43.759: WARN/System.err(24273): at android.bluetooth.BluetoothSocket.write(BluetoothSocket.java:398)
06-20 14:29:43.764: WARN/System.err(24273): at android.bluetooth.BluetoothOutputStream.write(BluetoothOutputStream.java:85)
06-20 14:29:43.769: WARN/System.err(24273): at com.example.BluetoothTest.RPiService$ConnectedThread.run(RPiService.java:344)

在 RPi 方面,我实际上是从 PyBluez 包:

On the side of the RPi, I am essentially running the following example server script from the PyBluez package:

from bluetooth import *

server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)

port = server_sock.getsockname()[1]

uuid = "00001101-0000-1000-8000-00805F9B34FB"

advertise_service( server_sock, "SampleServer",
    service_id = uuid,
    service_classes = [ uuid, SERIAL_PORT_CLASS ],
    profiles = [ SERIAL_PORT_PROFILE ]
)

print "Waiting for connection on RFCOMM channel %d" % port

client_sock, client_info = server_sock.accept()
print "Accepted connection from ", client_info

try:
    while True:
        data = client_sock.recv(1024)
        if len(data) == 0: break
        print "received [%s]" % data
except IOError:
    pass

print "disconnected"

client_sock.close()
server_sock.close()
print "all done"

我尝试了我在 SO 上阅读的帖子建议的各种 UUID,包括 00001101-0000-1000-8000-00805F9B34FB94f39d29-7d6d-437d-973b-fba39e49d和 00000003-0000-1000-8000-00805F9B34FB(在连接的两端始终相同).第一个似乎是正确的,因为在使用其他 UUID 时我什至无法建立连接.

I've tried various UUIDs suggested by posts I read on SO including 00001101-0000-1000-8000-00805F9B34FB, 94f39d29-7d6d-437d-973b-fba39e49d4ee and 00000003-0000-1000-8000-00805F9B34FB (always the same on both ends of the connection). It appears to be that the first one is correct as I can't even make a connection when using an other UUID.

连接被 RPi 重置的原因可能是什么?如果有人能够为我指明正确的方向,我将不胜感激.

What may be the cause for the connection to be reset by the RPi? If anyone would be able to point me in the right direction, I'd be grateful.

推荐答案

事实证明,Debian 上的默认 Bluez 配置是导致连接问题的原因(如 这个答案.禁用 /etc/bluetooth/main.conf 中的 pnat 插件允许 Android 和 RPi 之间的通信.

It turned out that the default Bluez configuration on Debian was the cause of the connection issues (as described in this answer. Disabling the pnat plugin in /etc/bluetooth/main.conf allowed for communication between Android and the RPi.

DisablePlugins = pnat

供将来参考,应用程序使用的 UUID 为 00000003-0000-1000-8000-00805F9B34FB.

For future reference, the UUID used by the applications is 00000003-0000-1000-8000-00805F9B34FB.

这篇关于Android 和 Linux (RPi) 之间的蓝牙连接在第一次写入操作时丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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