在Debian上使用PyBluez实现无配对的RFCOMM?

17 投票
1 回答
14525 浏览
提问于 2025-04-17 14: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上测试时,客户端连接被拒绝。在系统日志中,有来自bluetoothd的消息,显示链接密钥请求和PIN请求失败。

版本信息:

  • PyBluez 0.18
  • Python 2.6
  • Bluez 4.66
  • 连接两端的蓝牙硬件都是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的代码不需要做任何改动。

撰写回答