When using scanForPeripheralsWithServices:options, I'm able to discover a service when using
// Scanning with nil services will return all devices.
NSLog(@"Looking for any service.");
[self.centralManager scanForPeripheralsWithServices:nil options:nil];
however when specifying the service in advance using the service identifier obtained from the Bluetooth device via the code below, I'm unable to discover the device.
#define DEVICE_INFO_SERVICE_UUID @"180a"
NSArray *services = [NSArray arrayWithObjects:[CBUUID UUIDWithString:DEVICE_INFO_SERVICE_UUID], nil];
[self.centralManager scanForPeripheralsWithServices:services options:nil];
What's wrong?
[-CBCentralManager scanForPeripheralsWithServices:options:] only returns peripherals that actively advertise that they support a certain service.
If the service you are looking for is not advertised by the peripheral, you need to connect to it, and then discover the service you wish to access.
As space in the advertisement packets (that are processed when scanning) is limited, typically only the major service is advertised. Device Information Service is typically available on most BLE peripherals, and, therefore, is normally not advertised. However, you will see this service once connected to the peripheral with [-CBPeripheral discoverServices:].
As a side note, just a minor Objective-C reminder:
You can use initializer syntax to reduce the verbosity of your code:
[self.centralManager
scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:DEVICE_INFO_SERVICE_UUID]]
options:nil];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With