RobertL
RobertL

Reputation: 14884

Why does iOS bluetooth discover the same device more than once?

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?

Upvotes: 3

Views: 3276

Answers (1)

krishnanunni
krishnanunni

Reputation: 510

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.

Upvotes: 2

Related Questions