scanForPeripheralsWithServices on iOS not finding devices by service UUID - ios

I am using scanForPeripheralsWithServices to scan for devices by service UUID. When I use some of the standard service UUID values it will work sometimes.
When I try to use custom service UUIDs it often won't find any devices, but if I use nil for the service UUID list it will find those same devices that it doesn't find when I use their service UUID.
What is going on? Could it be because the advertising packet for the devices can only advertise some of its services?

The set of services advertised by a peripheral is typically smaller than the services it offers. This is because there is limited space in the advertisement so it makes sense to advertise only the "primary" service.
From the Apple Core Bluetooth Programming Guide -
There are also limits as to how much space you can use when advertising
data. When your app is in the foreground, it can use up to 28 bytes of
space in the initial advertisement data for any combination of the two
supported advertising data keys. If this space is used up, there are
an additional 10 bytes of space in the scan response that can be used
only for the local name. Any service UUIDs that do not fit in the
allotted space are added to a special “overflow” area; they can be
discovered only by an iOS device that is explicitly scanning for them.
While your app is in the background, the local name is not advertised
and all service UUIDs are place in the overflow area.
To help you stay within these space constraints, limit the service
UUIDs you advertise to those that identify your primary services.
As an example, a Heart rate device may offer the heart rate service, the device information service, the battery service and more, but it will only advertise the heart rate service as this is service that other devices will be "interested in" as it distinguishes this device from other classes of device.
Once the device has been identified and a connection established the other services can be discovered if required.

Related

Advertise ANCS Service using Adafruit Feather 32u4 using AT Commands

I'm trying to implement the Apple Notification Center Service using an Adafruit Feather 32u4 Bluefruit LE to be able to receive notifications from my iPhone to the Feather board, sending them to my PC over Serial connection and dealing with them with some software that I wrote.
I set up the ANCS service and characteristics using the AT+GATTADDSERVICE and AT+GATTADDCHAR commands but when I try to make the device discoverable and to advertise the use of the ANCS service the device won't become discoverable.
This is the command that I use:
AT+GAPSETADVDATA=02-01-06-11-06-79-05-F4-31-B5-CE-4E-99-A4-0F-4B-1E-12-2D-00-D0
Where:
0x02: is the byte length of the first message in the payload
0x01: is the 'Data Type Value' and indicates that this is a 'Flag'
0x06: indicates the flag LE General Discoverable Mode without BR/EDR support
0x11: is the byte length of the second message (0x11 is 17 bytes in decimal, 1 for the type of "message "Incomplete List of 128-bit Service Class UUIDs", 16 bytes for the UUID of ANCS)
0x06: is the 'Data Type Value' for "Incomplete List of 128-bit Service Class UUIDs"
0x7905F431B5CE4E99A40F4B1E122D00D0: is the UUID for ANCS.
When I use the above command though, the device won't become discoverable. Do you know why?
While trying to troubleshoot the problem I noticed that if I try to advertise different services like an heart rate service and a device information service the device does become visible from other devices. This is the command that I used:
# Advertise as Discoverable and BLE only with 16-bit UUIDs 0x180D and 0x180A
AT+GAPSETADVDATA=02-01-06-05-02-0d-18-0a-18
Do you know why the device won't become visible when trying to advertise the ANCS service?
Thank you!
P.S. I'm new to the bluetooth protocol, sorry.
Have a look at the specification of the ANCS you provided. It states:
Only one instance of the ANCS may be present on an NP. Due to the nature of iOS, the ANCS is not guaranteed to always be present. As a result, the NC should look for and subscribe to the Service Changed characteristic of the GATT service in order to monitor for the potential publishing and unpublishing of the ANCS at any time.
Further down you find figures that show that your iPhone is the NP and it is the one that might implement the ANCS.
Your Feather board would be the Central and is supposed to look for the service and connect to it. As #Paulw stated in the comments you have it the wrong way around.

BLE: Advertising local name and service UUID from background iOS

According to iOS documentation, when an iOS application that utilizes BLE as a peripheral moves to background mode, peripheral name is not advertised and all service UUIDs are placed in a special ‘overflow’ area, they can be discovered only by an iOS device which is explicitly scanning for them.
I sniffed the BLE packets sent over the air when application is in background. There is no local name and service UUID data. There is an 'overflow' area which encodes the service UUID. A brief discussion can be found here: https://github.com/crownstone/bluenet-ios-basic-localization/blob/master/BROADCASTING_AS_BEACON.md
I wish to know if there is any way we can determine the actual service UUID being advertised from the data in 'overflow' area. iOS documentation states that when an app is advertising as BLE peripheral in background, another iOS app can find it by explicitly specifying the service UUIDs to scan for. So, there must be a way to figure out the actual UUID from overflow data.
Any pointers on this would be helpful.
No. The data in the overflow area is hashed (sending several 128-bit UUIDs would be much too large for an advertising packet). I don't believe the hash is documented, but I strongly suspect that it's based on a Bloom filter, so that Apple can probabilistically pack a unlimited number of UUIDs into the very limited space of an advertising packet.
The upside of all of this is that it means the data isn't there in the advertising packet (and really can't be). You will need to connect to the device to discover its services.

How to scan and connect to iBeacon using BLE services in the background

I have a iBeacon device with services advertising. I need to know to how to connect to the iBeacon from the iOS app in the background mode.
I know the service UUID of the iBeacon but I am not able to scan using serviceUUID in foreground and background mode.
If I scan for nil and filter using the name, working in the foreground but not in the background.
Please help me, I am new to iOS programming. Any ideas?
Don't confuse the ProximityUUID of the iBeacon transmission with a GATT Service UUID. While both are 16 byte identifiers typically represented in the same hex format, the two have entirely different meanings. An iBeacon ProximityUUID cannot be used as a GATT Service UUID.
There is no requirement at all that a bluetooth beacon transmitting an iBeacon frame hosts any connectable GATT services. While some manufacturers do offer a GATT service in their hardware beacons for configuring its identifiers as well as other purposes, the GATT Service UUID is typically not the same as the ProximityUUID.
If you want to do what you describe you need to:
Find out of your beacon manufacturer even hosts a GATT service at all.
It yes, find out what the GATT Service UUID is. Again, it will typically not be the same as the iBeacon ProximityUUID.
If you cannot get the info from the manufacturer, you might be able to find it out by scanning in the foreground (without specifying the GATT Service UUID), then printing out the discovered GATT Service UUIDs for the device that you get by calling discoverServices on the CBPeripheral from the scan results. You may find there are no services, which would give you a no answer to the first question above.
Once you have the above info, you can scan for the beacon in the background by specifying the GATT Service UUID when starting the scan. In the background, you will not get results if you do not specify a GATT Service UUID, and even if you do results will come much more slowly.

How to send service UUID

We are developing an app which needs to scan for beacons in the background. This requires us for iOS to specify a service UUID while scanning. See Apple documentation:
Apps that have specified the bluetooth-central background mode are allowed to scan while in the background. That said, they must explicitly scan for one or more services by specifying them in the serviceUUIDs parameter. The CBCentralManagerOptionShowPowerAlertKey scan option is ignored while scanning in the background.
We are using a Raspberry Pi with a bluetooth adapter to send a beacon signal (conforming the AltBeacon spec). Unfortunately we are not able to find out how the service UUID should be set in the BLE Advertising PDU, is it part of the Bluetooth specification or part of the Manufacturer specific data structure? We did find examples for setting the service UUID for when you use an iOS device as beacon, but since we are using a generic bluetooth adapter we cannot use that.
Could anyone clarify us how and where the service UUID should be set in the beacon transmission?
is it part of the Bluetooth specification or part of the Manufacturer specific data structure?
the later.
generally when you setting the advertising parameters, you can set the UUID(or name, tx power, etc) to it.

Identify iOS peripheral advertising in background

I work on iOS application with acts as Bluetooth peripheral. I need to implement a searching for my iOS peripheral from non iOS centrals. I’m faced with a problem while my iOS application advertising in background mode. When it is advertising in foreground, my central can read primary service UUID from advertising data, but when it is advertising in background, I can’t see the name or the UUID of peripheral – there is only Apple manufacturer data in advertising packet.
The essence of the problem lies in the fact that my non iOS central can’t determinate – is it advertising my peripheral in background or any other peripheral in background. I have to connect every iOS device with advertising in background, enumerate its services to look for my service UUID.
The documentation says that during background advertising all service UUIDs go to special “overflow” area and only iOS devices can read it specifying service in CBCentralManager’s scanForPeripherialWithServices method. It looks like Apple have an ability to check background advertising packet data for service UUID.
After searching on SO I found some interesting information about service hashed UUID. Background mode advertising packet always contains Apple manufacturer data (14 FF 4c 00 01) and undocumented 128 bit value, where one of these 128 bits equals 1. I tried to change my service UUID several times and found out than this 128 bit value after Apple manufacturer data changes too.
I’d like to use this value to filter peripherals around and reduce
connections to wrong (not mine) peripherals. Can anyone give any
information how this value depends on service UUID? Is there any
hash function that is used on specified service UUID in
scanForPeripherialWithServices method?
Can I be sure that the background advertising packet will always be the same for the immutable service UUID which will never change?
Is there any other way to send my custom information while
advertising in background mode? Maybe I can add my data into BLE
SCAN RESPONSE packet?
Best Regards,
Dezmond

Resources