python - bluetooth connection using pybluez

07
2014-07
  • srj0408

    I am working on bluetooth not exactly on bluetooth stack-development but to use bluetooth in one of my project. I had done all that before using some of the py-bluez commands like hciconfig, hcitool scan , then simple-agents and using serial module inside python. But that was quite random. We were able to connect only one specific device based on its bluetooth address and there was no facility of reconnection once the devices are disconnected.

    Now i want to try out this stuff in a sequential manner like this (i am doing that all on a RPI and for at present on ubuntu 12.04.)

    i) Store some names in a file along with some other information with respect to that device. ii) Run a script to find out the device in locality with those names and if any one if found, report that. For this step, i had taken a reference from BTBook , made available from MIT. Below is the script for the same, but that script only search for the single name.

    from bluetooth import *
    
    target_name = "XT1033"
    target_address = None
    nearby_devices = discover_devices()
    
    for address in nearby_devices:
        if target_name == lookup_name( address ):
                target_address = address
        break
    
    if target_address is not None:
        print "found target bluetooth device with address ", target_address
            connect_socket(target_address);
    
    else:
        print "could not find target bluetooth device nearby"
    

    iii) Connect the device using client sock. But i dont have any device on which i can write a simple python script. My client can be any device that will be publishing data.

    Now i came through a script in the same book, that actually connect to a client requesting permission to connect to server.

    from bluetooth import *
    port = 1
    server_sock=BluetoothSocket( RFCOMM )
    server_sock.bind(("",port))
    server_sock.listen(1)
    client_sock, client_info = server_sock.accept()
    print "Accepted connection from ", client_info
    data = client_sock.recv(1024)
    print "received [%s]" % data
    client_sock.close()
    server_sock.close()
    

    here client_sock, client_info = server_sock.accept() provide the client address and port requested to be connected. Can i pass address obtained from the earlier script to this, so that it connect server to the client?

    iv) Then if client get disconnected, re-connect(a simple polling can be used.)

    All this stuff can be done using bash and py-bluez functions but i want to do that in a sequential manner.I am not a master in python but i can do some small stuff. Can any one guide me for the same or can direct me to more usefull resource through which i can continue my coding part after finding the "X", "Y" named devices.

  • Answers
    Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

    Related Question

    How to connect bluetooth headset via command line on ubuntu 12.04
  • Questioner

    I want to connect my computer to bluetooth headset. My computer has ubuntu 12.04 64 bits server operating system with no gui. So I must connect my computer via command line. But at internet documentation has no working example.

    I have used hciconfig, hcitool, btsco e.t.c and I was not able to success.

    My command line output is:

    hciconfig -a

    hci0: Type: BR/EDR Bus: USB

    BD Address: 00:15:83:2E:2F:70  ACL MTU: 384:8  SCO MTU: 64:8
    UP RUNNING
    RX bytes:2309 acl:0 sco:0 events:118 errors:0
    TX bytes:299 acl:0 sco:0 commands:48 errors:0
    Features: 0xff 0xff 0x8f 0xfe 0x9b 0xf9 0x00 0x80
    Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3 
    Link policy: RSWITCH HOLD SNIFF PARK 
    Link mode: SLAVE ACCEPT 
    Name: 'EDRClassone'
    Class: 0x280404
    Service Classes: Capturing, Audio
    Device Class: Audio/Video, Device conforms to the Headset profile
    HCI Version: 2.0 (0x3)  Revision: 0xc5c
    LMP Version: 2.0 (0x3)  Subversion: 0xc5c
    Manufacturer: Cambridge Silicon Radio (10)
    

    hcitool scan

    Scanning ... 00:11:67:00:25:02 BH025C

    hcitool cc 00:11:67:00:25:02

    Can't create connection: Connection timed out

    I have bluetooth deamond at /etc/init.d/bluetooth and just work fine.

    How to connect via command line?


  • Related Answers
  • Ferit Cubukcuoglu

    here is an step-by-step instruction! Bluetooth Setup via commandline

    and for your connection timed out, do you reset the adapter?? if not you can do this with this command. sudo hciconfig hci0 reset

    I recommend you to read the instructions in the given link.

    And to use it in commandline, here are some commands Some Bluetooth send/get commands