objective-c 是否有命令行界面可以列出 mac 范围内的所有蓝牙设备?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38116299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:38:11  来源:igfitidea点击:

Is there a command-line interface to list all Bluetooth devices in range on mac?

objective-cswiftmacosbluetoothapplescript

提问by Nick Sweeting

I want to list all in-range Bluetooth devices, ideally with hostnames, but MAC addresses would be sufficient. I need a command that performs an active scan so I can poll to see if my device is nearby, not one that lists previously paired devices.

我想列出所有范围内的蓝牙设备,最好使用主机名,但 MAC 地址就足够了。我需要一个执行主动扫描的命令,以便我可以轮询我的设备是否在附近,而不是列出以前配对的设备。

My use-case is detecting whether my Bluetooth headphones or iPhone are in range of my Mac, to trigger security setting changes (e.g. sudo timeout). I've done a fair bit of Googling, and the only things I seem to be able to find are iOS swift code examples. I would be happy with OS X Swift code examples, AppleScript, terminal utilities, or pointers to good docs.

我的用例是检测我的蓝牙耳机或 iPhone 是否在我的 Mac 范围内,以触发安全设置更改(例如 sudo 超时)。我已经做了相当多的谷歌搜索,我似乎唯一能找到的是 iOS swift 代码示例。我会对 OS X Swift 代码示例、AppleScript、终端实用程序或指向优秀文档的指针感到满意。

I've looked at these options so far:

到目前为止,我已经看过这些选项:

  • blued: prints a list of mac addresses but they don't seem to change when devices go in or out of range or connect, and they aren't matched up to hostnames
  • blueutil(brew install blueutil): only prints power state, not device names
  • bluetoothaudiod: no usable interface
  • This Objective-C code Create a Terminal-Based Bluetooth Monitor in XCode?: I get several errors when compiling so it's hard to test it
  • Using AppleScript to read it out the Bluetooth Explorer.appGUI
  • Using dtraceto see how Bluetooth Explorer.appcomputes the list: difficult last resort
  • The ProximityApp from this forum post: Doesn't have a command line interface, so I cant query it remotely. I can write a script that stores the state in a file to read later, but it only works for one device at a time.
  • blued: 打印一个 mac 地址列表,但当设备进入或超出范围或连接时它们似乎没有改变,并且它们与主机名不匹配
  • blueutil( brew install blueutil): 只打印电源状态,不打印设备名称
  • bluetoothaudiod: 没有可用的界面
  • 这个 Objective-C 代码在 XCode 中创建一个基于终端的蓝牙监视器?: 编译时出现几个错误,所以很难测试
  • 使用 AppleScript 在Bluetooth Explorer.appGUI 中读出它
  • 使用dtrace来看看如何Bluetooth Explorer.app计算列表:困难不得已而为之
  • 此论坛帖子中Proximity应用程序:没有命令行界面,因此我无法远程查询。我可以编写一个脚本,将状态存储在一个文件中以供稍后读取,但它一次只适用于一个设备。

Edit 2017/01: Found this SO answer with details on finding nearby devices in Swift:
List devices that are in range of Bluetooth device in Swift

编辑 2017/01:找到这个 SO 答案,详细说明如何在 Swift 中查找附近的设备:列出Swift
中蓝牙设备范围内的设备

Edit 2017/10: Found an updated SO answer for Swift 3 & 4.0: Nearby Bluetooth devices using Swift 3.0

编辑 2017/10:为 Swift 3 和 4.0 找到了更新的 SO 答案:使用 Swift 3.0 的附近蓝牙设备

回答by pbell

In Terminal, the command :

在终端中,命令:

system_profiler SPBluetoothDataType

gives you all the details about Bluetooth, including devices paired, with their names, MAC address. Result is unfortunately a bit too much, but with sed or grep command, you should extract what you need.

为您提供有关蓝牙的所有详细信息,包括配对的设备及其名称、MAC 地址。不幸的是,结果有点太多了,但是使用 sed 或 grep 命令,您应该提取您需要的内容。