在Debian系统上使用PyBluez实现无需配对的RFCOMM连接?

17
我正在试图使用Python创建一个RFCOMM服务器进程,可以在不需要配对的情况下使用。最初,我从PyBluez文档中获取了两个示例脚本:

服务器:

# file: rfcomm-server.py

# auth: Albert Huang <albert@csail.mit.edu>
# desc: simple demonstration of a server application that uses RFCOMM sockets
#
# $Id: rfcomm-server.py 518 2007-08-10 07:20:07Z albert $

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 = [ OBEX_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"

客户端:

# file: rfcomm-client.py
# auth: Albert Huang <albert@csail.mit.edu>
# desc: simple demonstration of a client application that uses RFCOMM sockets
#       intended for use with rfcomm-server
#
# $Id: rfcomm-client.py 424 2006-08-24 03:35:54Z albert $

from bluetooth import *
import sys

addr = None

if len(sys.argv) < 2:
    print "no device specified.  Searching all nearby bluetooth devices for"
    print "the SampleServer service"
else:
    addr = sys.argv[1]
    print "Searching for SampleServer on %s" % addr

# search for the SampleServer service
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
service_matches = find_service( uuid = uuid, address = addr )

if len(service_matches) == 0:
    print "couldn't find the SampleServer service =("
    sys.exit(0)

first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]

print "connecting to \"%s\" on %s" % (name, host)

# Create the client socket
sock=BluetoothSocket( RFCOMM )
sock.connect((host, port))

print "connected.  type stuff"
while True:
    data = raw_input()
    if len(data) == 0: break
    sock.send(data)

sock.close()

当我在Windows上运行服务器脚本时,一切都按照我的期望进行 - 不需要配对。此时一切看起来非常有前途。然而,我需要在Debian Squeeze下运行服务器进程。在Debian上测试时,客户端连接被拒绝。在syslog中,bluetoothd出现了关于失败的链接密钥请求和PIN请求的消息。 版本信息:PyBluez 0.18、Python 2.6、Bluez 4.66、连接的两端均为Bluetooth v2.0硬件。这个讨论似乎表明,如果我可以调整服务器套接字的安全级别,则配对将被禁用,并且一切都会按预期工作。但是,我不知道如何使用PyBluez进行此操作,甚至不确定是否可能。我尝试使用各种BT_SECURITY*常量调用setsockopt(),以及获取最新的PyBluez并调用setl2capsecurity(),但是没有取得任何进展。使用PyBluez能实现这个目标吗?
1个回答

18

这实际上是Debian Squeeze蓝牙默认配置的问题。

如果有其他人遇到这个问题,请通过编辑/etc/bluetooth/main.conf禁用pnat插件:

DisablePlugins = pnat

然后重新启动bluetoothd。

$ sudo invoke-rc.d bluetooth restart

PyBluez代码无需进行任何更改。


谢谢!这也是在Ubuntu 12.04下的一个问题! - Unapiedra
谢谢!非常有帮助。我也在使用Ubuntu 12.04。我还发现杀掉蓝牙代理进程有所帮助。 - jagsaund
这个Debian错误报告有更多信息:http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=690749 - Tim Connolly
很奇怪,我在使用Pi3的Raspbian上也遇到了同样的错误,但是那个修复方法对我不起作用。 - Cerin
最终我通过从源码构建来解决了原本的问题。让我惊讶的是它一开始就如预期地工作了。因此,我必须确保所有插件都被包含进来,这才复制了这个问题。 - Tim Connolly
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接