0

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

4

1 回答 1

0

我做了很多研究,我决定使用一个小的 5V 无线接入点来建立我的多个 iPod 通信系统。GameKit 的东西非常适合两个 iPod 互相通话,但它不是很可靠(而且范围是另一个问题)。CocoaAsyncSocket 很棒。这里似乎有很多人将其用于 TCP 广播和通信。

以下是我采取的一些步骤:

  • 我将我的“控制器”iPod 设置为打开与端口的连接。
  • 在我的“客户端”iPod 应用程序中,我让它们使用静态 IP 作为主机连接到“控制器”
  • 然后我向客户端发送命令并根据命令执行各种操作

有没有人成功地与本地网络中的多个 iPod 通信?我很想看看你是如何做到这一点的。

快乐编码

于 2011-01-01T00:10:16.293 回答