3

I have these snippet in python with pybluez framework:

from bluetooth import *

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, "SampleServer",
                   service_id = uuid
                   # service_classes = [ uuid, SERIAL_PORT_CLASS ],
                   # profiles = [ SERIAL_PORT_PROFILE ],
                   # protocols = [ RFCOMM_UUID ]
                    )

print "Waiting for connection on RFCOMM channel %d" % port

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

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

print "disconnected"

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

and also I have this other snippet in Android to connect the pybluez rfcomm server socket:

private static final UUID MY_UUID = UUID.fromString("94f39d29-7d6d-437d-973b-fba39e49d4ee"); 
....
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(myServerMacAddress);
....
BluetoothSocket tmp= device.createInsecureRfcommSocketToServiceRecord(MY_UUID);

My problem is that Android device could not connect to the pybluez socket. I think that the way I use to connect is wrong, and I don't know how to connect correctly or advertise my server socket

Thomasleveil
  • 95,867
  • 15
  • 119
  • 113
Pooya
  • 4,385
  • 6
  • 45
  • 73

1 Answers1

4

I offered a bounty, but found the solution myself. :) Posted on another answer but this may also apply to your problem. On certain versions of Debian (Raspbian etc) and maybe some others distros. The server_sock.accept() will by default just hang and never accept a connection - even from a paired device! I'm in some cases even convinced the socket isn't open at all. However, a solution to this is really simple.

Update your /etc/bluetooth/main.conf file, add a line or change the existing so it looks like this:

DisablePlugins = pnat

Then restart the Bluetooth service:

sudo invoke –rc.d bluetooth restart

It now MAY have been fixed.

Good luck!

Reference: RFCOMM without pairing using PyBluez on Debian?

Community
  • 1
  • 1
user1840255
  • 287
  • 1
  • 6
  • 15