raspberry-pi:如何配置hc-05

时间:2015-01-23 17:00:15

标签: bluetooth raspberry-pi

我想将我的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?

谢谢

1 个答案:

答案 0 :(得分:0)

它应该可以工作,但它可能需要HC-05模块的密码。 https://myraspberryandme.wordpress.com/2013/11/20/bluetooth-serial-communication-with-hc-05/

基本上,您需要在连接之前提供HC-05模块的密码(1234)。