05-28-2009 04:39 PM
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)
Solved! Go to Solution.
05-28-2009 08:13 PM
A little more code might be helpful...
How are you getting data into _dataRecevied?
05-29-2009 09:57 AM
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.
05-29-2009 11:45 AM
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'?
05-29-2009 01:28 PM
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
05-29-2009 02:28 PM
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...
05-29-2009 02:48 PM
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
05-29-2009 02:56 PM
Thanks! much better!!