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

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

问题描述

所以,我一直在一个项目中,设备运行的是Android(API级别= 14)必须连接到运行Linux的服务器(具体:一个树莓派)通过蓝牙。当建立一个连接,应用程序发送一个加密的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的最新版本之间的连接包)。零售物价指数已经从泰格斯蓝牙4.0适配器。当我尝试从应用程序到RPI发送一个字符串,其中,我停留在点上,是。蓝牙接口似乎是由然后关闭。给人的logcat消息连接复位

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.

用于创建套接字的code如下:

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-00805F9B34FB 94f39d29-7d6d- 437d-973B-fba39e49d4ee 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配置是连接问题的原因(如在的这个答案。禁用 PNAT 插件在 /etc/bluetooth/main.conf 允许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天全站免登陆