iOS VOIP socket over bluetooth PAN closed on screen locked
iOS VOIP socket over bluetooth PAN closed on screen locked
- Subject: iOS VOIP socket over bluetooth PAN closed on screen locked
- From: Yu Zhenyang <email@hidden>
- Date: Thu, 04 Jul 2013 16:11:26 +0800
I follow the URL: http://www.raywenderlich.com/29948/backgrounding-for-ios
to test voip function on iPhone.
I connect the iPhone to my Mac machine with bluetooth PAN profile. So
my iPhone can communicate my Mac machine with wifi socket or bluetooth
PAN socket (using different server IP).
Everything works OK when I setup voip server IP with Mac machine's wifi IP and run nc -l 10000
command in Mac to get a socket server. After I press the power button
on the iPhone and iPhone screen is locked, the socket connection between
iPhone and Mac still alive after. That say, I can also wake up the
iPhone by typing "notify" in Mac.
But, when I setup voip server IP with Mac machine's bluetooth PAN IP and run nc -l 10000
command again. The socket connection is closed when I press the power button on the iPhone and the nc -l 10000
exit. It seems that the voip flag setting up for bluetooth PAN network socket does not worked as previous one.
Can someone point how to avoid the problem because I need to
communicate iPhone to another device using bluetooth PAN profile
providing VOIP services.
The sample source code can be downloaded from: http://cdn3.raywenderlich.com/wp-content/uploads/2013/04/TheBackgrounder-final.zip
The key code for voip services is listed:
- (IBAction)didTapConnect:(id)sender
{
if (!self.inputStream)
{
// 1
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)(self.txtIP.text), [self.txtPort.text intValue], &readStream, &writeStream);
// 2
self.sentPing = NO;
self.communicationLog = [[NSMutableString alloc] init];
self.inputStream = (__bridge_transfer NSInputStream *)readStream;
self.outputStream = (__bridge_transfer NSOutputStream *)writeStream;
[self.inputStream setProperty:NSStreamNetworkServiceTypeVoIP forKey:NSStreamNetworkServiceType];
// 3
[self.inputStream setDelegate:self];
[self.outputStream setDelegate:self];
[self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
// 4
[self.inputStream open];
[self.outputStream open];
// 5
[[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{
if (self.outputStream)
{
[self.outputStream write:pingString maxLength:strlen((char*)pingString)];
[self addEvent:@"Ping sent"];
}
}];
}
}
and
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
switch (eventCode) {
case NSStreamEventNone:
// do nothing.
break;
case NSStreamEventEndEncountered:
[self addEvent:@"Connection Closed"];
break;
case NSStreamEventErrorOccurred:
[self addEvent:[NSString stringWithFormat:@"Had error: %@", aStream.streamError]];
break;
case NSStreamEventHasBytesAvailable:
if (aStream == self.inputStream)
{
uint8_t buffer[1024];
NSInteger bytesRead = [self.inputStream read:buffer maxLength:1024];
NSString *stringRead = [[NSString alloc] initWithBytes:buffer length:bytesRead encoding:NSUTF8StringEncoding];
stringRead = [stringRead stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
[self addEvent:[NSString stringWithFormat:@"Received: %@", stringRead]];
if ([stringRead isEqualToString:@"notify"])
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = @"New VOIP call";
notification.alertAction = @"Answer";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
else if ([stringRead isEqualToString:@"ping"])
{
[self.outputStream write:pongString maxLength:strlen((char*)pongString)];
}
}
break;
case NSStreamEventHasSpaceAvailable:
if (aStream == self.outputStream && !self.sentPing)
{
self.sentPing = YES;
if (aStream == self.outputStream)
{
[self.outputStream write:pingString maxLength:strlen((char*)pingString)];
[self addEvent:@"Ping sent"];
}
}
break;
case NSStreamEventOpenCompleted:
if (aStream == self.inputStream)
{
[self addEvent:@"Connection Opened"];
}
break;
default:
break;
}
}
--
Output is principal.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Bluetooth-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden