How to connect pybluez RFCOMM server socket on Debian?

爷,独闯天下 提交于 2019-12-10 10:16:35

问题


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


回答1:


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?



来源:https://stackoverflow.com/questions/17399146/how-to-connect-pybluez-rfcomm-server-socket-on-debian

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!