r/iOSProgramming • • 4d ago

Question How do some apps access battery details of AirPods

Title basically says everything.

I could only find a private but not public API to access battery details of AirPods in my own App.

However update submission using private APIs will be refused by Apple due to guidance 2.5.1

Does anyone know how other Apps in AppStore do it or do they maybe just use private and are lucky in review?

5 Upvotes

10 comments sorted by

1

u/Sea_Wrongdoer_3500 4d ago

pretty sure most of them dont use any private api. airpods constantly broadcast ble advertisements (apple continuity "proximity pairing" messages) and battery + charging state is in there unencrypted. you just scan with CBCentralManager and parse the manufacturer data (apple company id 0x004C)

few gotchas tho. its only ~10% steps, not exact. you pick up every airpods nearby so you need some logic to figure out which ones are actually yours (rssi, model etc). and background scanning without service uuids is pretty limited so its mostly a foreground thing

format is reverse engineered and apple can change it whenever, but at least it wont get you rejected under 2.5.1

1

u/Ottiro2000 4d ago

That’s actually what I tried as well, however could not find/identify my AirPods reliable.

And tbh do not think that this is the way the other apps are doing it since they show it accurately in 1% steps just as apple does it

1

u/Sea_Wrongdoer_3500 3d ago

which apps do you mean? if its mac apps they can just read it from the system since the airpods are connected to that mac. on ios i havent seen a public way to get 1% from a 3rd party app, curious how they do it

1

u/DimensionMindless336 4d ago

The "CoreBluetooth won't help" answer isn't quite right. You don't connect to the AirPods as a BLE peripheral -- you just scan their advertisements, which is public API and totally review-safe. AirPods broadcast an Apple "proximity pairing" message (company id 0x004C, so the first two bytes read 4C 00) with the battery level sitting in it unencrypted. CBCentralManager + scanForPeripherals + CBAdvertisementDataManufacturerDataKey is all you need.

The layout of that 0x07 message, since the other reply didn't spell it out: bytes 3-4 are the model id (0x0E20 = AirPods Pro, 0x1420 = Pro 2), byte 6 packs left and right bud battery into two nibbles, byte 7's upper nibble is the case and the lower nibble is charging flags. The encoding is quirky -- 0x0 to 0x9 means 0-90% in 10% steps, 0xA through 0xE all mean 100%, and 0xF means unknown/disconnected. So round to the nearest 10% in the UI, don't present it as exact.

Gotchas that actually bit me:

  • scanForPeripherals needs options [.allowDuplicatesKey: true], otherwise CoreBluetooth gives you one callback per device per scan and you never see the battery level change.
  • Telling "yours" apart is the real problem, and it's worse than just RSSI: Apple rotates the BLE address, so there's no stable identifier in the advert to match against your paired device. Byte 10 is a connection-state field (0x04 idle, 0x05 music) which is the best public hint that it's the pair hooked up to this phone. Fine when there's one pair in the room; in a crowded cafe it's a guess.
  • Background scanning won't work: CoreBluetooth only wakes you in the background if you filter scanForPeripherals(withServices:) by a service UUID, and proximity pairing is manufacturer-data only with no such UUID. Treat it as foreground-only.

The system Batteries widget gets exact levels over the classic Bluetooth hands-free link via a private framework -- that's the part you can't touch without risking 2.5.1. The advert route is lower fidelity but legit.

1

u/Needle_In_Red 4d ago

pretty sure its the standard bluetooth battery service but airpods are a bit special, so some apps probaly use private stuff. not 100% tho

1

u/Dogukanko 3d ago

The 1% ones are almost certainly not using public APIs, since nothing public exposes that. Some probably just got through review because it's hard to spot. If you want to stay safe, the BLE advertisement route is the only legit option, and for picking your own pair you can filter by model and strongest RSSI while the case is open near the phone. Showing it as an approximate level is honest and won't risk your account.

1

u/Dogukanko 3d ago

The 1% ones are almost certainly not using public APIs, since nothing public exposes that. Some probably just got through review because it's hard to spot. If you want to stay safe, the BLE advertisement route is the only legit option, and for picking your own pair you can filter by model and strongest RSSI while the case is open near the phone. Showing it as an approximate level is honest and won't risk your account.

2

u/[deleted] 4d ago

[removed] — view removed comment

1

u/Ottiro2000 4d ago

I also guess they are using the private API but I’m wondering why Apple accepts it. F.e. the App „Battery Life“ is out there for over three years with almost 30k rating in Germany. No way they did not noticed it