Search code examples
iphoneiosxcodebluetoothgamekit

Bluetooth warnings Xcode?


I'm trying to learn about using bluetooth connectivity through making a simple peer to peer bluetooth chat app. I'm hitting a bit of a problem as I'm new to exploring GameKit and Bluetooth and am hoping someone can help me. I think it's a formatting issue, but I could be very wrong. Basically when sending messages between both devices I'd like to use the name of the device that the message is coming from in the UIAlertView popup that displays the message. For example, 'Johnny's Iphone says:...'

Here's the bit of my code dealing with the AlertVIew:

- (void) receiveData:(NSData *)data 
        fromPeer:(NSString *)peer 
       inSession:(GKSession *)session 
         context:(void *)context {

//---convert the NSData to NSString---
NSString* str;
str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:(@"Chat Message: %@", peer)
                                                message:str 
                                               delegate:self 
                                      cancelButtonTitle:@"REPLY" 
                                      otherButtonTitles:nil];
[alert show];
[alert release];    
}

In the above I'm trying to use 'peer' to set the name but it's not working. I've tried initialising peer as a string and then passing it but this isn't working either. In both cases I get a string of 9 or 10 numbers with whatever message was sent displayed below. Where am I going wrong and what should I be doing?


Solution

  • Use [session displayNameForPeer:peer] which will return you the "display name"