Skip to content

Commit

Permalink
fix grabbing multiple MAC addresses in some cases when AirPods is not…
Browse files Browse the repository at this point in the history
… connected
  • Loading branch information
godbout committed Mar 25, 2022
1 parent d635c09 commit 5529cd7
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/battery.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SYSTEM_PROFILER=$(system_profiler SPBluetoothDataType 2>/dev/null)
MAC_ADDRESS=$(grep -B8 "Minor Type: Headphones" <<< "${SYSTEM_PROFILER}" | awk '/Address/{print $2}')
MAC_ADDRESS=$(grep -B8 "Minor Type: Headphones" <<< "${SYSTEM_PROFILER}" | awk '/Address/{ lastline = $2 } END { print lastline }')
CONNECTED=$(grep -A10 "${MAC_ADDRESS}" <<< "${SYSTEM_PROFILER}" | awk '/Services:/{print 1}')

if [[ "${CONNECTED}" ]]; then
Expand Down

8 comments on commit 5529cd7

@zjn0505
Copy link

@zjn0505 zjn0505 commented on 5529cd7 Oct 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With macOS 12.6, I still need to use the original version.

@godbout
Copy link
Owner Author

@godbout godbout commented on 5529cd7 Oct 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no idea what you mean.

@zjn0505
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my output of SYSTEM_PROFILER

Bluetooth:

      Bluetooth Controller:
          Address: AA:AA:AA:AA:AA:AA
          State: On
          Chipset: BCM_4364B3
          Discoverable: Off
          Firmware Version: v86 c4205
          Product ID: 0x0001
          Supported services: 0x382039 < HFP AVRCP A2DP HID Braille AACP GATT Serial >
          Transport: UART
          Vendor ID: 0x004C (Apple)
      Connected:
          My AirPods3:
              Address: BB:BB:BB:BB:BB:BB
              Vendor ID: 0x004C
              Product ID: 0x2013
              Case Battery Level: 98%
              Left Battery Level: 100%
              Right Battery Level: 100%
              Firmware Version: 4E71
              Minor Type: Headphones
              Serial Number: BBBBBBBBBB
              Services: 0x980019 < HFP AVRCP A2DP AACP GATT ACL >
      Not Connected:
          My AirPods Pro:
              Address: CC:CC:CC:CC:CC:CC
              Vendor ID: 0x004C
              Product ID: 0x200E
              Firmware Version: 2D27
              Minor Type: Headphones
              Serial Number: CCCCCCCCCCCC
          My MacBook Pro:
              Address: 11:11:11:11:11:11
          My AirPods:
              Address: DD:DD:DD:DD:DD:DD
              Vendor ID: 0x004C
              Product ID: 0x2002
              Firmware Version: 6.8.8
              Minor Type: Headphones
              Serial Number: DDDDDDDDDDDD
          My Beats Studio³:
              Address: EE:EE:EE:EE:EE:EE
              Vendor ID: 0x004C
              Product ID: 0x2009
              Firmware Version: 0.0.1
              Minor Type: Headphones
          My AirPods Pro:
              Address: FF:FF:FF:FF:FF:FF
              Vendor ID: 0x004C
              Product ID: 0x200E
              Firmware Version: 4C165
              Minor Type: Headphones
              Serial Number: FFFFFFFFFFFF
          MAJOR III BLUETOOTH:
              Address: 00:00:00:00:00:00
              Minor Type: Headset

MAC_ADDRESS would be FF:FF:FF:FF:FF:FF, the one that is not connected.

@godbout
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. so when you say:

With macOS 12.6, I still need to use the original version.

that means the latest version doesn't work? of this workflow? by original you mean the one from which this workflow is forked? the original one works?

@zjn0505
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have been using your workflow, not a fork.

The latest version with this change of

awk '/Address/{print $2}' -> awk '/Address/{ lastline = $2 } END { print lastline } will grab the last not connected headphones' MAC address.

Before this commit, it will print all headphones MAC address, which can be fed to the following commands to get the connect AirPods.

@godbout
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh ok, i think i got it. i ported an improvement that someone made in the alfred airpodspro connector: godbout/alfred-airpodspro-connector#3

personally i don't use multiple headphones so i'm not sure exactly how it works again. if i revert for this workflow, that would help your case? you're still gonna deal with multiple MAC addresses, and if i reckon the Workflow will not handle this well.

@zjn0505
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If only one is connected, it turns out to be o.k. fo MAC_ADDRESS to have multiple addresses including not connected ones

awk '/Case Battery Level/{print $4} will only grab from the connected one and ignore unconnected MAC addresses.

I am experimenting with multiple connected devices. I know very few bash scripts, will try to make it pretty and create a PR later.

@godbout
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know very few bash scripts, will try to make it pretty and create a PR later.

lol. the PR is so nice 😂️

Please sign in to comment.