Find the answer to your Linux question:
Results 1 to 5 of 5
I have a Raspberry Pi running Raspian Wheezy, and I have it set up as a Bluetooth A2DP reciever. The audio streaming and everything is working wonderfully, but I'd like ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    unc
    unc is offline
    Just Joined!
    Join Date
    Dec 2012
    Posts
    3

    Bluetooth Auto Connect


    I have a Raspberry Pi running Raspian Wheezy, and I have it set up as a Bluetooth A2DP reciever. The audio streaming and everything is working wonderfully, but I'd like to make it so that the Pi tries to connect to my phone automatically every 10 seconds or so, and when it does connect, I need it to execute one command. Does anyone have any idea how to do this? I think a cron job could do it, but I have no idea where to start.

  2. #2
    Linux Guru
    Join Date
    Oct 2007
    Location
    Tucson AZ
    Posts
    3,190
    I'd like to make it so that the Pi tries to connect to my phone automatically every 10 seconds
    If you know how to do that from a terminal, just put it in a script and make the script executable. Then create a cron job with a full path to the script. It might work if you had an if/else in your script, if it connects the command is executed, if it doesn't, exit the script. I'm not familiar with a Raspberry Pi or Bluetooth so can't give you any more detailed info.

  3. #3
    unc
    unc is offline
    Just Joined!
    Join Date
    Dec 2012
    Posts
    3
    I don't think it should be any different because its a pi. But unfortunately I don't know how to do that from terminal, I've been doing it through a GUI. I also don't known how to create a cron job
    I hadn't thought about using the if else like you said though, that's an interesting idea.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Feb 2006
    Location
    Corvallis, Oregon
    Posts
    8
    I am familiar with the RPi and BT but I am not sitting at home right now where the RPi lives.
    Not much time to play with mine... sure is a cute little computer though.

    You should be able to have the RPi scan bluetooth periodically, and notice what bluetooth things it sees.
    Then you should be able to make it respond when it sees your phone. Do you just want it to do something when it sees your phone or do you want it to pair with the phone?

    I am curious what the one command you will run is, or more to the point, just what you are trying to do?

    I have had an idea for some time now of tracking who is in the office by watching our WiFi network. My Linux server assigns the IP addresses via DHCP, so it knows who is out there. I can do this to see what it's assigned IP addresses to recently:

    grep client-hostname /var/lib/dhcp/dhcpd.leases | sort -u
    client-hostname "Alinas-iPhone";
    client-hostname "android-138dfd22b3f2bdff";
    client-hostname "Android_357814041325682";
    client-hostname "android_687e6cf74eecdb04";
    client-hostname "android-995bef71b52cf453";
    client-hostname "android-a1daf2ef7d1e3610";
    client-hostname "android-ceb79433bffe7414";
    client-hostname "Champoeg";
    client-hostname "LTHP0311N6S";
    client-hostname "LTHP952034W";
    client-hostname "LTHP952037C";
    client-hostname "MCKENZIE";
    client-hostname "touchet";
    client-hostname "umatilla";

    You can see several Androids and one iPhone in there. But I digress...

    To search for bluetooth things check out the hcitool program -- for example

    hcitool scan

    Once you have the addresses you can use "hcitool info" and the address to get more details. Once you know your phones address you'd just code that into the script. THere are about 30 other command options for hcitool I have not tried yet.

    No output because it's not doing anything interesting here at work. Usually it tells me what it sees. Maybe there are not any things to see here. Try it yourself.

    Your script should just run all the time and sleep instead of running it periodically from cron. Cron is designed for running jobs at long intervals like once an hour or once a month, not every 10 seconds. Start the script at boot (for example from /etc/rc.local) if you want so you don't have to log in to the Pi and start it. So for example

    loop
    get list of bluetooth devices in range with "hcitool scan"
    look for our phone in the list
    if we found our phone, do something
    sleep for 10 seconds
    go back to start of loop

    Personally I'd write this in Python because it is currently my favorite language for scripting.
    Please tell me what you really want to do and I will give you more help.

    Brian

  6. #5
    unc
    unc is offline
    Just Joined!
    Join Date
    Dec 2012
    Posts
    3
    The command I'm running is going to set pulseaudio to play the Bluetooth audio through the headphone jack.
    The thing is, I'm not sure what command to use to actually connect to my phone via a2dp. I also don't know the syntax of the script to run, so not much to offer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •