Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android和Linux(RPi)之间的蓝牙连接在第一次写入操作时丢失_Android_Linux_Bluetooth_Raspberry Pi_Bluez - Fatal编程技术网

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

Android和Linux(RPi)之间的蓝牙连接在第一次写入操作时丢失,android,linux,bluetooth,raspberry-pi,bluez,Android,Linux,Bluetooth,Raspberry Pi,Bluez,所以我一直在做一个项目,在这个项目中,运行Android(API级别=14)的设备必须通过蓝牙连接到运行Linux的服务器(具体来说:Raspberry Pi)。建立连接后,应用程序将向RPi发送加密的XML字符串。RPi必须解密这个字符串,解析XML并执行相应的操作。该操作的结果将发送回Android设备 到目前为止,我已经成功地在应用程序和RPi(运行最新版本的)之间建立了连接。RPi有一个来自Targus的蓝牙4.0加密狗。当我试图从应用程序向RPi发送字符串时,我就陷入了困境。此时蓝牙插

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

到目前为止,我已经成功地在应用程序和RPi(运行最新版本的)之间建立了连接。RPi有一个来自Targus的蓝牙4.0加密狗。当我试图从应用程序向RPi发送字符串时,我就陷入了困境。此时蓝牙插座似乎已关闭。Logcat给出消息“对等方重置连接”

用于创建套接字的代码如下所示:

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)
在RPi方面,我基本上是从包中运行以下示例服务器脚本:

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


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

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

DisablePlugins = pnat

作为将来的参考,应用程序使用的UUID是
0000000 3-0000-1000-8000-00805F9B34FB

您能否将解决方案作为答案发布,以便在人们查找未回答的问题时不会显示此问题?谢谢!
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"
DisablePlugins = pnat