Your SlideShare is downloading. ×
Multipeer Connectivity
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Multipeer Connectivity

3,149
views

Published on

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 …

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.

Published in: Technology

3 Comments
3 Likes
Statistics
Notes
No Downloads
Views
Total Views
3,149
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
53
Comments
3
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Multi-Peer Connectivity
  • 2. Architecture
  • 3. 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’.
  • 4. Terms • Advertiser - Broadcasts availability to connect • Browser - Invites advertisers to join a session
  • 5. MCSession • MCSession manages all communication between peers. • Must be used by all participants.
  • 6. Create A Session self.peerID = [[MCPeerID alloc] initWithDisplayName:@"Jill"]; ! self.currentSession = [[MCSession alloc] initWithPeer:self.peerID]; self.currentSession.delegate = self;
  • 7. Discovery Browser Advertiser Advertiser Who wants to chat? Ready to chat! Ready to chat!
  • 8. 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];
  • 9. Browsing self.peerID = [[MCPeerID alloc] initWithDisplayName:@"Jack"]; ! self.browser = [[MCNearbyServiceBrowser alloc] initWithPeer:self.peerID serviceType:@"whartman-chat"]; self.browser.delegate = self; [self.browser startBrowsingForPeers];
  • 10. Browser: Discover & Invite - (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info { [browser invitePeer:peerID toSession:self.currentSession withContext:nil timeout:3.0]; }
  • 11. 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 }
  • 12. 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];
  • 13. 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... } }
  • 14. Sending Resources To Peers NSString *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); } }];
  • 15. 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. } }
  • 16. Demo
  • 17. Best Practices • Keep messages short. • Be careful with inviting/accept invitations. • As an advertiser, once you have accepted an invitation, stop advertising.
  • 18. Limitations • Limit to 8 connected peers. • Data transfer is slow—especially on Bluetooth.
  • 19. Questions?
  • 20. Resources • Nearby Networking with Multipeer Connectivity (WWDC 2013) • Demo App - https://github.com/waynehartman/MultiPeerTest/