Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using Python to write a simple serial Bluetooth script that sends information about my computer stats periodically. The receiving device is a Sparkfun BlueSmirf Silver.

The problem is that, after the script runs for a few minutes, it stops sending packets to the receiver and fails with the error:

(11, 'Resource temporarily unavailable')

Noticing that this inevitably happens, I added some code to automatically try to reopen the connection. However, then I get:

Could not connect:  (16, 'Device or resource busy')

Am I doing something wrong with the connection? Do I need to occasionally reopen the socket? I'm not sure how to recover from this type of error.

I understand that sometimes the port will be busy and a write operation is deferred to avoid blocking other processes, but I wouldn't expect the connection to fail so regularly. Any thoughts?

Here is the script:

import psutil
import serial
import string
import time
import bluetooth

sampleTime = 1
numSamples = 5
lastTemp = 0


filename = '/sys/bus/platform/devices/applesmc.768/temp2_input'

def parseSensorsOutputLinux(output):
    return int(round(float(output) / 1000))

def connect():
            gaugeSocket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
            gaugeSocket.connect(('00:06:66:42:22:96', 1))
        except bluetooth.btcommon.BluetoothError as error:
            print "Could not connect: ", error, "; Retrying in 5s..."
    return gaugeSocket;

gaugeSocket = connect()
    usage = psutil.cpu_percent(interval=sampleTime)
    sensorFile = open(filename)
    temp = parseSensorsOutputLinux(
        #print("Wrote usage: " + str(int(usage)))

        #print("Wrote temp: " + str(temp))
    except bluetooth.btcommon.BluetoothError as error:
        print "Caught BluetoothError: ", error
        gaugeSocket = connect()


EDIT: I should add that this code connects fine after I power-cycle the receiver and start the script. However, it fails after the first exception until I restart the receiver.

P.S. This is related to my recent question, Why is /dev/rfcomm0 giving PySerial problems?, but that was more about PySerial specifically with rfcomm0. Here I am asking about general rfcomm etiquette.

share|improve this question
up vote 0 down vote accepted

The answer to this question is the same as the answer to Why is /dev/rfcomm0 giving PySerial problems?, a simple issue of the Bluetooth buffer overflowing on the PC.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.