通过MacOSX上的命令行连接到蓝牙设备(iPhone)

Afs*_*hin 9 iphone macos shell bluetooth

我正试图找出一种通过蓝牙与shell脚本连接到我的iPhone的方法.我目前正在使用一个基本上通过UIElements实现的AppleScript,但我想知道这是否可以通过命令行实用程序完成,ala blueutil但是能够连接到设备而不仅仅是打开/关闭蓝牙?谢谢你的考虑.

-Afshin

And*_*rns 9

这个答案与@ Wolph的答案非常相似; 然而,在与其他问题作斗争之后,这就是我想出来的.我更喜欢它有两个原因:#如果它已经连接,它将不会断开设备#Nisout的输出osascript

只需将其保存在文件中并调用即可 osascript path/to/file.applescript

这是在2014年4月11日的OSX Mavericks 10.9.2上工作,尽管您可能需要在安全性首选项面板中授予对用于运行此操作的任何方法的访问权限.请参阅此Apple KB:http://support.apple.com/kb/HT5914

您所要做的就是更改"LG HBS730"字符串以匹配您设备的名称,您应该进行设置.返回是那里,所以你得到了很好的输出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
Run Code Online (Sandbox Code Playgroud)


Dou*_*gal 6

我不得不改变一点,让安德烈·伯恩斯的回答在约塞米蒂为我工作; 他的代码一直在给我

connect-mouse.scpt:509:514:执行错误:系统事件出错:无法获取应用程序"SystemUIServer"的菜单栏1的菜单栏项目3的菜单1.索引无效.(-1719)

看起来像选择菜单栏项目,这样可以让你点击它,但没有进入菜单,因为一些难以理解的AppleScript原因.这个非常相似的代码适合我:

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
  tell (first menu item whose title is "The Device Name") of menu of bt
    click
    tell menu 1
      if exists menu item "Connect"
        click menu item "Connect"
        return "Connecting..."
      else
        click bt  -- close main dropdown to clean up after ourselves
        return "No connect button; is it already connected?"
      end if
    end tell
  end tell
end tell
Run Code Online (Sandbox Code Playgroud)

我不知道之间的区别(first menu bar item whose description is "bluetooth") of menu bar 1(menu bar item 1 of menu bar 1 where description is "bluetooth"),但是....


小智 5

根据Andrew Burnsdougal提供的答案,针对 High Sierra 10.13.2 更新此内容

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
Run Code Online (Sandbox Code Playgroud)

变化:

  • 单击蓝牙图标不再关闭菜单。通过按 Escape 键解决,根据此答案并在此页面上确认
  • 检查设备是否列在蓝牙菜单中,以检测不正确的名称。(我花了很长时间调试,才意识到'不一样......)

希望能帮到别人,不要去偷业!


cod*_*ody 0

据我所知,您只能打开/关闭蓝牙并在命令行中检查状态(使用blueutil或使用 进行操作launchctl)。osascript但是您可以通过shell使用您的 applescript 例程。