I am working on a Bluetooth LE application for iOS. I am using the Core Bluetooth framework within iOS to handle all communications.
Question & Description:
When I use a single tag, despite numerous connections and disconnections, the single tag connects seamlessly and the phone discovers it services.
Also, when multiple Bluetooth LE tags connect for the first time, they connect seamlessly and the phone discovers their services.
When the tags get disconnected and then reconnect to the phone, the tags connect fine. But one of the two tags (either one) does not seem to advertise its services. i.e when the app is open and the tag reconnects, the DiscoverServices method does not call the didDiscoverServices delegate.
Why is this happening only when connection with multiple devices takes place.
I have set the peripheral.delegate correctly. I have tried everything, including doing repeated re-connect, repeated DiscoverServices calls to the tag. Nothing seems to work.
How can I re-connect to multiple tags to the phone and still discover all services.
Please help
Thanks,
Manju
Turns out that there was command I was issuing to the device in the"didDiscovercharacteristicsForService" delegate method which was causing the connection instability.
If you are facing similar issues, I suggest you to let the delegate method complete without any intervention (of any kind) and pass the CBPeripheral to another function designed by you to pass any values / issue a command to the devices.
Thanks anyway Wilhemsen.
So the steps are as follows..
1> Search for Tag,
2> If in range, CONNECT to Tag
3> If Connected, call DISCOVER services method (do not interrupt)
4> IN DidDiscoverServices, call DISCOVER Characteristics Method
..
In DidDiscoverCharacteristics Method, wait until all Characteristics are discovered..
Then , at the end, call a function in your code that will do the necessary setup..
...
Code Sample
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
for (int i=0; i < peripheral.services.count; i++) {
CBService *s = [peripheral.services objectAtIndex:i];
printf("Fetching characteristics for service with UUID : %s\r\n",[self CBUUIDToString:s.UUID]);
[peripheral discoverCharacteristics:nil forService:s];
}
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
if (!error)
{
CBService *s = [peripheral.services objectAtIndex:(peripheral.services.count - 1)];
if([self compareCBUUID:service.UUID UUID2:s.UUID])
{
// This is when the **Tag** is completely connected to the Phone and is in a mode where it can accept Instructions from Device well.
printf("Finished discovering characteristics");
// Now Do the Setup of the Tag
[self setupPeripheralForUse:peripheral];
}
}
else
{
printf("Characteristic discorvery unsuccessfull !\r\n");
}
}
-(void) setupPeripheralForUse:(CBPeripheral *)peripheral
{
// DO all your setup in this function. Separate Perpiheral Setup from the process that synchronizes the tag and the phone.
}