If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

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

Help with Bluetooth Receive String

Hi,

 

I am trying to receive data from my paired device (BT - hyperterminal) and process it once a carriage return is detected '\r'

 

I am able to receive and display the data but for some reason each character is stored separetely even with my toString() efforts.

 

My code is as follows when there is incomming data:

 

 

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

DataProcess() first displays the value of _dataReceived then continues with the processing...

 

When I type anything into hyperterminal the dialog.Alert() from DataProcess returns the string in a column of char instead of single row.

 

T

E

S

T

instead of TEST

 

Any suggestions would be greatly appreciated...

 

Thanks,

David

Curve8330+JDE4.5(eclipse)

 

 

 

 

 

 

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

Re: Help with Bluetooth Receive String

Only had a very quick look at your code, but I suspect you assume in your dataReceived routine that there is only length bytes available. 

 

I belive there are at least that number available. so rather than:

 

port.read(_receiveBuffer, 0, length == -1 ? _receiveBuffer.length : length))

 

I would code a loop that read, single char at a time, until EOF (result = -1).

 

In that loop you can process the characters when you hit a "\n", or save them for next time.

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

Re: Help with Bluetooth Receive String

Thanks Peter.

 

I can now receive characters from hyperterminal.

 

I changed my code to:

 

try { // Read the data that arrived. if((len = _port.read())>0){ DataReadChar = (char)len; _data.append(DataReadChar); Dialog.alert("Data is "+_data); if(DataReadChar == '\r') { _dataReceived = _data.toString(); _dataReceived = _dataReceived.trim(); Dialog.alert("Data Received is: "+ _dataReceived); DataProcess(); _receiveBuffer = new byte[1024]; _data.delete(0, _data.length()); }

 

 

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

Re: Help with Bluetooth Receive String

Looks good to me.  Do you want to mark this Thread as solved?
Please use plain text.