cadri
cadri

Reputation: 1

Scanning for and handling reception of multiple services in CoreBluetooth Framework?

When I scan for 2 peripherals each having a single service and characteristic (with cServiceArray being an NSArray of both service CBUUIDs) with the method:

[self.centralManager scanForPeripheralsWithServices:cServiceArray options:@{CBCentralManagerScanOptionAllowDuplicatesKey : @NO }];

I receive the following console response:

2013-10-18 13:58:37.069 BlueMarco[1137:60b] didDiscoverPeripheral peripheral description <CBPeripheral: 0x14e799d0 identifier = 33C98AC7-E25E-0803-82D2-326FC8C0FDB1, Name = "ICServer_2", state = disconnected>
...
2013-10-18 13:58:37.072 BlueMarco[1137:60b] didDiscoverPeripheral Connecting to peripheral ICServer_2
...
2013-10-18 13:58:37.076 BlueMarco[1137:60b] didDiscoverPeripheral peripheral description <CBPeripheral: 0x14e75b60 identifier = E634E343-0DCF-3A4F-1F88-D4C87EA731EA, Name = "ICServer_1", state = disconnected>
...
2013-10-18 13:58:37.079 BlueMarco[1137:60b] CoreBluetooth[WARNING] <CBPeripheral: 0x14e799d0 identifier = 33C98AC7-E25E-0803-82D2-326FC8C0FDB1, Name = "ICServer_2", state = connecting> is being dealloc'ed while connecting
2013-10-18 13:58:37.080 BlueMarco[1137:60b] didDiscoverPeripheral Connecting to peripheral ICServer_1

This shows the connection to one peripheral broken as I make a connection to the second server. Is this the expected behavior? If so, why does the method scanForPeripheralsWithServices allow an NSArray of service CBUUIDs as an argument? If not, can anyone show me how to scan for and handle the reception of multiple services?

Upvotes: 0

Views: 777

Answers (1)

allprog
allprog

Reputation: 16780

The line showing CoreBluetooth[WARNING] indicates that you did not keep a strong reference to the CBPeripheral object in question and thus it is being deallocated despite the fact that a connection operation is in progress.

Make sure that as long as you use a CBPeripheral, you keep a strong reference to it or it will be automatically garbage collected by the system. This is probably not emphasized enough in the Core Bluetooth Programming Guide but is a must for correct operation.

Upvotes: 1

Related Questions