Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Developer
Posts: 264
Registered: ‎10-05-2010
My Device: Not Specified

Trouble with Bluetooth I/O?

[ Edited ]

Can someone please explain why this code does not work?

 

I am writing an app that communicates with a bluetooth peripheral. That is working. The communication already hapens on a separate thread (not shown). Then when the desired data is received in that thread, it does a callback to resultReady in the class below.

 

Typically, the data arrives back almost immediately. Like in milliseconds. But when I do it from the class below, the callback never gets called until after the getStringValue routine completes. Therefore, it always looks like it is timing out. If I comment out the timout calculation, it never completes. As soon as it times out, the callback is immediately run.

 

 

 

public class Command implements ResultListener {
	String cmd;
	private BluetoothClient btCon;
	boolean ready = false;
	private String result;
	
	public Command (String cmd){
		this.cmd=cmd;
		btCon = BluetoothClient.getInstance();
		btCon.addListener(this);
		
		System.out.println("[BTCL] Command init: ready="+ready);
	}
	
	public String getStringValue(){
		result = "TIMEOUT";
		ready = false;
		System.out.println("[BTCL] Command getStringValue sent: ready="+ready);
		btCon.sendCommand(cmd);

		long st = System.currentTimeMillis();
		while(!ready){				
			System.out.println("[BTCL] Command getStringValue waiting: "+(System.currentTimeMillis()-st));
			if((System.currentTimeMillis()-st) > 5000){
				System.out.println("[BTCL] Command getStringValue timeout: ready="+ready);
				break;
			}
		}
		return result;
	}

	public void resultReady(String cmd) {
		if(cmd.equalsIgnoreCase(this.cmd)){
			result = btCon.getHash(cmd);
			ready=true;
			System.out.println("[BTCL] Command resultReady: "+cmd+"="+result);
		}
	}
}

 

Developer
Posts: 264
Registered: ‎10-05-2010
My Device: Not Specified

Re: Trouble with threading?

What I really want is just a blocking call so I can just send the command on the output stream, then return the value from the input stream. All this messing around with threads and listeners is be a pain. I tried using a bluetooth input and output stream but could never get that working.

 

i.e.

 

doCommand(){

  os.send("ATZ");

  result = is.read(); // result="OK"

  return result; 

}

Developer
Posts: 264
Registered: ‎10-05-2010
My Device: Not Specified

Re: Trouble with threading?

Maybe I should ask the question in a different way...

 

I need to talk to this Bluetooth device. It uses a simple query/response ascii interface. e.g. Send "ATZ\r", it responds "OK>". The response happens almost instantly - millieconds not seconds.

 

I think I need to use btspp protocol for this. It should be really easy...

 

I tried using streaming I/O as per one of the BB examples. But no matter what I did, I got exceptions trying to make the connection.

 

I tried using the RIM APIs with a listener, and it works great - except that I now have async communications where I really needed a blocking call. 

 

I'm been running in circles trying to get this to work. 

 

Stepping back to the drawing board, How should I be approaching this? 

 

I'm not the first to have to do this... 

 

Thanks again in advance...