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
New Contributor
Posts: 3
Registered: ‎06-29-2011
My Device: Curve 8520
My Carrier: Software Ing.
Accepted Solution

A read problem in dataReceived method

Hi all, I need your help, please.
I am trying to read some data from a bluetooth chip, but i can't get a good reading. I have reviewed my code a lot and I can not find the mistake. I post my code:

 

////////////////////////////////////////////////////////////
// Implementacion de BluetoothSerialPortListener
// /////////////////////////////////////////////////////////
public void dataReceived(int length) {
       char[] buffer = new char[8];
	int idx = 0;
	try {
		char c = (char) puertoBT.read();
		while (c != '#') {
			buffer[idx] = c;
			idx++;
			c = (char) puertoBT.read();
		}
		ADCs[0].setText(new String(buffer, 0, idx));
	} catch (IOException e) {
		e.printStackTrace();
	}
}

As you can see, I use a "#" like a stop caracter. In fact, the last char that the bluetooth chip send is "#". For example, if this send 1.23#, i read in the BlackBerry app: "23" or ".23" and somethimes "1.23". Why?

Thank you in advance!!

Developer
Posts: 232
Registered: ‎12-15-2008
My Device: 9700
My Carrier: Vodafone India

Re: A read problem in dataReceived method

Gud idea is to DEBUG ur code line by line...

 

U wil come to knw whr u missin.... code seems f9.

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: A read problem in dataReceived method

Welcome to the forums.

 

I agree with the other poster, debugging on device would be a good idea.  However this can be problematic for Bluetooth devices which are timing related.

 

My other concern however is this:

 

ADCs[0].setText(new String(buffer, 0, idx));

 

I thought a setText had to be done on the Event Thread, and from memory dataReceived is actually run on a separate background Thread, so I am surprised that this works.  I would replace this setText() with a

System.out.println(...);

and use on device debugging to check the output.

 

I have not seen a problem with dataReceived not reading all the data, however, from memory, I have had dataReceived called a number of times for what it effectively the same packet of data from the device.  Perhaps this is the issue here.  You could make the input buffer and the idx global to this routine, so that if you it gets called once for the first character, and then again for the rest, you will put them all in the same buffer.  Reset the buffer and idx when you get a #. 

 

Hopefully this makes sense. 

New Contributor
Posts: 3
Registered: ‎06-29-2011
My Device: Curve 8520
My Carrier: Software Ing.

Re: A read problem in dataReceived method

Gracias muchachos (Thanks guys )

My problem is solved!

 

I post my final code to help anyone with similar problems

 

//Fields
private byte[] buffer = new byte[64];
private String valStr = "";

// ////////////////////////////////////////////////////////////////////////
// Implementacion de BluetoothSerialPortListener
// ////////////////////////////////////////////////////////////////////////
public void dataReceived(int length) {
	try {
		int num = puertoBT.read(buffer, 0, length);
		String s = new String(buffer,0, num);
		valStr += s;
		if (valStr.endsWith("#")) {
			actualizarADC(valStr);
			buffer = new byte[64];
			valStr = "";
		}
	} catch (Exception e) {
		Dialog.alert("Error de lectura de datos. " + e.toString());
	}
}

public void actualizarADC(final String valor) {
	UiApplication.getUiApplication().invokeLater(new Runnable() {
		public void run() {
			ADCs[0].setText(valor);
		}
	});
}

 peter_strange, thank you for your ideas (y)

Super Contributor
Posts: 253
Registered: ‎08-19-2010
My Device: BB10, Playbook, 9900, 9800, 9700, 9550, 9530, 9500, 8700.
My Carrier: TT

Re: A read problem in dataReceived method

BluetoothDemo is my solution.

I import BB example/BluetoothDemo.

And then, change Hi There to the device name. 

 

Only change APP Screen. 

 

int numServices = _portInfo.length;

// Create a MenuItem for each Bluetooth device we can connect to.
if (numServices>0)
_rtf = new RichTextField(String.valueOf(numServices)+" device, Name: "+_portInfo[0].getDeviceName(), RichTextField.NON_FOCUSABLE);
else
_rtf = new RichTextField("No devices in pairing list", RichTextField.NON_FOCUSABLE);
add(_rtf);
//Create a MenuItem for each Bluetooth device we can connect to.
DeviceMenuItem deviceItems[] = new DeviceMenuItem[numServices];
for (int count = 0; count < numServices; count++) {
deviceItems[count] = new DeviceMenuItem("Connect to: " + _portInfo[count].getDeviceName(), _portInfo[count]);
addMenuItem(deviceItems[count]);
}

 

You can also download my sample code, which bases on bluetoothDemo, from sourceforge.

https://sourceforge.net/projects/blackberry71blu/

 

I can begin to develop coding with the BT sensor.

 

Cheers,

-Fight with BB10