BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 06-28-2011, 11:35 PM   #1 (permalink)
New Member
 
Join Date: Jun 2011
Model: 8520
PIN: N/A
Carrier: Programmig
Posts: 2
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Incorrect reading at public void dataReceived(int length)

Please Login to Remove!

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, any ideas?

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();
			}
			Dialog.inform(new String(buffer, 0, idx));
			ADCs[0].setText(new String(buffer, 0, idx));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
As you can see, I use a "#" like a stop caracter. I 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!!
Offline  
Old 06-29-2011, 03:56 AM   #2 (permalink)
New Member
 
Join Date: Jun 2011
Model: 9000
PIN: N/A
Carrier: Vodafone
Posts: 8
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Re: Incorrect reading at public void dataReceived(int length)

Are you sure that your method [puertoBT.read()] reads your buffer char by char? If it reads it byte by byte, each time you read one byte, you convert it into a char (which is normally 16 bits = TWO bytes). Maybe you could try with:
byte[] buffer = new byte[8];
...
and finally
Dialog.inform(new String(buffer))

--
Toumou

Last edited by toumou : 06-29-2011 at 04:14 AM.
Offline  
Old 06-29-2011, 04:17 PM   #3 (permalink)
New Member
 
Join Date: Jun 2011
Model: 8520
PIN: N/A
Carrier: Programmig
Posts: 2
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Re: Incorrect reading at public void dataReceived(int length)

Thanks!
My problem is solved!

I post my final code to help anyone with similar problems

Code:
//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);
		}
	});
}
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads for: Incorrect reading at public void dataReceived(int length)
Thread Thread Starter Forum Replies Last Post
JVM Error 104 Uncaught: NoClassDefFoundError vinaymsc Developer Forum 3 07-06-2010 10:28 AM
Exception ou erreur 104 lors d'exicution d'un programe/Exception or Error 104 when ex rahmazayoud Developer Forum 2 05-14-2010 01:33 PM
unable to display content check the code chevuru Developer Forum 0 03-22-2006 03:44 AM
7100I Big HELP whoknowswhat General Legacy Device Discussion 1 10-23-2005 03:45 PM
Need help with udp connection (through simulator) ramin Developer Forum 4 03-04-2005 04:45 AM





Copyright © 2004-2014 BlackBerryForums.com.
The names RIM © and BlackBerry © are registered Trademarks of BlackBerry Inc.