20
Multi-Peer Connectivity

Multipeer Connectivity

Embed Size (px)

DESCRIPTION

Come check out Multi-Peer Connectivity, a brand new framework for connecting multiple devices in a peer-to-peer network. We'll learn how to get a basic application up and running and take advantage of these new APIs to create a whole new interactive dynamic in your apps.

Citation preview

Page 1: Multipeer Connectivity

Multi-Peer Connectivity

Page 2: Multipeer Connectivity

Architecture

Page 3: Multipeer Connectivity

Multi-Peer Connectivity

• New in iOS 7

• Ability to connect to a mesh of peers.

• Able to connect to peers over WiFi, ad-hoc wireless, and Bluetooth.

• Peers must be ‘nearby’.

Page 4: Multipeer Connectivity

Terms

• Advertiser - Broadcasts availability to connect

• Browser - Invites advertisers to join a session

Page 5: Multipeer Connectivity

MCSession

• MCSession manages all communication between peers.

• Must be used by all participants.

Page 6: Multipeer Connectivity

Create A Session

self.peerID = [[MCPeerID alloc] initWithDisplayName:@"Jill"]; !self.currentSession = [[MCSession alloc] initWithPeer:self.peerID]; self.currentSession.delegate = self;

Page 7: Multipeer Connectivity

Advertiser AdvertiserBrowser

Ready to chat!

Ready to chat!

Who wants to chat?

Discovery

Page 8: Multipeer Connectivity

Advertising

self.peerID = [[MCPeerID alloc] initWithDisplayName:@"Jill"]; !self.advertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:self.peerID discoveryInfo:nil serviceType:@"whartman-chat"]; self.advertiser.delegate = self; [self.advertiser startAdvertisingPeer];

Page 9: Multipeer Connectivity

Browsing

self.peerID = [[MCPeerID alloc] initWithDisplayName:@"Jack"]; !self.browser = [[MCNearbyServiceBrowser alloc] initWithPeer:self.peerID serviceType:@"whartman-chat"]; self.browser.delegate = self; [self.browser startBrowsingForPeers];

Page 10: Multipeer Connectivity

Browser: Discover & Invite

- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info { [browser invitePeer:peerID toSession:self.currentSession withContext:nil timeout:3.0]; }

Page 11: Multipeer Connectivity

Advertiser: Accept Invitation

- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler { // In most cases you will want to give users an option to connect or not. invitationHandler(YES, self.currentSession); [self.advertiser stopAdvertisingPeer]; // Once invited, stop advertising }

Page 12: Multipeer Connectivity

Sending Data To Peers

NSDictionary *message = @{ @"message" : @"Hello!" }; NSData *messageData = [NSJSONSerialization dataWithJSONObject:message options:0 error:nil]; !NSError *error = nil; ![self.currentSession sendData:messageData toPeers:self.currentSession.connectedPeers withMode:MCSessionSendDataReliable error:&error];

Page 13: Multipeer Connectivity

Receiving Data From Peers- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID { NSError *error = nil; ! NSDictionary *recievedData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; ! if (!recievedData) { NSLog(@"error decoding message! %@", error); } else { // Display message to the user... } }

Page 14: Multipeer Connectivity

Sending Resources To PeersNSString *filePath = [[NSBundle mainBundle] pathForResource:@"hello" ofType:@"jpg"]; NSURL *imageURL = [NSURL fileURLWithPath:filePath]; ![self.currentSession sendResourceAtURL:imageURL withName:@"hello.jpg" toPeer:peer withCompletionHandler:^(NSError *error) { if (error) { NSLog(@"Error sending image! %@", error); } }];

Page 15: Multipeer Connectivity

Receiving Resources From Peers- (void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress { NSLog(@"downloading file: %f%%", progress.fractionCompleted); } !!!!- (void)session:(MCSession *)session didFinishReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID atURL:(NSURL *)localURL withError:(NSError *)error { ! if (error) { NSLog(@"Error when receiving file! %@", error); } else { // Present the resource to the user. } }

Page 16: Multipeer Connectivity

Demo

Page 17: Multipeer Connectivity

Best Practices

• Keep messages short.

• Be careful with inviting/accept invitations.

• As an advertiser, once you have accepted an invitation, stop advertising.

Page 18: Multipeer Connectivity

Limitations

• Limit to 8 connected peers.

• Data transfer is slow—especially on Bluetooth.

Page 19: Multipeer Connectivity

Questions?

Page 20: Multipeer Connectivity

Resources

• Nearby Networking with Multipeer Connectivity (WWDC 2013)

• Demo App - https://github.com/waynehartman/MultiPeerTest/