我想把我的android手机连接到我的覆盆子,变形蓝牙连接。我在我的树莓里插入了一个hc-05模块,它可以工作。我可以从我的智能手机上读取一些设备信息,如果我修改这些文件
/boot/cmdline.txt /etc/inittab
对于使用蓝牙终端接入智能手机,它是可行的。如果我用:
hcitool dev
我得到了Devices:
/etc/init.dbluetooth status
我得到了[ ok ] bluetooth is running.
我无法用python脚本访问蓝牙模块。例如:
from bluetooth import *
targetName="Madlollo"
targetAddress=None
port=2
def searchService():
mServices=find_service(uuid="00001101-0000-1000-8000-00805F9B34FB")
if len(mServices)==0:
print"can't find available service"
else:
first_match=mServices[0]
mPort=first_match["port"]
mName=first_match["name"]
mHost=first_match["host"]
print"Discovered %s on %s at port %s",(name,host,port)
def searchPhone():
global targetAddress
nearbyDevices=discover_devices()
for address in nearbyDevices:
if targetName==lookup_name(address):
targetAddress=address
break
if targetAddress is not None:
print"found target Phone with address: ", targetAddress
else:
print"can't find target Phone"
def sendMsg(msg):
sock=BluetoothSocket(RFCOMM)
sock.connect((targetAddress,port))
a=sock.send(msg)
if(a>0):
print"inviati %d byte" %a
sock.close()
def readData():
server_sock=BluetoothSocket(RFCOMM)
#rport=get_available_port(RFCOMM)
rport=0
server_sock.bind(("",rport))
server_sock.listen(1)
print"listening on port %d" %rport
#advertise_service(server_sock,"00001101-0000-1000-8000-00805F9B34FB")
client_sock,address=server_sock.accept()
data=client_sock.recv(1024)
print"received[$s]"%data
client_sock.close()
server_sock.close()
如果使用searchPhone()
或searchService()
,则会出现以下错误:
File "blue.py", line 52, in <module>
searchService()
File "blue.py", line 7, in searchService
mServices=find_service(uuid="00001101-0000-1000-8000-00805F9B34FB")
File "/usr/local/lib/python2.7/dist-packages/bluetooth/bluez.py", line 186, in find_service
devices = discover_devices ()
File "/usr/local/lib/python2.7/dist-packages/bluetooth/bluez.py", line 17, in discover_devices
sock = _gethcisock ()
File "/usr/local/lib/python2.7/dist-packages/bluetooth/bluez.py", line 226, in _gethcisock
raise BluetoothError ("error accessing bluetooth device")
bluetooth.btcommon.BluetoothError: error accessing bluetooth device
我需要从hcitool找到蓝牙模块。有人知道如何用覆盆子控制hc-05吗?
谢谢
发布于 2015-11-11 15:47:55
它应该工作,但它可能需要一个密码的HC-05模块。https://myraspberryandme.wordpress.com/2013/11/20/bluetooth-serial-communication-with-hc-05/
基本上,在连接之前,您需要提供HC-05模块的密码(1234)。
https://stackoverflow.com/questions/28114927
复制相似问题