Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Initially, I grabbed the two example scripts from the PyBluez documentation:

Server:

# file: rfcomm-server.py

# auth: Albert Huang <[email protected]>
# 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 <[email protected]>
# 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. At this stage everything was looking very promising.

However, I need the server process to run under Debian Squeeze. When I test on Debian the client connection is refused. In the syslog there are messages from bluetoothd for a failed link key request and PIN request.

Version information:

  • PyBluez 0.18
  • Python 2.6
  • Bluez 4.66
  • Bluetooth v2.0 hardware on both ends of the connection

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.

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.

Is this going to be achievable with PyBluez?

like image 257
Tim Connolly Avatar asked Jan 31 '13 04:01

Tim Connolly


People also ask

Can I use RFCOMM with an unpaired device?

The Android SDK doesn't seem to really support unpaired connections: The current Android Bluetooth API's require devices to be paired before an RFCOMM connection can be established. (Pairing is automatically performed when you initiate an encrypted connection with the Bluetooth APIs.)

How do I connect to pybluez without a pin?

If you're using a Windows PC, you can go through the laborious create a new connection process and choose "don't use a pin", although Windows will probably still ask you for a pin when you connect (0000 usually works, but it's very flaky). If you're using python with pybluez, it's very simple:

Does pybluez work with the nRF52840?

The nRF52840 is strictly a BLE device. PyBluez and rfcomm will be of no help in connecting to it. Does your phone connect to the sensor? Does your phone connect to the sensor? Yes. I using nRF Connect app. Also I tested Bleak Lib. The RPi only connects to the sensor for about one second and disconnects suddenly.

Does insecure rfcom require pairing for data transfer?

Use the insecureRfcom respect of secure RFCOM. It does not require pairing for data transfer. The result of a successful pairing is the link key, the secret that paired devices share and enable them to communicate. If it is possible to set the link key on the system, then yes, no pairing is needed.


1 Answers

This turned out to be a problem with the Debian Squeeze bluez default configuration.

If anyone else hits this problem, disable the pnat plugin by editing /etc/bluetooth/main.conf:

DisablePlugins = pnat

Then restart bluetoothd.

$ sudo invoke-rc.d bluetooth restart

No changes were required to the PyBluez code.

like image 54
Tim Connolly Avatar answered Oct 11 '22 13:10

Tim Connolly