使用外部附件框架连接到蓝牙附件时,我遇到了延迟问题。发送数据时,我在控制台中得到以下自定义输出:
if( [stream hasSpaceAvailable] )
{
NSLog( @"Space avail" );
}
else {
NSLog(@"No space");
}
while( [stream hasSpaceAvailable] && ( [_outputBuffer length] > 0 ) )
{
/* write as many bytes as possible */
NSInteger written = [stream write:[_outputBuffer bytes] maxLength:[_outputBuffer length]];
NSLog( @"wrote %i out of %i bytes to the stream", written, [_outputBuffer length] );
if( written == -1 )
{
/* error, bad */
Log( @"Error writing bytes" );
break;
}
else if( written > 0 )
{
/* remove the bytes from the buffer that were written */
Log( @"erasing %i bytes", written );
[_outputBuffer replaceBytesInRange:NSMakeRange( 0, written ) withBytes:nil length:0 ];
}
}
这会产生以下输出,其中立即包缓冲区是有效负载。
immediate pack buffer-> 040040008
Space avail
wrote 10 out of 10 bytes to the stream
immediate pack buffer-> 040010005
No space
immediate pack buffer-> 030040007
No space
wrote 20 out of 20 bytes to the stream
immediate pack buffer-> 030010004
No space
immediate pack buffer-> 040000004
Space avail
wrote 20 out of 20 bytes to the stream
immediate pack buffer-> 030000003
Space avail
wrote 10 out of 10 bytes to the stream
immediate pack buffer-> 040040008
Space avail
wrote 10 out of 10 bytes to the stream
请注意它是如何连续写入“无空间”的,这意味着该方法hasSpaceAvailable
返回 false 并强制缓冲数据,直到它返回 true。
1)我需要知道的是为什么会这样?它是在等待来自 BT 硬件的 Ack 吗?如果是这样,您如何消除此阻塞?
2)你如何做到这一点,以便它立即发送,我们基本上实时流式传输数据而不缓冲?
3) 是否有隐藏的 API 方法可以禁用此阻塞?
这是一个真正的问题,因为将数据发送到设备时不会有任何延迟/延迟,必须立即发送数据,以便硬件与 iPhone 命令同步。请帮忙。