Roby
Roby

Reputation: 179

scanForPeripheralsWithServices:return result twice on the same peripheral

I use the below code

[centralManager scanForPeripheralsWithServices:serviceUUIDArray 
         options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] 
         forKey:CBCentralManagerScanOptionAllowDuplicatesKey]] 

to scan a perpheral device, then the result return twice on the same peripheral. How to return one? Thanks.

Upvotes: 4

Views: 1673

Answers (1)

allprog
allprog

Reputation: 16790

This is happening because the peripheral's name is updated. In this case you get the callback once more.

This thread contains a detailed explanation: http://lists.apple.com/archives/bluetooth-dev/2013/Apr/msg00099.html

Credits go to Etan Kissling:

The device name is cached by iOS according to a priority list:

  1. Name read from Device name characteristic after a connect
  2. Name from advertising packet
  3. null

Note that the name from the advertising packet should be a prefix of the final name in the Device name characteristic.

Maybe the physical device already had the name cached, and does not send the second update, as it already retrieved the name.

Your app should be able to deal with duplicates even when you don't request them.

To deal with duplicates, you can check the already received ones with the isEqualTo: method on the peripheral or use an NSSet for storing them.

Upvotes: 2

Related Questions