Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bluetooth connection between Android and Linux (RPi) lost on first write action

Tags:

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.

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.

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:

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:

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.

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.

like image 605
Kaj Nelissen Avatar asked Jun 20 '13 14:06

Kaj Nelissen


1 Answers

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 

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

like image 93
Kaj Nelissen Avatar answered Nov 11 '22 20:11

Kaj Nelissen