简体   繁体   English

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

[英]RFCOMM without pairing using PyBluez on Debian?

I am trying to create an RFCOMM server process with Python that can be used without the need for pairing. 我正在尝试使用Python创建一个RFCOMM服务器进程,无需配对即可使用。 Initially, I grabbed the two example scripts from the PyBluez documentation: 最初,我从PyBluez文档中抓取了两个示例脚本:

Server: 服务器:

# 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"

Client: 客户:

# 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()

When I ran the server script on Windows everything worked just how I had hoped - no pairing was necessary. 当我在Windows上运行服务器脚本时,所有工作都是我所希望的 - 不需要配对。 At this stage everything was looking very promising. 在这个阶段,一切看起来都很有希望。

However, I need the server process to run under Debian Squeeze. 但是,我需要服务器进程在Debian Squeeze下运行。 When I test on Debian the client connection is refused. 当我在Debian上测试时,客户端连接被拒绝。 In the syslog there are messages from bluetoothd for a failed link key request and PIN request. 在syslog中,有来自bluetoothd的消息,用于链接密钥请求失败和PIN请求。

Version information: 版本信息:

  • PyBluez 0.18 PyBluez 0.18
  • Python 2.6 Python 2.6
  • Bluez 4.66 Bluez 4.66
  • Bluetooth v2.0 hardware on both ends of the connection 连接两端的蓝牙v2.0硬件

This discussion seems to suggest that if I can adjust the security level on the server socket then pairing will be disabled and everything will just work as expected. 这个讨论似乎表明,如果我可以调整服务器套接字上的安全级别,那么将禁用配对,一切都将按预期工作。 It is not apparent to me how to do this with PyBluez though, or even if it is possible. 我不清楚如何用PyBluez做到这一点,或者即使它是可能的。

I have experimented with calls to setsockopt() using various BT_SECURITY* constants, as well as grabbing the last PyBluez and calling setl2capsecurity() but have not been able to make any progress. 我已尝试使用各种BT_SECURITY *常量调用setsockopt(),以及获取最后一个PyBluez并调用setl2capsecurity()但未能取得任何进展。

Is this going to be achievable with PyBluez? 这是用PyBluez实现的吗?

This turned out to be a problem with the Debian Squeeze bluez default configuration. 这被证明是Debian Squeeze bluez默认配置的问题。

If anyone else hits this problem, disable the pnat plugin by editing /etc/bluetooth/main.conf: 如果有其他人遇到此问题,请通过编辑/etc/bluetooth/main.conf来禁用pnat插件:

DisablePlugins = pnat

Then restart bluetoothd. 然后重新启动bluetoothd。

$ sudo invoke-rc.d bluetooth restart

No changes were required to the PyBluez code. PyBluez代码无需更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM