sending strings with bluetooth

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

sending strings with bluetooth

Postby slippy » Mon Mar 01, 2010 7:59 pm

hi all,

I am having trouble sending a string over bluetooth.
here is my code

device
String a = "hello";
output.writeChars(a);
output.flush();

computer
String otp = input.readUTF();

The problem is the string that is received is completely different to the string that is sent.

I think readUTF is wrong but not really sure what to use instead.

thanks
slippy
Novice
 
Posts: 32
Joined: Fri Oct 02, 2009 9:50 am

Re: sending strings with bluetooth

Postby Shawn » Mon Mar 01, 2010 10:41 pm

How about this?

Code: Select all
 
device
String a = "hello";
output.writeUTF(a);
 



http://lejos.sourceforge.net/nxt/nxj/api/index.html

see java.io
User avatar
Shawn
Advanced Member
 
Posts: 723
Joined: Wed Sep 12, 2007 4:59 am
Location: Tokyo

Postby slippy » Mon Mar 01, 2010 11:07 pm

thanks for your reply.

I did try that and it chucked a exception which I added a try/catch round which said "not yet implemented" when thrown.

I checked my os version im running 0.85 which i think is the latest version.
slippy
Novice
 
Posts: 32
Joined: Fri Oct 02, 2009 9:50 am

Postby Shawn » Tue Mar 02, 2010 1:24 am

Well I think you could :

1)convert the string to a byte array --byte[] data
2) get the data.length().
3) write the lenght to your outputstream
4) write the data

(send data)

5) read the length and create an array of that length
6) read that many bytes into your array
7) make a string from the byte array you just created

voila presto magic
User avatar
Shawn
Advanced Member
 
Posts: 723
Joined: Wed Sep 12, 2007 4:59 am
Location: Tokyo

Postby slippy » Tue Mar 02, 2010 9:41 am

I thought that bytes could only hold numbers ?
I need to hold hexadecimal
slippy
Novice
 
Posts: 32
Joined: Fri Oct 02, 2009 9:50 am

Postby Shawn » Tue Mar 02, 2010 12:11 pm

slippy wrote:I thought that bytes could only hold numbers ?
I need to hold hexadecimal


Yes bytes only hold numbers, but no it'll work because you if mark how long each byte array is and use that to know how much data to put into the array, you can send as much text as you want via byte arrays. Basically, it is what pascal does for utf8 I believe instead of using a null terminating character that is in c I think.

hexadecimal --> string --> byte[]
Code: Select all
getBytes

public byte[] getBytes(String charset)

    Get bytes in US Acsii

    Parameters:
        charset - ignored
    Returns:
        the ascii bytes




byte[] --->string -->hexadecimal

Code: Select all
public String(byte[] b)

    Create a String from a byte array

    Parameters:
        b - the byte array


Note I'm leaving hexadecimal conversion to you but from a string that is no problem I suspect
User avatar
Shawn
Advanced Member
 
Posts: 723
Joined: Wed Sep 12, 2007 4:59 am
Location: Tokyo

Postby burti » Tue Mar 02, 2010 3:13 pm

the issue has been discussed.
see http://lejos.sourceforge.net/forum/viewtopic.php?t=1332 for more details and my solution
burti
Novice
 
Posts: 61
Joined: Thu Jun 25, 2009 11:41 pm

Postby slippy » Wed Mar 03, 2010 8:43 am

Thanks for your help
slippy
Novice
 
Posts: 32
Joined: Fri Oct 02, 2009 9:50 am


Return to NXJ Software

Who is online

Users browsing this forum: Google [Bot] and 4 guests

more stuff