Skip to main content
gamedev.net
🔒 Locked

Bluetooth - InputStream.read() returns -1

Started by Kiristu 1 reply 1.4k views
Original Post
Kiristu
Kiristu
Hello, I try to create a client/server as simple as possible using Bluetooth between 2 mobile phones (Sony/Ericson W810i and Motorola V360v). I tried with the example provided with J2ME Wireless Toolkit "BluetoothDemo", that works perfectly in WTK, but not at all on the mobiles. Trying to find something simpler, I found this site: http://www.webpronews.com/topnews/2004/12/27/introduction-to-bluetooth-and-jme-part And in particular the sample sources: http://www.jasonlam604.com/downloads/content/articles/bluetoothechodemo_source.zip But even with this very simple code, I can't get the thing working. I've added some logs here and there to trace the execution, and I found it is stuck at the input.read() step (line 10):

public final static String readData(StreamConnection conn) {
 
    InputStream input = null;
    byte[] data = null;
 
    try {
      input = conn.openInputStream();
 
      // Probably want to throw an exception if length is not greater then 0
      int length = input.read();
      data= new byte[length];
      length = 0;
 
      // Assemble data
      while (length != data.length) {
        int ch = input.read(data, length, data.length - length);
        if (ch == -1) {
          throw new IOException("Can't read data");
        }
        length += ch;
      }
 
    }
    catch (IOException e) {
    }
    catch (Exception e) {
    } finally {
 
      // close input stream
      if (input != null) {
        try {
          input.close();
        } catch (IOException e) {
        }
      }
    }
    return new String(data);
  }
}


I've checked that the output.write() works well, and it does. So, the client sees the server, and can read from it and write to it, but when I try to read on the server side it does not work (if server is Sony and client is Motorola). If I set the client Sony and server Motorola, this is not exactly the same, but something like it. Is it an incorrect way to do that ? Is it possible for a mobile phone to be Bluetooth, but not able to work in a simple client/server application ? Is there any other way to share simple data (short strings for instance) ? Could it be related to some security settings in the application or in the phones ? Thanks for your help, K. [Edited by - Kiristu on January 27, 2008 2:03:05 PM]
Kiristu
Kiristu
Finally, it seems that my initial problem was caused by the missing flush on the output side (that is supposed to do nothing according to the javadoc ...).

I've added it and the read does not stuck the process anymore.
But if I read from Sony on Motoral (that writes), the read method always returns -1.

The write does not throw any error, and finishes.

Any idea ?
MassacrerAL
MassacrerAL
none, but keep us informed. maybe -1 is returned in some special case?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.