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
Developer
dmravyan
Posts: 25
Registered: ‎01-29-2009
My Device: Curve 8330
Accepted Solution

extra bytes when reading bluetooth _dataReceived...

I am sending a stream of bytes via a bluetooth module to the blackberry.

I am then taking the substrings from the data received.

 

For some reason I am getting 3 extra bytes of data from somewhere....

I have setup a dialog.alert which displays the data stream correctly after it is received by the dataReceived(int length) method.

 

 

Stream sent:

 

"STA152535455565"

 

 

Dialog.alert(_dataReceived); // equal to STA152535455565

 

 

this is where it gets weird:

 

 

_lblS1.setText(_dataReceived.substring(2, 4)); //Returns: ST   should be 15 

_lblS2.setText(_dataReceived.substring(4, 6)); //Returns: A1   should be 25

etc...

 

when I change the substring(2,4) to substring(5,7) it returns 15

 

Any ideas?

 

Thanks!

David

Curve8300+JDE4.5(eclipse)

 

 

 

 

 

 

 

 

 

 

Please use plain text.
Developer
jonberry
Posts: 484
Registered: ‎07-17-2008
My Device: Not Specified

Re: extra bytes when reading bluetooth _dataReceived...

A little more code might be helpful...

 

How are you getting data into _dataRecevied?

Please use plain text.
Developer
dmravyan
Posts: 25
Registered: ‎01-29-2009
My Device: Curve 8330

Re: extra bytes when reading bluetooth _dataReceived...

Hi,

 

I am getting the data into _dataReceived based on the Bluetooth example provided by RIM.

 

// Invoked when data has been received. public void dataReceived(int length) { int len; try { // Read the data that arrived. if((len = _port.read(_receiveBuffer, 0, length == -1 ? _receiveBuffer.length : length)) != 0) { if(len == 1 && _receiveBuffer[0] == '\r') { _receiveBuffer[1] = '\n'; ++len; } _data.append(new String( _receiveBuffer, 0, len)); _dataReceived = _data.toString(); } DataProcess(); } catch(IOException ioex) { // Catch and re-throw the exception. throw new RuntimeException(ioex.toString()); } }

 

 

 

In DataProcess I placed the substring code.

 

 

 

 

 

Please use plain text.
Developer
peter_strange
Posts: 19,602
Registered: ‎07-14-2008
My Device: Not Specified

Re: extra bytes when reading bluetooth _dataReceived...

Be aware that the length provided as the parameter to dataReceived is a minimum, there could be more data by the time you get to do your read.  However I think your code copes with that.

 

What is actually in the extra bytes you receive?

 

Have you considering dumping _receiveBuffer out in integer form - for example to see if there are some funny characters in there that you won't see on your 'display'?

Please use plain text.
Developer
dmravyan
Posts: 25
Registered: ‎01-29-2009
My Device: Curve 8330

Re: extra bytes when reading bluetooth _dataReceived...

I played around with the code some more. I am starting to think the first two characters are blank for some reason.

 

Stream Sent:

 

MON182838485868

 

Code dealing with stream:

Dialog.alert(_dataReceived);           //Shows MON182838485868....

_lblS1.setText(_dataReceived.substring(0, 5));             //      S1 shows MON   <-- and some extra spacing after  

 _lblS2.setText(_dataReceived.substring(7, 9));             //      S2 shows 28  

_lblS3.setText(_dataReceived.substring(0));                //       S3 shows MON182838485868

_lblS4.setText(_dataReceived, 0, 3);                           //       S4 shows M

_lblS5.setText(_dataReceived.getBytes());                   //      S5 shows MON182838485868 

_lblS6.setText(_dataReceived);                                 //        S6 shows " " blank... getBytes changed _dataReceived?

 

 

Thanks,

David

 

 

Please use plain text.
Developer
dmravyan
Posts: 25
Registered: ‎01-29-2009
My Device: Curve 8330

Re: extra bytes when reading bluetooth _dataReceived...

For now I have fixed the issue by simply removing the first two characters from the _dataReceived

 

_dataReceived = _dataReceived.substring(2,_dataReceived.length());

 

I placed the code right after _dataReceived is first written....

 

I think I might have a problem with my other device sending a couple of blank characters at start.

 

Thanks for your help...

Please use plain text.
Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: extra bytes when reading bluetooth _dataReceived...

Some extra blank space can be added to the begin and also can be added at the end of the receive data. I don't know the reason.

 

But i think in this case it would be more appropriate to use:

     _dataReceived = _dataReceived.trim();

 

 

Regards

Bikas 

Please use plain text.
Developer
dmravyan
Posts: 25
Registered: ‎01-29-2009
My Device: Curve 8330

Re: extra bytes when reading bluetooth _dataReceived...

Thanks! much better!!

 

Please use plain text.