搜尋此網誌

2010年5月16日 星期日

iphone開發-Peer-to-Peer Connectivity used by Bluetooth

最近寫過wifi 也有看到怎麼用藍芽來建立雙方連線 順便記錄一下

GamekitTestViewController.h

#import <UIKit/UIKit.h>
#import <GameKit/GameKit.h>

@interface GameKitTestViewController :
//用GKPeerPickerControllerDelegate 協定
UIViewController <GKPeerPickerControllerDelegate,GKSessionDelegate>
{
GKPeerPickerController *mPicker;
GKSession *mSession;
IBOutlet UITextField *mTextField;
IBOutlet UITextView *mTextView;
NSMutableArray *mPeers;
}
-(IBAction) connectClicked:(id)sender;
-(IBAction) sendData:(id)sender;
@property (retain) GKSession *mSession;

@end

GameKitTestViewController.m

#import " GameKitTestViewController.h "

@implementation GameKitTestViewController

@synthesize mSession;

- (void)viewDidLoad {
[super viewDidLoad];

mPicker=[[GKPeerPickerController alloc] init];
mPicker.delegate=self;
//使用iphone內建介面 讓user選擇要wifi or bluetooh
//但這邊須注意一點是 iphone 藍芽的部分 會自動幫你建立雙方的連線 但是透過
wifi的話 就要自己寫 可以參考我前幾篇
mPicker.connectionTypesMask = GKPeerPickerConnectionTypeNearby | GKPeerPickerConnectionTypeOnline;
mPeers=[[NSMutableArray alloc] init];
//顯示週遭有哪些peer
[mPicker show];
}

//當user選擇哪種連線時 就會呼叫下列method
- (void)peerPickerController:(GKPeerPickerController *)picker didSelectConnectionType:(GKPeerPickerConnectionType)type{
if (type == GKPeerPickerConnectionTypeOnline) {
picker.delegate = nil;
[picker dismiss];
[picker autorelease];
}
}
//當peer picker 需要session 就會呼叫此method 也就是準備session讓其他peer可以連進來
- (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type{

NSString *txt=mTextField.text;
GKSession* session = [[GKSession alloc] initWithSessionID:@"test" displayName:txt sessionMode:GKSessionModePeer];
[session autorelease];
return session;
}

//當連線建立完成時 會呼叫此method
- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session{

NSLog(@"Connected from %@",peerID);

self.mSession = session;
session.delegate = self;
[session setDataReceiveHandler: self withContext:nil];
picker.delegate = nil;
[picker dismiss];
[picker autorelease];
}

//這邊如同wifi 就不再敘述了
- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state{

switch (state)
{
case GKPeerStateConnected:
{
NSString *str=[NSString stringWithFormat:@"%@\n%@%@",mTextView.text,@"Connected from pier ",peerID];
mTextView.text= str;
NSLog(str);
[mPeers addObject:peerID];
break;
}
case GKPeerStateDisconnected:
{
[mPeers removeObject:peerID];

NSString *str=[NSString stringWithFormat:@"%@\n%@%@",mTextView.text,@"DisConnected from pier ",peerID];
mTextView.text= str;
NSLog(str);
break;
}
}
}

6 則留言:

  1. 不好意思想請問一下,如果要在didSelectConnectionType使用wifi步驟大概是怎樣,是不是藥用自己的UI,如果要使用UDP應該如何實作,謝謝!

    回覆刪除
  2. 把didSelectConnectionType這個method和選擇藍牙or wifi介面加入到我前幾篇的wifi那個 並且在didSelectConnectionType中加入else{[myGKSession connectToPeer:peerID withTimeout:10.0];}這樣user應該就能用那個介面選擇wifi來和其他人連線

    回覆刪除
  3. 不好意思,如果藥用選擇UDP來傳送,socket的建置及連結應該怎麼做,感謝

    回覆刪除
  4. 你好,這部份我只有在桌上電腦用java寫過server, iphone上 我就不清楚了 剛有google一下 應該可以參考一下此篇 http://0rz.tw/7UQ3M

    回覆刪除
  5. 還有這篇
    http://0rz.tw/jXOt7

    回覆刪除
  6. 作者已經移除這則留言。

    回覆刪除