Search code examples
pythonbluetoothpybluez

For bluetooth connection using PyBluez, socket connection is not getting accepted even after pairing


I have a Raspberry pi model B booted with Rasbian OS and a bluetooth dongle plugged into it. I am able to discover my pi when searching from my PC and am able to pair using Blueman GUI. This is the python program I am using for the pairing process:

from bluetooth import *

server_sock=BluetoothSocket(L2CAP)
server_sock.bind(("", 17))

with open(sys.path[0] + "/sdp_record.xml", "r") as fh:
        service_record = fh.read()
self.bus = dbus.SystemBus()
self.manager = dbus.Interface(self.bus.get_object("org.bluez", "/"),"org.bluez.Manager")
adapter_path = self.manager.DefaultAdapter()
self.service = dbus.Interface(self.bus.get_object("org.bluez",adapter_path),
                                                       "org.bluez.Service")
service_handle = service.AddRecord(service_record)
print "Service record added"
server_sock.listen(1)

print("Waiting for connection on L2CAP")

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

    while True:
        data = client_sock.recv(1024)
        if len(data) == 0:
                break
        print("received [%s]" % data)
except IOError:
    pass
except KeyboardInterrupt:
    print "Stopping..."
    stop_advertising(server_sock)
    sys.exit()  

print("disconnected")

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

I already have a working SDP record, which is being read and it is added to the SDP server. At first when I posted this question I got this error:

Traceback (most recent call last):
  File "pytest.py", line 4, in <module>
    server_sock.bind(("", 17))
  File "/usr/lib/python2.7/dist-packages/bluetooth/bluez.py", line 140, in bind
    return self._sock.bind (addrport)
_bluetooth.error: (13, 'Permission denied')

This is because bluetooth needs root privileges. I ran the python code using sudo python code.py and this error was resolved. Thanks very much to guys who answered.

Now the problem I get is, the socket connection is not getting accepted even after pairing. For pairing, I run the code in one terminal and when it is waiting for connection on L2CAP, I open another console and pair the pi with my computer using the command sudo bluez-simple-agent hci0 computers_macaddress, it is getting paired after entering a pin in both the pi and computer. But after the pairing, the code is still waiting for connection:

Service record added
Waiting for connection on L2CAP

It seems to be looping at that server_sock.accept() function.

Can anyone give a solution?


Solution

  • I also faced the same kind of issue. The problem was with the operating power of the pi. USB ports usually provide only 500 mA, 5 V. Raspberry pi need a voltage source of about 4.75 to 5.25V and current in range of 700 to 1000 mA for optimum performance. Exactly how much current (mA) the Raspberry Pi requires is dependent on what you connect to it. Try changing the source, fetch power via a 1000 mA 5v adapter. Let me know if it worked.