本文介绍了如何在 Debian 上连接 pybluez RFCOMM 服务器套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在带有 pybluez 框架的 python 中有这些片段:

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"

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

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);

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

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

推荐答案

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

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.

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

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

DisablePlugins = pnat

然后重启蓝牙服务:

sudo invoke –rc.d bluetooth restart

现在可能已修复.

祝你好运!

参考:RFCOMM 没有在 Debian 上使用 PyBluez 配对?

这篇关于如何在 Debian 上连接 pybluez RFCOMM 服务器套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-20 14:17