45

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.

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 a macOS Swift code example, AppleScript example, bash example, or pointers to good docs as answers.

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.app GUI
  • Using dtrace to see how Bluetooth Explorer.app computes the list: difficult last resort
  • The Proximity App 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.

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

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

Nick Sweeting
  • 5,364
  • 5
  • 27
  • 37

1 Answers1

54

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.

pbell
  • 2,965
  • 1
  • 16
  • 13
  • 31
    Unfortunately this doesn't give the info I need. This lists the history of paired devices, but doesn't show whether or not they're currently in range. I'm looking for an active command that scans to find nearby devices, or returns if a given device is in range. – Nick Sweeting Jun 30 '16 at 22:44
  • 2
    Fwiw, in 2022 on macOS Monterey, this command lists devices in two groups: `Connected:` and `Not Connected:` – aaaidan Sep 05 '22 at 01:55
  • That's marginally more useful but still `Connected` != `In Range`. I'm really looking for a command that shows whether devices are reachable/visibly broadcasting/within range even if they are not actively connected. – Nick Sweeting Feb 12 '23 at 07:07