4

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))
d-cubed
  • 1,034
  • 5
  • 30
  • 58
  • Markus Blechschmidt, what is exact problem and what is your question? – osgx Apr 27 '14 at 03:50
  • I added a bounty because I have a related problem. The closest that bluetooth (as described above) comes to connecting to the devices is that a ping is sent the NXT asks for a password. (Supposedly this has to do with the computer initiating contact - not the NXT). Looking at the NXT documentation didn't hint at any passkeys. This looks related but it's not: http://stackoverflow.com/questions/1972605/lego-mindstorm-nxt-2-0-error-nxt-bluetooth-passkey-confirmation-failed – d-cubed Apr 28 '14 at 12:06

2 Answers2

3

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.

d-cubed
  • 1,034
  • 5
  • 30
  • 58
0

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.

1j01
  • 3,714
  • 2
  • 29
  • 30