3

I'm trying to discover a heart rate monitor with this statement:

[self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:SERVICE_HR]] options:@{@"CBCentralManagerScanOptionAllowDuplicatesKey":@NO} ];

SERVICE_HR is defined to be the heart rate service number.

As I read the documentation the option I gave means to coalesce all discoveries of the same device into a single discovery event. I'm scanning for 2.2 seconds with only one heart rate monitor available and getting 2 or 3 discoveries. Since the HR monitor advertises once per second that makes sense if every advertisement is "discovered" separately. I get the following callback 2 or 3 times:

 (void)centralManager:(CBCentralManager *)central
 didDiscoverPeripheral:(CBPeripheral *)peripheral
     advertisementData:(NSDictionary *)advertisementData
                  RSSI:(NSNumber *)RSSI

I save the data from each callback into a mutable array. Here is the array with two discoveries:

(lldb) po [RCLBTLE sharedBTLE].discoveredPeripherals
<__NSArrayM 0x17404bc70>(
<CBPeripheral: 0x1700f8180, identifier = 1DC9167F-6DB8-4216-5217-B1E8B2F3FB90, name = Polar H7 3F1DE71C, state = disconnected>,
<CBPeripheral: 0x1700f8180, identifier = 1DC9167F-6DB8-4216-5217-B1E8B2F3FB90, name = Polar H7 3F1DE71C, state = disconnected>
)

As you can see the same device was discovered twice but I think it should have been discovered only once. What am I doing wrong? Or what am I misunderstanding?

Michał Ciuba
  • 7,876
  • 2
  • 33
  • 59
RobertL
  • 14,214
  • 11
  • 38
  • 44
  • possible duplicate of [Corebluetooth central manager callback didDiscoverPeripheral twice](http://stackoverflow.com/questions/11557500/corebluetooth-central-manager-callback-diddiscoverperipheral-twice) – Michał Ciuba Jan 13 '15 at 20:47
  • Good catch! I searched but didn't find that one. The only suggested answer there was a weak signal but my signal is like -55 dB or -60 dB which should be fine. I'm rebooting the phone to check out their other suggested answer but even if that fixed it the question remains why is it happening? Is this a known iOS bug? – RobertL Jan 13 '15 at 20:55
  • I rebooted the phone, deleted the app from the phone, and "cleaned" Xcode but the same thing happens. – RobertL Jan 13 '15 at 21:00
  • 1
    It's not a bug. It's about filtering additional discovery packets when the peripheral supports active scanning. This answer explains it: http://stackoverflow.com/a/11731243/2128900, further explanation: http://lists.apple.com/archives/bluetooth-dev/2012/Apr/msg00047.html. You will receive more than one delegate callback for the same peripheral even with `CBCentralManagerScanOptionAllowDuplicatesKey` set to `NO`. You just have to check if peripheral already exists before adding it to the array. – Michał Ciuba Jan 13 '15 at 21:01

1 Answers1

2

When specifying the scan use the following code

[self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]] options: @{CBCentralManagerScanOptionAllowDuplicatesKey : @NO }];

If you specify the above key as @YES then it will detect the device when ever it is seen.

krishnanunni
  • 510
  • 7
  • 16
  • 1
    If you carefully read the original question you will see that I'm using exactly the code you suggest but I'm getting multiple callbacks. – RobertL Jan 24 '15 at 17:49
  • But that is the code used to get single/multiple detection. Try calling stopScan for centralmanager once the peripheral is discovered and see whether it is still calling multiple times – krishnanunni Jan 28 '15 at 05:53