Markus Blechschmidt
Markus Blechschmidt

Reputation: 101

Python Bluetooth Passkey/Password Linux

I'm working on a Python script to control my Mindstorms NXT with a Raspberry Pi. My problem is, that the NXT has a Bluetooth passkey. You can change the passkey but not delete it.

I want to know how you can connect the PyBluez socket to a device with a passkey.

This is the current program:

import bluetooth
import socket

target_name = "Jerry"
target_address = None

print "performing inquiry..."
nearby_devices = bluetooth.discover_devices()
print "found %d devices" % len(nearby_devices)

for bdaddr in nearby_devices:
    if target_name == bluetooth.lookup_name( bdaddr ):
        target_address = bdaddr
        break

if target_address is not None:
    print "found target bluetooth device with address ", target_address
else:
    print "could not find target bluetooth device nearby"

bluesock= socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
bluesock.connect((target_address, 1))

Upvotes: 4

Views: 1804

Answers (2)

1j01
1j01

Reputation: 4180

On Windows, I just had to go into go into Bluetooth settings and pair with the device, entering the passkey on Windows and then on the NXT. It never showed a screen saying that it had paired, seemingly getting stuck pairing, but it did work and I was able to connect with nxt-python.

Upvotes: 0

d-cubed
d-cubed

Reputation: 1112

I'm not sure there's a Python specific answer. The py-nxt posts I saw seemed to point at the OS.

Does starting this background process (on your computer) with a passkey help you?

bluetooth-agent 1234 &

I've found it useful to pair with the NXT first using:

hcitool cc 00:16:53:0A:17:16

Whereby, I'd found the MAC address with:

hcitool scan

If you hadn't already tried the rfcomm related bits for Linux, there's a worthwhile ref here.

Upvotes: 3

Related Questions