I've been trying to create a simple client/server network over Bluetooth with 5 iPods. One iPod will be sending basic strings like "stop" or "go" to four iPods, which will then do various stuff when they receive the commands. I'm currently using GameKit without PeerPicker, and it has been unreliable and complicated to even start the connections. Once connected, I can send data just fine between two devices, but not more.
I'd like to be able to broadcast simple, short messages to iPod "clients" from an iPod "server" without a network (like a remote controller). I've looked at numerous examples, including the tanks and wiTap examples, but I'm surprised there's nothing more straightforward without needing a network.
Here is how I start the connection:
- (IBAction) btnConnect:(id)sender
{
if (sender == connectServer) {
self.currentSession = [[GKSession alloc] initWithSessionID:@"0000"
displayName:nil
sessionMode:GKSessionModeServer];
NSLog(@"Setup Server Connection");
} else { //connectClient
// Peers discover themselves ...
self.currentSession = [[GKSession alloc] initWithSessionID:@"0000"
displayName:nil
sessionMode:GKSessionModeClient];
NSLog(@"Setup Client Connection");
}
amAcceptingConnections = YES;
[self.currentSession peersWithConnectionState:GKPeerStateAvailable];
self.currentSession.delegate = self;
self.currentSession.disconnectTimeout = 30;
[self.currentSession setDataReceiveHandler:self withContext:nil];
// Advertise the session to peers
self.currentSession.available = YES;
}
And I handle the connection with the didChangeState in the delegate
- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {
thePeerID = [session displayNameForPeer:peerID];
NSLog(@"didChangeState was called from peerID: %@.", thePeerID);
self.currentSession = session;
switch (state) {
case GKPeerStateAvailable:
thePeerID = [session displayNameForPeer:peerID];
NSLog(@"Peer %@ Available", thePeerID);
[session connectToPeer:peerID withTimeout:30];
NSLog(@"Issued Peer Connection");
//session.available = NO; //TODO: Look at this
break;
case GKPeerStateUnavailable:
NSLog(@"Peer %@ Unavailable", thePeerID);
break;
case GKPeerStateConnected:
NSLog(@"Peer %@ Connected", thePeerID);
break;
case GKPeerStateDisconnected:
NSLog(@"Peer %@ Disconnected", thePeerID);
[self.currentSession release];
currentSession = nil;
break;
}
}
The trouble is that the didChangeState method fires when it's good and ready, and the behavior is unpredictable. Maybe it's a problem with Bluetooth or GameKit, but I'd like to connect without the other iPod "authorizing" the connection. Is this possible?
Think I'm going to need a router and a network for something this simple?
Thank you