如何在iOS 6.1上正确设置GKSession(蓝牙) [英] How do I correctly setup a GKSession (Bluetooth) on iOS 6.1

查看:128
本文介绍了如何在iOS 6.1上正确设置GKSession(蓝牙)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使GKSession正常工作时遇到问题.下面是我按下特定按钮时执行的代码.

GKSession *session;
if (connectButtonHasBeenPressed == false) {
    NSLog(@"connectToBluetoothDevice has been called");
    connectButtonHasBeenPressed = true;
    GKSession *session = [[GKSession alloc] initWithSessionID:@"Unicorn" displayName:nil sessionMode:GKSessionModePeer];
    [session setDataReceiveHandler:self withContext:nil];
    [session setDelegate:self];
    [session setAvailable:YES];
    NSLog(@"Session ID: %@", [session sessionID]);
    NSLog(@"Currently Available Peers: %i", [[session peersWithConnectionState:GKPeerStateAvailable] count]);
    if ([session isAvailable]) {
        NSLog(@"The Session Is Available");
    }
    [connectToDeviceButton setTitle:@"Searching..." forState:UIControlStateNormal];

}
else {
    NSLog(@"Currently Available Peers: %i", [[session peersWithConnectionState:GKPeerStateAvailable] count]);
}

第一次按下按钮后,一切似乎都正常.并且此后每当我按下该按钮时,它将打印当前可用对等体:0".如果我没有两个设备并排运行,并且都在按下按钮的情况下运行了程序,那么这将是预期的输出.我还具有在该类中实现的所有GKSessionDelegate方法,这些方法都将消息记录到控制台.这些方法都没有运行过.所有这些都向我表明这些设备找不到彼此.

但是,我运行了示例程序GKRocket,该程序使用GKSession连接两个设备,并且在这两个设备之间正常工作.我已经将GKRocket的代码与程序的代码进行了比较,但没有发现我认为可能会影响GKSession的任何差异.

有什么建议吗?

解决方案

您似乎有两个GKSession实例. if语句位于一个外部,另一个位于内部.

这意味着,如果connectButtonHasBeenPressedfalse,它将创建它自己保留的GKSession版本.但是如果它是true,则session将等于nil.

此外,我建议使用nil作为会话ID,然后使用捆绑ID为您设置会话ID.尽管这可能是个人喜好.

尝试使用类似这样的内容:

if (session == nil)
{
    NSLog(@"connectToBluetoothDevice has been called");
    session = [[GKSession alloc] initWithSessionID:nil displayName:nil sessionMode:GKSessionModePeer];
    [session setDataReceiveHandler:self withContext:nil];
    [session setDelegate:self];
    [session setAvailable:YES];

    NSLog(@"Session ID: %@", [session sessionID]);
    if ([session isAvailable])
    {
        NSLog(@"The Session Is Available");
    }
    [connectToDeviceButton setTitle:@"Searching..." forState:UIControlStateNormal];

    connectButtonHasBeenPressed = true;
}

NSLog(@"Currently Available Peers: %i", [[session peersWithConnectionState:GKPeerStateAvailable] count]);

您实际上并不需要一个connectButtonHasBeenPressed变量,因为您只需检查GKSession是否等于nil即可,如果没有连接,它应该始终为nil.会话结束后,您应始终取消所有会话操作并设置session = nil;.

实际上应该在.h文件中声明session变量,以便您可以在整个类中使用它.这样就不再需要GKSession *session;.

注意事项:只是connectToBluetoothDevice has been called日志中的一条注意事项.根据我的经验,GKSession将使用WiFi或蓝牙,具体取决于可用的设备.如此之多,您可以拥有3台设备,其中1台仅启用蓝牙,1台仅启用WiFi,最后一台同时启用,它们都可以相互连接并正常通信.

希望这会有所帮助.

编辑:从示例代码中删除了不需要的connectButtonHasBeenPressed变量,并添加了更多说明.

I am having an issue with getting a GKSession to work. Below is my code that is executed when a specific button is pressed.

GKSession *session;
if (connectButtonHasBeenPressed == false) {
    NSLog(@"connectToBluetoothDevice has been called");
    connectButtonHasBeenPressed = true;
    GKSession *session = [[GKSession alloc] initWithSessionID:@"Unicorn" displayName:nil sessionMode:GKSessionModePeer];
    [session setDataReceiveHandler:self withContext:nil];
    [session setDelegate:self];
    [session setAvailable:YES];
    NSLog(@"Session ID: %@", [session sessionID]);
    NSLog(@"Currently Available Peers: %i", [[session peersWithConnectionState:GKPeerStateAvailable] count]);
    if ([session isAvailable]) {
        NSLog(@"The Session Is Available");
    }
    [connectToDeviceButton setTitle:@"Searching..." forState:UIControlStateNormal];

}
else {
    NSLog(@"Currently Available Peers: %i", [[session peersWithConnectionState:GKPeerStateAvailable] count]);
}

After the button is pressed the first time, everything appears to be working okay. And every time I press the button after that, it prints "Currently Available Peers: 0". This would be the expected output if I didn't have two devices sitting next to each other, running the program both with the button pushed. I also have all of the GKSessionDelegate methods implemented into this class, which all log a message to the console. None of those methods are ever run. All of this would indicate to me that the devices could not find each other.

However, I have run the sample program GKRocket which uses GKSession to connect two devices and it works fine between these same two devices. I have compared the code of GKRocket to my program's code, and I have not found any differences that I think could effect GKSession.

Any Suggestions?

解决方案

You seem to have two instances of GKSession. One outside and the other inside the if statement.

This means that if connectButtonHasBeenPressed is false it will create it's own version of GKSession that it keeps. but if it's true then session will equal nil.

Also I would recommend using nil as the session ID as it then gets set for you using the bundle ID. Though this may be personal preference.

Try using something like this:

if (session == nil)
{
    NSLog(@"connectToBluetoothDevice has been called");
    session = [[GKSession alloc] initWithSessionID:nil displayName:nil sessionMode:GKSessionModePeer];
    [session setDataReceiveHandler:self withContext:nil];
    [session setDelegate:self];
    [session setAvailable:YES];

    NSLog(@"Session ID: %@", [session sessionID]);
    if ([session isAvailable])
    {
        NSLog(@"The Session Is Available");
    }
    [connectToDeviceButton setTitle:@"Searching..." forState:UIControlStateNormal];

    connectButtonHasBeenPressed = true;
}

NSLog(@"Currently Available Peers: %i", [[session peersWithConnectionState:GKPeerStateAvailable] count]);

You don't really need to have a connectButtonHasBeenPressed variable as you can just check to see if the GKSession is equal to nil, which it should always be if there is no connection. When your session ends you should always cancel all session actions and set session = nil;.

The session variable should really be declared in your .h file so that you can use it throughout the class. So that GKSession *session; is no longer needed.

Note: Just a note from your connectToBluetoothDevice has been called log. From my experience, GKSession will use WiFi or Bluetooth, depending on whatever is available. So much so that you can have 3 devices, 1 with only Bluetooth on, 1 with only WiFi on and the last with both on and they will all connect and talk with each other absolutely fine.

Hope this helps.

Edit: Removed unneeded connectButtonHasBeenPressed variable from example code and added more explanation.

这篇关于如何在iOS 6.1上正确设置GKSession(蓝牙)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆