title: Connection refused when trying to connect to bluetooth server
body: <p>I am trying to build a simple Bluetooth client / server where my Raspberry Pi is the server and my laptop the client. </p>
  
  <p>This is the server code (running on my Raspberry Pi):</p>
  
  <pre><code>#!/usr/bin/python
  # -*- coding: utf-8
  
  import wifi, bluetooth
  
  uuid="1e0ca4ea-299d-4335-93eb-27fcfe7fa848"
  
  print "Setting up Bluetooth socket"
  
  try:
    sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
    sock.bind(("", 0))
    sock.listen(1)
  except IOError as e:
    print str(e)
  
  
  print "Registering service"
  try:
    bluetooth.advertise_service(sock, "MyService", uuid)
  
    while True:
      print "Waiting for connection..."  
      client_sock,address = sock.accept()
      print "Accepted connection from {0}".format(address)
  
      data = client_sock.recv(1024)
      print "Received data: {0}".format(data)
  
      print "Closing client socket."
      client_sock.close()
  except IOError as e:
    print str(e) 
  </code></pre>
  
  <p>This seems to work, the script runs and blocks with <code>Waiting for connection...</code>.</p>
  
  <p>Then, my client code:</p>
  
  <pre><code>#!/usr/bin/python
  # -*- coding: utf-8
  
  import bluetooth, time
  
  mac = "00:15:83:E5:E2:46"
  uuid = "1e0ca4ea-299d-4335-93eb-27fcfe7fa848"
  
  service = []
  retry = 1
  while len(service) == 0:
    print "Looking for service on {0}, try {1}".format(mac, retry)
    service = bluetooth.find_service(address=mac, uuid=uuid)
    retry = retry + 1
    time.sleep(1)
  
  if len(service) == 1: 
    service = service[0]
    print "Service found. Name={0}".format(service["name"])
  
    print "Connecting to service."
  
    sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
    try:
      sock.connect((mac, service["port"]))
      print "Connected to service on {0} on port {1}".format(mac, service["port"])
    except bluetooth.btcommon.BluetoothError as e:
      print "Connection failed: {0}".format(e)
  
  elif len(service) == 0:
    print "No service found on mac {0}.".format(mac)
  else:
    print "{0} services found for mac/uuid, ignored.".format(len(service))
  </code></pre>
  
  <p>Also works, up until I try to <code>connect()</code> to the Raspberry Pi. I get the following error:</p>
  
  <pre><code>Connecting to service.
  Connection failed: (111, 'Connection refused')
  </code></pre>
  
  <p>I tried connecting the laptop to the Raspberry Pi (it finds it and says it is "Connected") and searching for more info online, but wasn't able to find anything.</p>