2013-07-24 106 views
2

我正在嘗試使用Python在RaspberryPi和Bluegiga WT11i藍牙模塊之間建立連接。如何使用Python在Raspberry Pi和Bluegiga WT11i Bluetooth模塊之間建立連接?

到現在爲止我來:

[email protected] ~ $ hcitool scan 
Scanning ... 
     00:07:80:54:CA:E2  BGWT11i 

下一個...

[email protected] ~ $ 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 

下一個...

[email protected] ~ $ 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() 

我能做些什麼?

回答

相關問題