3

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

I don't know whether it is looping at that server_sock.accept() function, because the cpu processor usage is minimum like 4% when the code is waiting for connection.

Can anyone give a solution?

2
  • Bluetooth sometimes requires root permissions, have you tried running your script with sudo python yourscript? Jan 24, 2016 at 17:20
  • oh. Yup. Thanks. It runs now. But another problem, it is in waiting for connection state even after pairing. For pairing, I run this program in one terminal and use another terminal to pair using the command: sudo bluez-simple-agent hci0 macaddress, Even after pairing, the socket connection is not getting accepted. It seems to be looping in that .accept() function itself. How to solve this? Or do I have to run another client program in the other end after pairing? Jan 27, 2016 at 4:46

1 Answer 1

2

The issue is resolved. It was an out-of-box solution actually. The problem was with the operating power of the pi. The pi was powered from my laptop's USB port and it's voltage was about 4.4V. USB 2.0 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.

I just changed the source, I fetched power via a 1000 mA 5v adapter and checked the voltage in the pi, it was about 4.64V and it worked fine. That is now the connection was extablished. Always make sure that the raspberry pi operating voltage is in the optimum voltage.

In case you are using a usb hub to extend the usb port, use an externally powered usb hub. A USB 2.0 port provides only 2.5 W of power. A USB mouse requires only 100 mA to operate, I am not sure about keyboard. In windows system you can check how much current does a USB device require. Please refer to this link for more information.

Check the second answer from that link.

Thank You!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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