3
votes

In my iOS app, When I scan for Bluetooth devices, I see Peripheral UUID and other data from each nearby device, like below:

(uuid: 58B95943-A57E-F818-D3CC-9F8122C64D88, name: "iPad Pro"), advertisementPacket: ["kCBAdvDataRxSecondaryPHY": 0, "kCBAdvDataRxPrimaryPHY": 1, "kCBAdvDataIsConnectable": 1, "kCBAdvDataTimestamp": 612132622.292843, "kCBAdvDataTxPowerLevel": 12], rssi: -37

I then wish to continue scanning for a single device with specific UUID in the background (when the app is closed) and be notified when the app is near or connectable.

Please note that many of the devices (including Apple iPad etc) do not have a UUID in the advertisement packet. They have a UUID in Peripheral info only.

I've tried several approaches:

  1. There is a Connect method where you leave a connect to request for Bluetooth device waiting in the background and callback lets you know when the device connects but this does not work for Apple AirPods which never connect.

  2. I know I can scan for devices with known Service IDs like this: scanForPeripherals(withServices serviceUUIDs: [CBUUID(string: GATT ServiceCode]?, options... but I tried every single Assignment number on Bluetooth GATT Services Specifications website, and none of them seem to find Apple Airpods or other Apple devices.

I have seen other iOS apps work in the background for devices including Apple AirPods, so there has to be a way to achieve this.

2
The serviceUUID required for a background bluetooth scan is a common 'service' identifier (for each service) advertised by all devices of that type. I think AirPods do not advertise a serviceUUID, and hence cannot be scanned for, in background mode. Can you give example of an app that is able to scan for, and detect and AirPod while in background mode? May be that will give a clue to how its happening.Swapnil Luktuke
Wait, no. Above shared app description has this: HOW IT WORKS -> 1. Open Wunderfind. .. thats not finding airpods while app is in background. Are you sure there are apps finding airpods in background? As far as i understand this is not possible.Swapnil Luktuke
You can choose “Notify When Found” in airpods detail in this app. And then close the app. Once you are near airpods it sends u an alert.Kashif
I don’t think wunderfind works like you expect (though, this could be an effect of my phone / AirPod version). I currently have my AirPods right next to me and wunderfind displays that they were last found 15 minutes ago. It’s only if you open the AirPod case that the AirPods start advertising so they can be detected.ibash

2 Answers

2
votes

You are confusing device identifiers with service identifiers. The UUID you shown for the iPad Pro is an identifier that has been assigned by Core Bluetooth on your device for that device. Every other iOS device in the world will receive a different identifier for that iPad.

Devices can advertise well known GATT services, such as battery information, device information or "private" services. These services are also identified by a UUID. These are the UUIDs that you scan for. E.g. your app could scan for devices advertising a "heart rate monitor" service.

To see specific services in the advertisement you need an app running in the device acting as a peripheral and advertising a service.

If your intention is to understand when this particular device is nearby then the approach would be to try and maintain a connection to it.

If you connect to the device then you will get a delegate callback when it disconnects (e.g. it goes out of range). You can then immediately issue another connect. This connection will be pending until the device is back in range. iOS will then reconnect and you will get another delegate callback.

2
votes

You should be able to detect the AirPods via the AVAudioSession route change notification.

In particular configure your audio session category to playAndRecord and set the option to allowBluetoothA2DP.

Edit: I did a scan of bluetooth packets when you open / close the AirPods (which is when wunderfind locates them). It shows a handsfree audio gateway, so if you haven't yet tried using the AVAudioSession to see if you get a newDeviceAvailable notification that could be how they're doing detection.

enter image description here