如何使用Python在Raspberry Pi和Bluegiga WT11i蓝牙模块之间建立连接?

时间:2013-07-24 10:21:09

标签: python bluetooth raspberry-pi

我正在尝试使用Python在我的RaspberryPi和Bluegiga WT11i蓝牙模块之间进行连接。

到目前为止,我来了:

pi@raspberrypi ~ $ hcitool scan
Scanning ...
        00:07:80:54:CA:E2       BGWT11i

接下来......

pi@raspberrypi ~ $ python sdp-browse.py 00:07:80:54:CA:E2
found 1 services on 00:07:80:54:CA:E2

Service Name: Bluetooth Serial Port
  Host: 00:07:80:54:CA:E2
  Description: None
  Provided By: None
  Protocol: RFCOMM
  channel/PSM: 1
  svc classes: ['00001101-0000-1000-8000-00805F9B34FB']
  profiles: []
  service id: None

接下来......

pi@raspberrypi ~ $ python rfcomm-client.py 00:07:80:54:CA:E2
searching 00:07:80:54:CA:E2 for service
connecting to "Bluetooth Serial Port" on 00:07:80:54:CA:E2
Traceback (most recent call last):
  File "rfcomm-client.py", line 28, in <module>
    sock.connect((host, port))
  File "<string>", line 5, in connect
bluetooth.btcommon.BluetoothError: (111, 'Connection refused')

这是代码(来自PyBluez的例子)......

sdp-browse.py:

import sys
import bluetooth

if len(sys.argv) < 2:
        print "usage: sdp-browser <addr>"
        print "   addr can be a bluetooth address, \"localhost\" or \"all\""
        sys.exit(2)

target = sys.argv[1]
if target == "all" : target = None

services = bluetooth.find_service(address=target)

if len(services) > 0:
        print "found %d services on %s" % (len(services), sys.argv[1])
        print
else:
        print "no services found"

for svc in services:
        print "Service Name: %s" % svc["name"]
        print "  Host: %s" % svc["host"]
        print "  Description: %s" % svc["description"]
        print "  Provided By: %s" % svc["provider"]
        print "  Protocol: %s" % svc["protocol"]
        print "  channel/PSM: %s" % svc["port"]
        print "  svc classes: %s" % svc["service-classes"]
        print "  profiles: %s" % svc["profiles"]
        print "  service id: %s" % svc["service-id"]

rfcomm-client.py:

from bluetooth import *
import sys

addr = None

if len(sys.argv) < 2:
        print "need address"
        sys.exit(0)
else:
        addr = sys.argv[1]
        print "searching %s for service" % sys.argv[1]

uuid = "00001101-0000-1000-8000-00805f9b34fb"
service_matches = find_service( uuid = uuid, address = addr )

if len(service_matches) == 0:
        print "couldn't find the 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)

sock=BluetoothSocket( RFCOMM )
sock.connect((host, port))

print "connected. type stuff"
while True:
        data = raw_input()
        if len(data) == 0: break;
        sock.send("%s\n\r" % data)

sock.close()

我该怎么办?

1 个答案:

答案 0 :(得分:2)

搞定了!!

pi@raspberrypi ~ $ aptitude install bluetooth
pi@raspberrypi ~ $ hcitool dev
Devices:
   hci0   00:07:80:54:CA:E2

pi@raspberrypi ~ $ hcitool scan
Scanning ...
        00:07:80:54:CA:E2   BGWT11i

pi@raspberrypi ~ $ bluez-simple-agent hci0 00:07:80:54:CA:E2
Enter PIN Code: 1234

现在我可以连接......

pi@raspberrypi ~ $ python rfcomm-client.py 00:07:80:54:CA:E2