Android和Linux(RPi)之间的蓝牙连接在第一次写入操作时丢失

时间:2022-06-03 18:52:49

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.

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

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.

到目前为止,我已经设法在应用程序和RPi(运行最新版本的Bluez软件包)之间创建连接。 RPi配备了Targus的蓝牙4.0加密狗。我坚持的地方是,当我尝试从应用程序向RPi发送字符串时。那时蓝牙插座似乎已关闭。 Logcat给出消息Connection by peer。

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 output is as follows:

Logcat输出如下:

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)

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

在RPi方面,我基本上是从PyBluez包运行以下示例服务器脚本:

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"

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.

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

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.

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

1 个解决方案

#1


1  

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.

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

DisablePlugins = pnat

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

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

#1


1  

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.

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

DisablePlugins = pnat

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

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