Communicating via Bluetooth serial with Python
<p>How do you process and receive serial data via Bluetooth and Python?</p>

<p>I'm trying to make a simple Python server that access data via Bluetooth as <a href="http://blog.davidvassallo.me/2014/05/11/android-linux-raspberry-pi-bluetooth-communication/" rel="noreferrer">explained here</a>.</p>

<p>My <code>server.py</code> file, which sends a random number when sent the text "temp", is:</p>

<pre><code>#!/usr/bin/env python

import os
import glob
import time
import random

from bluetooth import *

def read_temp():
    return random.random()

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

port = server_sock.getsockname()[1]

uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"

advertise_service( server_sock, "TestServer",
                   service_id = uuid,
                   service_classes = [ uuid, SERIAL_PORT_CLASS ],
                   profiles = [ SERIAL_PORT_PROFILE ], 
#                   protocols = [ OBEX_UUID ] 
                    )
while True:          
    print "Waiting for connection on RFCOMM channel %d" % port

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

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

        if data == 'temp':
            data = str(read_temp())+'!'
            client_sock.send(data)
        else:
            data = 'WTF!' 
            client_sock.send(data)
        print "sending [%s]" % data

    except IOError:
        pass

    except KeyboardInterrupt:

        print "disconnected"

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

        break
</code></pre>

<p>When I first ran this, I was getting the error:</p>

<pre><code>bluetooth.btcommon.BluetoothError: (2, 'No such file or directory')
</code></pre>

<p>which <a href="https://www.raspberrypi.org/forums/viewtopic.php?f=63&amp;t=133263" rel="noreferrer">Googled showed</a> could be fixed by enabling compatibility mode and loading the serial profile by editing file <code>/lib/systemd/system/bluetooth.service</code> and changing line:</p>

<pre><code>ExecStart=/usr/lib/bluetooth/bluetoothd
</code></pre>

<p>to:</p>

<pre><code>ExecStart=/usr/lib/bluetooth/bluetoothd -C
</code></pre>

<p>and then running <code>sudo sdptool add SP</code>.</p>

<p>Now <code>python server.py</code> seems to run without error, and I can pair the machine running it to my Android phone. However, it seems it's unable to receive any data.</p>

<p>I've tried using bluetooth terminal emulation apps like BlueTerm, BT Simple Terminal, and Arduino BT, but when I connect to the server enter text and press enter, there's no response from <code>server.py</code>. It will initially report a "Accepted connection..." and having received and empty string, but it receives nothing after that.</p>

<p>Since there's no explicit error being reported, I'm not sure how to diagnose the problem. How would I determine if the problem lies in my Python code? Or the bluetooth configuration on the server? Or my Android phone?</p>