本文介绍了我怎样才能让用Python树莓派和蓝兆WT11i蓝牙模块之间的连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图让我的betweet和树莓派使用Python一蓝兆WT11i蓝牙模块的连接。
I'm trying to make a Connection betweet my RaspberryPi and a Bluegiga WT11i Bluetooth Module using Python.
到现在为止我来了:
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')
下面是code(从PyBluez例子)...
Here is the code (from PyBluez examples) ...
sdp-browse.py:
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:
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()
我该怎么办?
推荐答案
明白了!
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
现在我可以连接...
Now I can connect ...
pi@raspberrypi ~ $ python rfcomm-client.py 00:07:80:54:CA:E2
这篇关于我怎样才能让用Python树莓派和蓝兆WT11i蓝牙模块之间的连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!