CocoaHeads Taichung
2014.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 Res...
在網狀網絡中,所有設備可以彼此直接通信,⽽而無需central hub
App 實例
Firechat
iOS
Android
iOS+Android
SDK-P2P網
個⼈人區域網路
NFC?
iBeacon? BLE?
Multipeer?
CoreBluetooth 低功耗藍⽛牙框架
Multipeer 網狀網絡框架
MCPeerID:周邊設備。
MCSession:內網會議室。
MCAdvertiserAssistant:廣播給附近的設備(快速版)。
MCBrowserView...
CoreBluetooth 低功耗藍⽛牙框架
Multipeer 網狀網絡框架
serviceType(指定連線服務)
foundPeer(發現外部設備)
didReceiveInvitationFromPeer(連接外部設備)
lostPee...
要進⼊入程式碼了!
新⼿手上場請多多包涵
1/5 Creating a Peer
_peerID = [[MCPeerID alloc] initWithDisplayName:displayName];
2/5 Creating a Session
_session = [[MCSe...
3/5 內建廣播
_advertiser = [[MCAdvertiserAssistant alloc]
initWithServiceType:ServiceType
discoveryInfo:nil
session:_session];...
5/5 開啟⾯面板
[[[appDelegate mcManager] advertiser] setDelegate:self];
[[[appDelegate mcManager] browser] setDelegate:self];
!...
MCBrowserViewControllerDelegate
-(void)browserViewControllerDidFinish:(MCBrowserViewController *)browserViewController
{
N...
Demo:跨協定Peer互通
3/5 實作Advertising
appDelegate.mcManager.NearbyAdvertiser =
[[MCNearbyServiceAdvertiser alloc]
initWithPeer:appDelegate.mcM...
4/5 實作Discovering
appDelegate.mcManager.NearbyBrowser =
[[MCNearbyServiceBrowser alloc]
initWithPeer:appDelegate.mcManager...
5/5 實作⾃自訂介⾯面
剩下的交給MCSessionDelegate
foundPeer 函式可收集找到的設備
_session.connectedPeers 可列出已連線設備
-(void)session:(MCSession *)sess...
MCNearbyServiceBrowserDelegate
-(void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID
*)peerID withDiscovery...
MCNearbyServiceAdvertiserDelegate
- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser
didReceiveInvitationFromPeer:...
MCNearbyServiceAdvertiserDelegate
//TODO:⾃自訂詢問
arrayInvitationHandler = [NSArray arrayWithObject:[invitationHandler
copy]]...
UIAlertViewDelegate
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
BOOL accept ...
發送訊息
MCSessionDelegate
MCSessionDelegate
// Received data from remote peer
-(void)session:(MCSession *)session didReceiveData:(NSData *)data from...
MCSessionDelegate
// Start receiving a resource from remote peer
-(void)session:(MCSession *)session didStartReceivingReso...
MCSessionDelegate
// Received a byte stream from remote peer
-(void)session:(MCSession *)session didReceiveStream:(NSInput...
Demo:⽣生活應⽤用
⺫⽬目標同步 - 出遊規劃分⼯工同步
戰術指導 - 儲存下來還可以有回合制
延伸螢幕 - 再⼤大的投影機都還是有搖滾區
導覽解說 - 更⽣生動即時的旅遊回憶(個⼈人語⾳音導覽機偏向iBeacon)
點餐系統 - 不必架設...
感謝聆聽
RAKI - 丫狸
raki-fox@msn.com
Upcoming SlideShare
Loading in …5
×

2014.06.26 CocoaHeads - Multipeer Connectivity Framework

644
-1

Published on

2014.06.26 CocoaHeads - Multipeer Connectivity Framework

Published in: Mobile, Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
644
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

2014.06.26 CocoaHeads - Multipeer Connectivity Framework

  1. 1. CocoaHeads Taichung 2014.06.26 RAKI - 丫狸
  2. 2. iOS藍⽛牙內網技術 這是⽩白話⽂文
  3. 3. Multipeer Connectivity Framework 其實就是 iOS7 的 https://dl.dropboxusercontent.com/u/2857188/MCDemo.zip
  4. 4. Multipeer Connectivity Framework 以Blueooth或是Wi-Fi建設起近場網絡 使附近的設備可以彼此聯通 無需通過web server中介處理 相連的節點都能夠安全地傳輸 1 NSData 訊息資料 2 Resources ⽂文件檔案 3 Streaming data 影⾳音串流
  5. 5. 在網狀網絡中,所有設備可以彼此直接通信,⽽而無需central hub
  6. 6. App 實例 Firechat iOS Android iOS+Android SDK-P2P網
  7. 7. 個⼈人區域網路 NFC? iBeacon? BLE? Multipeer?
  8. 8. CoreBluetooth 低功耗藍⽛牙框架 Multipeer 網狀網絡框架 MCPeerID:周邊設備。 MCSession:內網會議室。 MCAdvertiserAssistant:廣播給附近的設備(快速版)。 MCBrowserViewController:搜尋附近的設備(快速版)。 MCNearbyServiceAdvertiser:廣播給附近的設備(實作版)。 MCNearbyServiceBrowser:搜尋附近的設備(實作版)。 CBCentralManager 搜尋/連線/處理資料(中央設備) CBPeripheral 廣播/傳送/接收資料(外部設備) 快速對照
  9. 9. CoreBluetooth 低功耗藍⽛牙框架 Multipeer 網狀網絡框架 serviceType(指定連線服務) foundPeer(發現外部設備) didReceiveInvitationFromPeer(連接外部設備) lostPeer(外部設備斷線) scanForPeripheralsWithServices:nil(指定連線服務) didDiscoverPeripheral(發現外部設備) didConnectPeripheral(連接外部設備) didDisconnectPeripheral(外部設備斷線) 快速對照
  10. 10. 要進⼊入程式碼了! 新⼿手上場請多多包涵
  11. 11. 1/5 Creating a Peer _peerID = [[MCPeerID alloc] initWithDisplayName:displayName]; 2/5 Creating a Session _session = [[MCSession alloc] initWithPeer:_peerID]; _session.delegate = self;
  12. 12. 3/5 內建廣播 _advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:ServiceType discoveryInfo:nil session:_session]; [_advertiser start]; 4/5 內建搜尋 _browser = [[MCBrowserViewController alloc] initWithServiceType:ServiceType session:_session];
  13. 13. 5/5 開啟⾯面板 [[[appDelegate mcManager] advertiser] setDelegate:self]; [[[appDelegate mcManager] browser] setDelegate:self]; ! [self presentViewController:[[appDelegate mcManager] browser] animated:YES completion: ^{NSLog(@"打開browser");}];
  14. 14. 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]; }
  15. 15. Demo:跨協定Peer互通
  16. 16. 3/5 實作Advertising appDelegate.mcManager.NearbyAdvertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:appDelegate.mcManager.peerID discoveryInfo:nil serviceType:appDelegate.mcManager.ServiceType]; appDelegate.mcManager.NearbyAdvertiser.delegate = self; [appDelegate.mcManager.NearbyAdvertiser startAdvertisingPeer];
  17. 17. 4/5 實作Discovering appDelegate.mcManager.NearbyBrowser = [[MCNearbyServiceBrowser alloc] initWithPeer:appDelegate.mcManager.peerID serviceType:appDelegate.mcManager.ServiceType]; ! appDelegate.mcManager.NearbyBrowser.delegate = self; [appDelegate.mcManager.NearbyBrowser startBrowsingForPeers];
  18. 18. 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} }
  19. 19. 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]; }
  20. 20. MCNearbyServiceAdvertiserDelegate - (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler { //TODO:直接同意 ! 或是 ! //TODO:⾃自訂詢問 }
  21. 21. 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);
  22. 22. 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"); } }
  23. 23. 發送訊息 MCSessionDelegate
  24. 24. 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]; }
  25. 25. 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
  26. 26. 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); }
  27. 27. Demo:⽣生活應⽤用 ⺫⽬目標同步 - 出遊規劃分⼯工同步 戰術指導 - 儲存下來還可以有回合制 延伸螢幕 - 再⼤大的投影機都還是有搖滾區 導覽解說 - 更⽣生動即時的旅遊回憶(個⼈人語⾳音導覽機偏向iBeacon) 點餐系統 - 不必架設server很多事都可以在⼩小型區域內快速達成 會議記錄 - 開完會⾼高校產出跨部會記錄表 互動教材 - 學習模式數位化,資料收集可排除網路環境的限制
  28. 28. 感謝聆聽 RAKI - 丫狸 raki-fox@msn.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×