Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to bluetooth device (iPhone) via command line on MacOSX

I'm trying to figure out a way to connect to my iPhone via Bluetooth with a shell script. I'm currently using an applescript which essentially does this through UIElements, but I'm wondering if this can be done with a command line utility, a.l.a. blueutil but with the ability to connect to the device not just turn on/off bluetooth? Thanks for the consideration.

-Afshin

like image 580
Afshin Avatar asked Jul 08 '13 05:07

Afshin


Video Answer


2 Answers

This answer is very similar to @Wolph's answer; however after fighting other issues this is what I came up with. I like it better for two reasons: # It will not disconnect the device if it is already connected # Nice output from osascript

Just save it in a file and call osascript path/to/file.applescript

This was working on OSX Mavericks 10.9.2 as of 11-Apr-2014, although you may need to grant access to whatever method you use to run this in the security preferences panel. See this Apple KB: http://support.apple.com/kb/HT5914

All you should have to do is change the "LG HBS730" string to match the name of your device and you should be set. The returns are there so you get nice output from osascript.

activate application "SystemUIServer"
tell application "System Events"
  tell process "SystemUIServer"
    -- Working CONNECT Script.  Goes through the following:
    -- Clicks on Bluetooth Menu (OSX Top Menu Bar)
    --    => Clicks on LG HBS730 Item
    --      => Clicks on Connect Item
    set btMenu to (menu bar item 1 of menu bar 1 where description is "bluetooth")
    tell btMenu
      click
      tell (menu item "LG HBS730" of menu 1)
        click
        if exists menu item "Connect" of menu 1
          click menu item "Connect" of menu 1
          return "Connecting..."
        else
          click btMenu -- Close main BT drop down if Connect wasn't present
          return "Connect menu was not found, are you already connected?"
        end if
      end tell
    end tell
  end tell
end tell
like image 114
Andrew Burns Avatar answered Oct 25 '22 01:10

Andrew Burns


Updating this for High Sierra 10.13.2, based on the answers provided by Andrew Burns and dougal.

set DeviceName to "LG HBS730"

tell application "System Events" to tell process "SystemUIServer"
    set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
    click bt
    if exists menu item DeviceName of menu of bt then
        tell (first menu item whose title is DeviceName) of menu of bt
            click
            tell menu 1
                if exists menu item "Connect" then
                    click menu item "Connect"
                    return "Connecting..."
                else
                    key code 53 -- hit Escape to close BT menu
                    return "No connect button; is it already connected?"
                end if
            end tell
        end tell
    else
        key code 53 -- hit Escape to close BT menu
        return "Cannot find that device, check the name"
    end if
end tell

Changes:

  • Clicking on the Bluetooth icon no longer closes the menu. Solved by hitting the Escape key, per this answer and confirmed on this page
  • Checking to see if the device is listed in Bluetooth menu, to detect incorrect names. (I spent quite a while debugging, only to realize ' isn't the same as ...)

Hope this helps others, not trying to steal karma!

like image 27
Greg Schwartz Avatar answered Oct 25 '22 01:10

Greg Schwartz