2

I am working on an app that shares information peer to peer. Both devices act as central and peripheral simultaneously.

when iOS device A connects to iOS device B, they exchange information and then the centrals disconnect , and start scanning, again . if a device acquires any new information, i would like to update the a second service UUID in the CBAdvertisementDataServiceUUIDsKey with a new serial date(and deviceID) reflecting the update time. In this way other iOS devices can tell from the advertisement whether they should connect or not. The code to update the broadcast is below. The UUIDs it produces are valid, but once I revise the advertisement, my centrals no longer find the peripheral. i know that the advertisement is cached, but I had assumed that removing the service and re-adding it, I could get around this.

-(void)updateBroadcast{
    [self.peripheralManager stopAdvertising];
    if (self.pmIsConnected) {
        NSLog(@"PM connected->Can't update advertising");
        dispatch_async(dispatch_get_main_queue(), ^{
            [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateBroadcast) userInfo:nil repeats:YES];
        });
        return;
    }else{
        [self.peripheralManager removeService:self.infoService];
        [self setupInfoService];
        [self startBroadcasting];

        NSArray *services=@[self.tNetService.UUID,[self createAdvertiseUUID];
        NSDictionary *advertisingDict = 
              @{CBAdvertisementDataLocalNameKey:self.deviceNameKey,
                CBAdvertisementDataServiceUUIDsKey:services};
        [self.peripheralManager startAdvertising:advertisingDict];
    }

}

Is it possible to update this key? Or have I made some other mistake?

1
  • can you post your connecting code as well?
    – bluefloyd8
    Mar 31, 2014 at 1:11

1 Answer 1

1

Without trying to reproduce your code I'd have a few observations

  • don't mess around too much with the service database as the stack will get corrupt quite easily
  • adding and removing the services are asynchronous operations, you should wait for the callbacks to confirm your operations
  • before modifying the advertisement data use stopAdvertising (though this may not be absolutely necessary if it works for you)
  • try setting the CBCentralManagerScanOptionAllowDuplicatesKey option to @YES when you start scanning and see whether you notice the update in the advertisement

The advertisement and the hosted services need not necessarily be the same. They can be completely different actually. So updating the service DB is not a requirement to change the advertisement.

Your use case is not completely clear for me but I feel like you want to implement a multipeer chat or something similar. There was a question on this earlier where I explained that implementing broadcasting by advertisement is not a particularly good idea. If the advertisement changes relatively infrequently, then it may work but I wouldn't base my solution on this approach. The Multipeer Connectivity Framework may better suit your needs if you have relatively low number of peers, but an IP based solution will scale best.

6
  • I realize that this is asynchronous. I implement peripheral didAddService: and it returns both services, with no errors. I do stop advertising (first line of above method shown above) I will try the allowDuplicatesKeys, but do you think this would make a difference in this case. Finally and most importantly if your first point is correct, is this whole attempt in vain? For it to be of value, i would need to be able to update it every time it received new data. Thanks for the ideas.
    – mflac
    Jan 31, 2014 at 4:24
  • Oh, I overlooked the stop adv. call. Updated the answer to incorporate your comment.
    – allprog
    Jan 31, 2014 at 9:01
  • I stumbled on the solution to at least part of the problem. I discovered that for some reason, if I added a second CBService of ANY kind, my peripheral would not be discovered. On a whim, i removed the CBAdvertisementDataLocalNameKey, and then my central could discover my peripheral. I assume this means I was bumping up against a size limit for the advertisement, but that seems much smaller than I thought it would be.
    – mflac
    Feb 1, 2014 at 1:59
  • allprog, you indicated that I could change the advertisement without changing the service database, but if CBAdvertisementDataLocalNameKey is my only key, how would I do that? (the service UUID has to be created before publishing)? Perhaps put the info in the LocalNameKey instead? My use case is trading information between phones without using the internet.The info that I need in the advertisement is very small, just a serial date of the last update, so that other devices won't have to bother connecting if the peripheral has no new information.
    – mflac
    Feb 1, 2014 at 2:11
  • Sorry for all the self-comments, but the problem with using LocalNameKey is that it isn't broadcast in the background! So it's back to finding a safe way to update a second UUID in the CBAdvertisementDataServiceUUIDsKey. Is there really a way to change this with out affecting the Service DB?
    – mflac
    Feb 1, 2014 at 2:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.