3

I'd like to use the Multipeer Connectivity functionality for my app. Brief intro to the functionality of the app: The app should scan for other devices running the app (in background), connect to them and transfer a bit of data. All without interaction with the user.

Question: is it possible to connect to other devices using multipeer but without having to show the alert view that another device wants to connect and forcing the user to accept or decline the connection? Is there a way who I can programmatically accept all incoming connections from other devices? If so, how?

Thanks a lot in advance!

Vincent
  • 185
  • 1
  • 9

1 Answers1

9

You have two questions here:

The app should scan for other devices running the app (in background)

The answer here is NO - MPC does not work in the background (see this so response)

For your second question:

is it possible to connect to other devices using multipeer but without having to show the alert

The answer is Yes .. all the detail you need can be found in the apple docs. Here's some snippets on what I do:

On one device - start the browser

_serviceBrowser = [[MCNearbyServiceBrowser alloc] initWithPeer:_peerID
                                                       serviceType:_sessionName];

[_serviceBrowser startBrowsingForPeers];

On the other device - start the advertiser

_serviceAdvertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:_peerID
                                                           discoveryInfo:nil
                                                             serviceType:_sessionName];
[_serviceAdvertiser startAdvertisingPeer];

These services implement delegate functions to advise your app of a possible connection. Here the browser is advised of an advertiser and now invites the peer to join in a session

- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID     withDiscoveryInfo:(NSDictionary *)info
{
    [browser invitePeer:peerID toSession:_session withContext:nil timeout:30.0];

}

The advertiser then responds

- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:    (MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler
{
    invitationHandler(YES, _session);
}

Now you will receive a session delegate call to advise you of the state of your peer connectivity. At this point you should be connected - no "real" user interaction required.

- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state
Community
  • 1
  • 1
300baud
  • 540
  • 4
  • 17
  • Thanks a lot! Do you know by accident how to solve this background problem? – Vincent Mar 16 '14 at 10:39
  • if by background you mean on another thread, then that is do-able.. MPC does that already. However if you mean that your app is in the background, then according to the feedback on the apple Dev forums, it's not possible.. and I can confirm I've tried it and not been successful – 300baud Mar 16 '14 at 11:52
  • I indeed meant the latter. Would Core Bluetooth help me out (since the data to transfer is 100KB max, but scanning and connecting to new devices is required)? – Vincent Mar 16 '14 at 12:57
  • Bluetooth or wifi.. same problem. no go in background – 300baud Mar 16 '14 at 20:06
  • in your above example, does it show the 'accept' dialog on the device that received the invitation? or is it suppressed? If it doesn't show, can you explain to me which code makes it happen/not happen? – joon Dec 21 '14 at 16:07