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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Highlighted
Developer
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)

 

 

 

 

 

 

Developer
Posts: 19,636
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.

Developer
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()); }

 

 

Developer
Posts: 19,636
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?