Problems downloading files from NXT

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

Moderators: roger, imaqine, 99jonathan

aFringeDweller
New User
Posts: 2
Joined: Fri Apr 06, 2012 4:54 pm

Problems downloading files from NXT

Postby aFringeDweller » Fri Apr 06, 2012 5:06 pm

Hi, I just got my NXT 2.0 kit last week and this is my first post.

I'm writing a PC java app too practice controlling the NXT remotely.
So far I can send commands and text to the brick through a DataOutputStream, but the next step is causing me some trouble.
I want to be able to select a file (on the NXT) and download it to my PC from inside my GUI. I've looked through the source code of NXJBrowse and tried experimenting with the ExtendedFileModel and its fetchFiles() method. But when I run the method, the PC app freezes up. If it had worked, I would have created a JTable with the ExtendedFileModel in the constructor. Is this the only way to access the contents of the file model?

Maybe I'm doing it all wrong? Please help.

The code that's causing me problems:

Code: Select all

NXTComm connection = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
NXTInfo device = new NXTInfo(2, "MyName", "MyAddress");
connection.open(device);
NXTCommand nxt = new NXTCommand(connection);
ExtendedFileModel filemodel = new ExtendedFileModel();
filemodel.fetchFiles(nxt);


Thanks,
Morten

Xfel
New User
Posts: 11
Joined: Mon Jan 30, 2012 3:29 pm

Re: Problems downloading files from NXT

Postby Xfel » Tue Apr 10, 2012 12:29 pm

Are you running a special program on the NXT or using a custom protocol? ExtendedFileModel is using the LCP protocol. It won't work with others.

aFringeDweller
New User
Posts: 2
Joined: Fri Apr 06, 2012 4:54 pm

Re: Problems downloading files from NXT

Postby aFringeDweller » Tue Apr 10, 2012 2:26 pm

Xfel wrote:Are you running a special program on the NXT or using a custom protocol? ExtendedFileModel is using the LCP protocol. It won't work with others.

Oh, I didn't realize that. No, the program on the NXT just a normal class that connects to the PC via Bluetooth and sends/receives data in streams. I'm not very experienced with streams though. How would I go about sending a file on the DataOutputStream?

Xfel
New User
Posts: 11
Joined: Mon Jan 30, 2012 3:29 pm

Re: Problems downloading files from NXT

Postby Xfel » Wed Apr 11, 2012 10:12 am

You will of course need to tell the PC that it's a File comming through, but to send just the data you could write:

Code: Select all

File file = ...

FileInputStream fis = new FileInputStream(file);
DataOutputStream dos = ... // wherever it comes from

final int BUFSIZE = 256; // can vary
byte[] buf = new byte[BUFSIZE];

int read = 0;
long fileSize = file.length();

dos.writeLong(fileSize);

while(true){
  read = fis.read(buf,0,BUFSIZE);
  if(read<0)break;
  dos.write(buf,0,read);
}


matthias
New User
Posts: 20
Joined: Thu Mar 22, 2012 11:44 am

Re: Problems downloading files from NXT

Postby matthias » Wed Apr 11, 2012 12:17 pm

Dont forget to flush


Return to “NXJ Software”

Who is online

Users browsing this forum: No registered users and 3 guests