Upload
po-wei-chang
View
455
Download
0
Embed Size (px)
DESCRIPTION
2014.06.26 CocoaHeads - Multipeer Connectivity Framework
Citation preview
CocoaHeads Taichung2014.06.26
RAKI - 丫狸
iOS藍⽛牙內網技術這是⽩白話⽂文
Multipeer Connectivity Framework 其實就是 iOS7 的
https://dl.dropboxusercontent.com/u/2857188/MCDemo.zip
Multipeer Connectivity Framework
以Blueooth或是Wi-Fi建設起近場網絡 使附近的設備可以彼此聯通 無需通過web server中介處理
相連的節點都能夠安全地傳輸 1 NSData 訊息資料 2 Resources ⽂文件檔案 3 Streaming data 影⾳音串流
在網狀網絡中,所有設備可以彼此直接通信,⽽而無需central hub
App 實例
Firechat
iOS Android
iOS+Android SDK-P2P網
個⼈人區域網路NFC?
iBeacon? BLE?
Multipeer?
CoreBluetooth 低功耗藍⽛牙框架
Multipeer 網狀網絡框架MCPeerID:周邊設備。 MCSession:內網會議室。 MCAdvertiserAssistant:廣播給附近的設備(快速版)。 MCBrowserViewController:搜尋附近的設備(快速版)。 MCNearbyServiceAdvertiser:廣播給附近的設備(實作版)。 MCNearbyServiceBrowser:搜尋附近的設備(實作版)。
CBCentralManager 搜尋/連線/處理資料(中央設備)CBPeripheral 廣播/傳送/接收資料(外部設備)
快速對照
CoreBluetooth 低功耗藍⽛牙框架
Multipeer 網狀網絡框架serviceType(指定連線服務) foundPeer(發現外部設備) didReceiveInvitationFromPeer(連接外部設備) lostPeer(外部設備斷線)
scanForPeripheralsWithServices:nil(指定連線服務) didDiscoverPeripheral(發現外部設備) didConnectPeripheral(連接外部設備) didDisconnectPeripheral(外部設備斷線)
快速對照
要進⼊入程式碼了!新⼿手上場請多多包涵
1/5 Creating a Peer_peerID = [[MCPeerID alloc] initWithDisplayName:displayName];
2/5 Creating a Session_session = [[MCSession alloc] initWithPeer:_peerID]; _session.delegate = self;
3/5 內建廣播_advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:ServiceType discoveryInfo:nil session:_session]; [_advertiser start];
4/5 內建搜尋 _browser = [[MCBrowserViewController alloc]
initWithServiceType:ServiceType session:_session];
5/5 開啟⾯面板[[[appDelegate mcManager] advertiser] setDelegate:self]; [[[appDelegate mcManager] browser] setDelegate:self]; !
[self presentViewController:[[appDelegate mcManager] browser] animated:YES completion: ^{NSLog(@"打開browser");}];
MCBrowserViewControllerDelegate-(void)browserViewControllerDidFinish:(MCBrowserViewController *)browserViewController { NSLog(@"完成browser"); [appDelegate.mcManager.browser dismissViewControllerAnimated:YES completion:nil]; } !
-(void)browserViewControllerWasCancelled:(MCBrowserViewController *)browserViewController { NSLog(@"取消browser"); [appDelegate.mcManager.browser dismissViewControllerAnimated:YES completion:nil]; }
Demo:跨協定Peer互通
3/5 實作AdvertisingappDelegate.mcManager.NearbyAdvertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:appDelegate.mcManager.peerID discoveryInfo:nil serviceType:appDelegate.mcManager.ServiceType]; appDelegate.mcManager.NearbyAdvertiser.delegate = self; [appDelegate.mcManager.NearbyAdvertiser startAdvertisingPeer];
4/5 實作DiscoveringappDelegate.mcManager.NearbyBrowser = [[MCNearbyServiceBrowser alloc] initWithPeer:appDelegate.mcManager.peerID serviceType:appDelegate.mcManager.ServiceType]; !
appDelegate.mcManager.NearbyBrowser.delegate = self; [appDelegate.mcManager.NearbyBrowser startBrowsingForPeers];
5/5 實作⾃自訂介⾯面
剩下的交給MCSessionDelegate
foundPeer 函式可收集找到的設備 _session.connectedPeers 可列出已連線設備
-(void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state { NSLog(@"Session for peer: %@, changed state to: %i", peerID, (int)state); //MCSessionStateNotConnected, // not in the session //MCSessionStateConnecting, // connecting to this peer //MCSessionStateConnected // connected to the session} }
MCNearbyServiceBrowserDelegate-(void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info { if (![appDelegate.mcManager.session.connectedPeers containsObject:peerID]{ //TODO:直接邀請 [appDelegate.mcManager.NearbyBrowser invitePeer:peerID toSession:appDelegate.mcManager.session withContext:nil timeout:0.0]; }
MCNearbyServiceAdvertiserDelegate- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler { //TODO:直接同意 !
或是 !
//TODO:⾃自訂詢問 }
MCNearbyServiceAdvertiserDelegate
//TODO:⾃自訂詢問 arrayInvitationHandler = [NSArray arrayWithObject:[invitationHandler copy]]; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:peerID.displayName message:@"Would like to create a session with you" delegate:self cancelButtonTitle:@"Decline" otherButtonTitles:@"Accept", nil]; [alertView show];
//TODO:直接同意 invitationHandler(YES,appDelegate.mcManager.session);
UIAlertViewDelegate- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { BOOL accept = (buttonIndex != alertView.cancelButtonIndex) ? YES : NO; //TODO:回答詢問 if(accept) { NSLog(@"Session accept"); void (^invitationHandler)(BOOL, MCSession *) = [arrayInvitationHandler objectAtIndex:0]; invitationHandler(accept, appDelegate.mcManager.session); } else { NSLog(@"Session disallowed"); } }
發送訊息MCSessionDelegate
MCSessionDelegate// Received data from remote peer -(void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID { NSString *message = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; !
NSDictionary *dict = @{@"data": data,@"peerID": peerID }; [[NSNotificationCenter defaultCenter] postNotificationName:@"MCDidReceiveDataNotification" object:nil userInfo:dict]; }
MCSessionDelegate// Start receiving a resource from remote peer -(void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress
// Finished receiving a resource from remote peer -(void)session:(MCSession *)session didFinishReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID atURL:(NSURL *)localURL withError:(NSError *)error
MCSessionDelegate// Received a byte stream from remote peer -(void)session:(MCSession *)session didReceiveStream:(NSInputStream *)stream withName:(NSString *)streamName fromPeer:(MCPeerID *)peerID { NSLog(@"Session started receiving stream: %@ with name: %@ from peer: %@", stream, streamName, peerID); }
Demo:⽣生活應⽤用⺫⽬目標同步 - 出遊規劃分⼯工同步戰術指導 - 儲存下來還可以有回合制延伸螢幕 - 再⼤大的投影機都還是有搖滾區導覽解說 - 更⽣生動即時的旅遊回憶(個⼈人語⾳音導覽機偏向iBeacon)點餐系統 - 不必架設server很多事都可以在⼩小型區域內快速達成會議記錄 - 開完會⾼高校產出跨部會記錄表互動教材 - 學習模式數位化,資料收集可排除網路環境的限制