其他分享
首页 > 其他分享> > android-如何在Debian上连接pybluez RFCOMM服务器套接字?

android-如何在Debian上连接pybluez RFCOMM服务器套接字?

作者:互联网

我在pybluez框架的python中有以下代码段:

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"

而且我在Android中还有另一个片段来连接pybluez rfcomm服务器套接字:

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

我的问题是Android设备无法连接到pybluez套接字.
我认为我使用的连接方式是错误的,而且我不知道如何正确连接或宣传服务器套接字

解决方法:

我提供了赏金,但自己找到了解决方案. :)发表在另一个答案上,但这也可能适用于您的问题.在某些版本的Debian(Raspbian等)上,也许还有其他发行版.默认情况下,server_sock.accept()只会挂起,并且永远不会接受连接-即使来自配对设备!在某些情况下,我甚至确信套接字根本没有打开.但是,解决这个问题的方法非常简单.

更新您的/etc/bluetooth/main.conf文件,添加一行或更改现有行,如下所示:

DisablePlugins = pnat

然后重新启动蓝牙服务:

sudo invoke –rc.d bluetooth restart

现在可能已修复.

祝好运!

参考:RFCOMM without pairing using PyBluez on Debian?

标签:debian,bluetooth,bluez,python,android
来源: https://codeday.me/bug/20191123/2064974.html