Glenn De Vos
Glenn De Vos

Reputation: 43

Why can't a specific BLE peripheral service be discovered?

I'm scanning with following code:

self.centralManager?.scanForPeripherals(withServices: serviceUUID, options: nil)

This works just fine (my tableview gets populated with BLE devices) if I either replace serviceUUID with nil or define it as

let serviceUUID = [CBUUID(string:"1803")]

But it will not work with

let serviceUUID = [CBUUID(string:"00001803-494c-4f47-4943-544543480000")]

Doublechecked with Lightblue, but the service UUID doesn't seem to have any typos. Lightblue screenshot

Upvotes: 4

Views: 1980

Answers (2)

Thern
Thern

Reputation: 1059

I think Larme has already given the appropriate solution, but I will formulate it as an answer instead of a comment because the comment was probably overlooked.

You scan for peripherals that expose a specific service in their advertisement data. That means that the whole string 00001803-494c-4f47-4943-544543480000 has to be part of the advertisement data. If there is no device found, but if it works for 1803, it seems that only 1803 is broadcasted by the devices as service information.

Please check the advertisement data of the devices. If they only expose the 16-bit UUID, you have found the reason for the problem.

Remember that advertisement data is limited to a length of 31 bytes, and this is further reduced by the length and data type declarations for each item. So it is quite common to only broadcast 16-bit UUIDs and not waste a large part of the payload for a 128-bit UUID.

Upvotes: 2

mharper
mharper

Reputation: 3272

I believe the 128-bit UUID version of 0x1803 would be 00001803-0000-1000-8000-00805F9B34FB. Is Light Blue showing you this value under "Advertisement Data (Service UUIDs)" or is it showing you 00001803-494c-4f47-4943-544543480000? Because regardless of the actual service UUID that the device responds to via GATT, scanForPeripherals is only going to return devices with the specified service UUID in the advertisement data.

Upvotes: 0

Related Questions