Posts: 108
Joined: Sun Feb 13, 2005 3:19 pm

Improving I/O performance

Tue Jun 20, 2006 11:40 am

To improve performance on synchronous streams, you should introduce buffering to avoid frequent calls to the bluetooth stack. This should be done only if you write data using WriteByte or write small chunks of data using Write, but does not introduce any significant overhead if you apply it in other situations.

.NET provides the BufferedStream for adding buffering to arbitrary streams. To use it, simply create a new BufferedStream instance that wraps the remote service stream acquired from e.g. a service discovery:

Code: Select all

Franson.Bluetools.RemoteService service = ...;
System.IO.Stream stream = new System.IO.BufferedStream(service.Stream);
Now the stream can be used as any other stream, with the exception that you need to flush the stream when the data should be written. This would occure in e.g. when a packet is sent and a response is awaited. Example

Code: Select all

Stream stream = new BufferedStream(myRemoteService.Stream);
Packet packet = ...;

stream.Write(packet.Data, 0, packet.Data.Length);
stream.Flush(); // This is necessary, or the packet data may get stuck in the buffer
Note that the BufferedStream is not available on Compact Framework. Here you should instead use Write to write chunks of data or write your own buffering mechanism.

Andreas Nilsson
Franson Support

Return to “Knowledge base BlueTools”