Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Wellington2k

macrumors regular
Original poster
Jun 4, 2011
131
0
Hello!

I'm trying to use bluetooth for one of my games.

So to test I made a simple app that sends an alert.

I put the app on my iPhone and ran it at the same time as the simulator.

The simulator finds my iPhone and I tap to connect.

I agree and then one of three things happen.

1. Simulator says waiting for iPhone and iPhone just sits at agree.

2. Simulator says connected successfully and iPhone just sits at agree.

3. iPhone says connected successfully and simulator just sits at waiting for iPhone.

.h
Code:
#import <GameKit/GameKit.h>

@interface ViewController : UIViewController <GKSessionDelegate, GKPeerPickerControllerDelegate>{
    GKSession *Session;
    GKPeerPickerController *Picker;
    NSMutableArray *Peers;
}

@property (retain) GKSession *Session;

- (void) connectToPeers:(id) sender;
- (void) sendText;
-(IBAction)sendit;

.m
Code:
-(IBAction)sendit {
    [self sendText];
}

#pragma mark -
#pragma mark GKPeerPickerControllerDelegate

// Tells us that the peer was connected
- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session{
    
    // Get the session and assign it locally
    self.Session = session;
    session.delegate = self;
    
    //No need of teh picekr anymore
    picker.delegate = nil;
    [picker dismiss];
}

// Function to receive data when sent from peer
- (void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context
{
    //Convert received NSData to NSString to display
    NSString *whatDidIget = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    
    //Dsiplay the  as a UIAlertView
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@" Received" message:whatDidIget delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

#pragma mark -
#pragma mark GKSessionDelegate

- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state{
    
    if(state == GKPeerStateConnected){
        // Add the peer to the Array
        [Peers addObject:peerID];
        
        NSString *str = [NSString stringWithFormat:@"Connected with %@",[session displayNameForPeer:peerID]];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connected" message:str delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
    
}

- (void) connectToPeers:(id) sender{
    [Picker show];
}

- (void) sendText{
    NSLog(@"Sent!");
    NSString *loud = @"Text Sent!";
    
    // Send the  to Peers using teh current sessions
    [Session sendData:[loud dataUsingEncoding: NSASCIIStringEncoding] toPeers:Peers withDataMode:GKSendDataReliable error:nil];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    Picker = [[GKPeerPickerController alloc] init];
    Picker.delegate = self;
    
    //There are 2 modes of connection type
    // - GKPeerPickerConnectionTypeNearby via BlueTooth
    // - GKPeerPickerConnectionTypeOnline via Internet
    // We will use Bluetooth Connectivity for this example
    
    Picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
    Peers=[[NSMutableArray alloc] init];
    
    // Create the buttons
    UIButton *btnConnect = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btnConnect addTarget:self action:@selector(connectToPeers:) forControlEvents:UIControlEventTouchUpInside];
    [btnConnect setTitle:@"Connect" forState:UIControlStateNormal];
    btnConnect.frame = CGRectMake(20, 100, 280, 30);
    btnConnect.tag = 12;
    [self.view addSubview:btnConnect];
}
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.