Results 1 to 2 of 2
  1. #1
    werner291 is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Data input stream behaving strangely

    Hello,

    I'm trying to get some input from a Bluetooth device.
    The device sends a UTF string multiple times every second: "USDistance *variable int*"

    handleCommands() is called inside the main loop, multiple times per second.

    this function is behaving (as far as I know) quite strangely. ("dis" is a DataInputStream)

    Java Code:
    private void handleCommands() {
    		String[] command = null;
    		try {
    			command = dis.readUTF().split(" ");
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    				
    		if (command == null) return;
    		
    		if (command[0].equals("USDistance")){
    			System.out.println("Distance: "+command[1]);
    		}
    		
    	}
    When coded like this, it outputs the received distance. However, when I add a dis.available()>0 checker, like this:

    Java Code:
    private void handleCommands() {
    		String[] command = null;
    		try {
    			if (dis.available()>0){
    				command = dis.readUTF().split(" ");
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    				
    		if (command == null) return;
    		
    		if (command[0].equals("USDistance")){
    			System.out.println("Distance: "+command[1]);
    		}
    		
    	}
    Without any changes to anything outside of this function, it stops printing output. For some reason, dis.available() returns 0.

    Does someone know why?

    EDIT:

    If I remove dis.readUTF(), dis.available() still returns 0. I am shure that data IS sent. Shouldn't dis.available() be counting up as more data is being recieved?
    Last edited by werner291; 01-01-2012 at 07:34 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Data input stream behaving strangely

    The first version blocks until data is available, the second doesn't. I'm no expert in this, but I assume that you want to block until data becomes available and then read the data. If this blocks your program, then do it in a separate thread.

    To the experts: Corrections most welcome!

Similar Threads

  1. Should this input stream be closed?
    By barock in forum Advanced Java
    Replies: 8
    Last Post: 06-10-2011, 08:11 PM
  2. Readline from an input stream and nothing more.
    By couling in forum Advanced Java
    Replies: 19
    Last Post: 05-27-2011, 10:20 PM
  3. Input Stream as an Object?
    By sehudson in forum New To Java
    Replies: 1
    Last Post: 03-12-2011, 01:37 AM
  4. Input stream error
    By Johnny68 in forum New To Java
    Replies: 10
    Last Post: 08-05-2010, 07:20 PM
  5. Stream closed on a ClassLoader input
    By RaistlinMajeren in forum Advanced Java
    Replies: 15
    Last Post: 06-03-2010, 08:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •