开发者

Problem with Sending and Receiving Files with SPP over Bluetooth

I am attempting to transfer files (MP3s about six megabytes in size) between two PCs using SPP over Bluetooth (in Java, with the BlueCove API). I can g开发者_如何学编程et the file transfer working fine in one direction (for instance, one file from the client to the server), but when I attempt to send any data in the opposite direction during the same session (i.e., send a file from the server to the client), the program freezes and will not advance.

For example, if I simply:

StreamConnection conn;
OutputStream outputStream;

outputStream = conn.openOutputStream();

....

outputStream.write(data);  //Data here is an MP3 file converted to byte array

outputStream.flush();

The transfer works fine. But if I try:

StreamConnection conn;
OutputStream outputStream;
InputStream inputStream;
ByteArrayOutputStream out = new ByteArrayOutputStream();

outputStream = conn.openOutputStream();
inputStream = conn.openInputStream();

....

outputStream.write(data);
outputStream.flush();


int receiveData;
while ((receiveData = inputStream.read()) != -1) {
   out.write(receiveData);
}

Both the client and the server freeze, and will not advance. I can see that the file transfer is actually happening at some point, because if I kill the client, the server will still write the file to the hard drive, with no issues. I can try to respond with another file, or with just an integer, and it still will not work.

Anyone have any ideas what the problem is? I know OBEX is commonly used for file transfers over Bluetooth, but it seemed overkill for what I needed to do. Am I going to have to use OBEX for this functionality?


It could be as simple as both programs stuck in blocking receive calls, waiting for the other end to say something... try adding a ton of log statements so you can see what "state" each program is in (ie, so it gives you a running commentary such as "trying to recieve", "got xxx data", "trying to reply", etc), or set up debugging, wait until it gets stuck and then stop one of them and single step it.


you can certainly use SPP to transfer file between your applications (assuming you are sending and receiving at both ends using your application). From the code snippet it is difficult to tell what is wrong with your program. I am guessing that you will have to close the stream as an indication to the other side that you are done with sending the data .. Note even though you write the whole file in one chunk, SPP / Bluetooth protocol layers might fragment it and the other end could receive in fragments, so you need to have some protocol to indicate transfer completion.


It is hard to say without looking at the client side code, but my guess, if the two are running the same code (i.e. both writing first, and then reading), is that the outputStream needs to be closed before the reading occurs (otherwise, both will be waiting for the other to close their side in order to get out of the read loop, since read() only returns -1 when the other side closes).

If the stream should not be closed, then the condition to stop reading cannot be to wait for -1. (so, either change it to transmit the file size first, or some other mechanism).


Why did you decide to use ByteArrayOutputStream? Try following code:

    try {
        try {

            byte[] buf = new byte[1024];

            outputstream = conn.openOutputStream();
            inputStream = conn.openInputStream();

            while ((n = inputstream.read(buf, 0, 1024)) > -1)
                outputstream.write(buf, 0, n);                

        } finally {
            outputstream.close();
            inputstream.close();
            log.debug("Closed input streams!");
        }
    } catch (Exception e) {
    log.error(e);
        e.printStackTrace();
    }

And to convert the outputStream you could do something like this:

byte currentMP3Bytes[] = outputStream.toString().getBytes();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(currentMP3Bytes);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜