我在使用外部附件框架连接到蓝牙配件时遇到延迟问题。当发送数据时,我得到在控制台下面的自定义输出:如何让NSOutputStream立即发送或刷新数据包
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命令同步。请帮忙。