How to send Integers from an Android-Phone to a NXT?

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

Moderators: 99jonathan, roger, imaqine

How to send Integers from an Android-Phone to a NXT?

Postby The_M » Sun Jan 05, 2014 10:08 pm

Hey there,
I'm new here and I hope you can help me to get my Android-App on my Samsung Galaxy S3 (v.4.1.2) to work.
The NXT's Firmware ist Lejos 0.9.1!

In my last project, I connected succesfully from the PC to the NXT and sent some data by using "Bluecove.jar".
Now I'm trieing to do the same with the one difference that I want to send some data from the Smartphone to the NXT. Apparently, the "Bluecove.jar" is not working on Android, so I decided to search on the Internet. I found out that i had to use "Bluecove.jar" and "Bluecove-gpl.jar" as well on Android. Unfortunately i got an error message which told me there were some natives libraries which would not run on the device. So i looked around for possibilities to connect from Android to the NXT without using these libraries.
After all, i am trying to get that work by using a bluetooth socket. Both devices are already paired and succesfully connected without errors. But when i try to send data through the DataInput-/DataOutputStreams, the NXT shows Errormessages on its display:

class 95
at: 225:17
at: 226:2
at: 51:9
at: 50:52
at: 49:4

The Code on the Smartphone-App:

The method sendenToNXT() is called by pressing a button...
Code: Select all
BluetoothSocket bSocket;
BluetoothDevice nxt;

OutputStreamWriter bOut;
BufferedReader bIn;
DataOutputStream dos;

final String nxtAddress = "00:16:53:15:A5:B5";

@Override
   public void run()
        {
      initBluetooth();
      while (true)
              {
          //....
      }
   }

void initBluetooth()
   {      
      adapter = BluetoothAdapter.getDefaultAdapter();
      nxt = adapter.getRemoteDevice(nxtAddress);
      try {
         bSocket = nxt.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
         bSocket.connect();
         bOut = new OutputStreamWriter(bSocket.getOutputStream());
         bIn = new BufferedReader(new InputStreamReader(bSocket.getInputStream()));
         dos = new DataOutputStream(bSocket.getOutputStream());
      } catch (IOException e) {
         e.printStackTrace();
      }
      System.out.println("Bluetooth-Verbindung hergestellt!");
   }

void sendenToNXT(int toSend)
   {
      try {
         dos.writeInt(toSend);
         dos.flush();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }


The NXT-Code:
Code: Select all

TouchSensor bsensor = new TouchSensor(SensorPort.S3);

NXTConnection btc;
DataOutputStream dos;
DataInputStream dis;

public static void main(String[] args) {
      new RobbiRemoted();
   }
   
   public RobbiRemoted() {
      verbinden();
      while (bsensor.isPressed()==false)
      {
         
         try {
            nachricht(dis.readInt());
         } catch (IOException e) {
            e.printStackTrace();
         }
         
      }
   }

void verbinden()
   {
      LCD.drawString("Warten auf..", 3, 4);
      btc = Bluetooth.waitForConnection();
      dos = btc.openDataOutputStream();
      dis = btc.openDataInputStream();
      LCD.clear();
      LCD.drawString("Verbindung hergestellt", 1, 5);
   }

void nachricht(int empfangen) {
      LCD.clear();
      LCD.drawInt(empfangen ,5,2);
      LCD.refresh();
   }


I hope nothing is missing. I don't know why it does not work, maybe you have some ideas.

I appreciate your help and hope that you are able to enlighten me my mistakes.
______________
Greetings The_M
Last edited by The_M on Mon Jan 06, 2014 12:26 am, edited 1 time in total.
The_M
New User
 
Posts: 8
Joined: Sun Jan 05, 2014 7:14 pm

Re: How to send Integers from an Android-Phone to a NXT?

Postby gloomyandy » Sun Jan 05, 2014 10:28 pm

Hi,
can't tell you exactly what is wrong but you can find out a lot more about what is happening by decoding the exception data and stack trace produced on the NXT. To do this you need to look at the output from the leJOS linker which will allow you to convert the numbers displayed on the screen into class names. To see how to do this please take a look at the leJOS tutorial and in particular the debugging section:
http://www.lejos.org/nxt/nxj/tutorial/E ... ugging.htm

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3890
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: How to send Integers from an Android-Phone to a NXT?

Postby The_M » Sun Jan 05, 2014 11:26 pm

Hey, i appreciate your fast help:)

I tried to debug now via Bluetooth, but i am getting following exception:

Code: Select all
Exception in thread "main" java.lang.NoSuchMethodError: lejos.nxt.NXT.getUserPages()I
   at lejos.nxt.Flash.<clinit>(Flash.java:16)
   at lejos.nxt.SystemSettings.<clinit>(SystemSettings.java:32)
   at lejos.nxt.comm.NXTCommDevice.loadSettings(NXTCommDevice.java:165)
   at lejos.nxt.comm.NXTCommDevice.<clinit>(NXTCommDevice.java:20)
   at lejos.nxt.comm.RConsole.openBluetooth(RConsole.java:227)
   at robbipc.DebugRobbi.<init>(DebugRobbi.java:13)
   at robbipc.DebugRobbi.main(DebugRobbi.java:9)


same for USB:
Code: Select all
Exception in thread "main" java.lang.NoSuchMethodError: lejos.nxt.NXT.getUserPages()I
   at lejos.nxt.Flash.<clinit>(Flash.java:16)
   at lejos.nxt.SystemSettings.<clinit>(SystemSettings.java:32)
   at lejos.nxt.comm.NXTCommDevice.loadSettings(NXTCommDevice.java:165)
   at lejos.nxt.comm.NXTCommDevice.<clinit>(NXTCommDevice.java:20)
   at lejos.nxt.comm.RConsole.openUSB(RConsole.java:216)
   at robbipc.DebugRobbi.<init>(DebugRobbi.java:13)
   at robbipc.DebugRobbi.main(DebugRobbi.java:9)


My debug-code:
Code: Select all
import lejos.nxt.comm.RConsole;

public class DebugRobbi {

   
   public static void main(String[] args) {
      new DebugRobbi();
   }
   
   public DebugRobbi() {
      RConsole.openBluetooth(10000);
   }

}


I have no idea how to fix it, at first i thought i've forgotten to add libraries, but when i added all of them, the error is still the same...

____________________________________________________________________________________________________________________
EDIT:
I looked around in my folders and found "NXJchartingLogger.bat". With that file i am able to connect to the NXT. When i started the NXT-program and my smartphone connected to the NXT, the .bat file gave me the following:
Code: Select all
Connect
DataLogger-log file is:C:\Windows\System32\NXTData.txt
folder set to "C:\Users\START\Desktop"
LoggerProtocolManager-startLogging(): EOFException in getBytes(4): java.io.EOFException: getBytes: is.read(): java.io.EOFException: unable to read reply packet length
DataLogger-!** dataInputStreamEOF from NXT

Maybe it helps you a bit, my smartphone didnt send anything, but tried to connect to the NXT!
Hope, we can figure out the problem...
The_M
New User
 
Posts: 8
Joined: Sun Jan 05, 2014 7:14 pm

Re: How to send Integers from an Android-Phone to a NXT?

Postby gloomyandy » Mon Jan 06, 2014 2:29 am

I think you need to take a step back. Go back to your original code and simply use the output from the leJOS linker to identify the exception and the method that is throwing it.

As to your two errors that looks like you are trying to run code built for the NXT on a PC, you can't do that. In particular you must not include the leJOS classes.jar in a program that will run on the PC.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3890
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: How to send Integers from an Android-Phone to a NXT?

Postby The_M » Mon Jan 06, 2014 1:17 pm

Good morning,

i am still thankful to get a help so fast!

You were right! Your hint was absolutely right! I just misunderstood the explanation how to use the RConsole..

So i put RConsole.open(); as nearly the first line in my NXT-programm.. I succesfully opened the RConsole, but when i started to send via the Smartphone, the Errormessage was just shown on the nxt-display as before.

How do i get the decompiled exception?

I managed to print the message, which was shown on the NXT-dispaly into the RConsoe on the pc, but it didn't give me newer informations :/
What am i doing wrong?
The_M
New User
 
Posts: 8
Joined: Sun Jan 05, 2014 7:14 pm

Re: How to send Integers from an Android-Phone to a NXT?

Postby gloomyandy » Mon Jan 06, 2014 1:24 pm

You don't need to use RConsole, just lookup the numbers in the linker output file. The steps are very simple and explained here:
http://www.lejos.org/nxt/nxj/tutorial/E ... ugging.htm

If you want to use RConsole then again the steps are all described in the above tutorial. What is it that you do not understand, have you actually followed all of the steps (including building your program from the command line and running the console tool with the required parameters and .nxd file)?
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3890
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: How to send Integers from an Android-Phone to a NXT?

Postby The_M » Mon Jan 06, 2014 2:39 pm

It took a long time, but finally i succeeded:
Code: Select all

The class number 95 refers to:
  lejos.nxt.comm.NXTCommDevice (NXTCommDevice.java)

The method number 225 refers to:
  java.io.DataInputStream.readByte0()I (DataInputStream.java)

PC 17 refers to:
  line 20 in DataInputStream.java
The_M
New User
 
Posts: 8
Joined: Sun Jan 05, 2014 7:14 pm

Re: How to send Integers from an Android-Phone to a NXT?

Postby gloomyandy » Mon Jan 06, 2014 4:59 pm

Those numbers do not look correct to me. In particular the class should refer to an exception of some sort. Are you sure you are using the numbers generated by executing the code you have the .nxd file for? If you have re-compiled and relinked the program then the numbers may have changed so make sure you run the actual code and use the .nxd file for that code. It really is not that hard once you get the hang of it all!
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3890
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: How to send Integers from an Android-Phone to a NXT?

Postby The_M » Mon Jan 06, 2014 6:27 pm

I report what i did here:
- The NXT shows:
Code: Select all
class 95
at: 225:17
at: 226:2
at: 51:9
at: 50:52
at: 49:4

-As instructed by the tutorial, i typed the following in my cmd:
Code: Select all
C:\Program Files (x86)\LEGO Software\leJOS NXJ\bin>nxjc RobbiRemoted.java
C:\Program Files (x86)\LEGO Software\leJOS NXJ\bin>nxjlink -o RobbiRemoted.nxj -
od RobbiRemoted.nxd RobbiRemoted
C:\Program Files (x86)\LEGO Software\leJOS NXJ\bin>nxjdebugtool -di RobbiRemoted
.nxd -c -m 95 225 17

The class number 95 refers to:
  lejos.nxt.comm.NXTCommDevice (NXTCommDevice.java)

The method number 225 refers to:
  java.io.DataInputStream.readByte0()I (DataInputStream.java)

PC 17 refers to:
  line 20 in DataInputStream.java


Am i doing something wrong?
The_M
New User
 
Posts: 8
Joined: Sun Jan 05, 2014 7:14 pm

Re: How to send Integers from an Android-Phone to a NXT?

Postby gloomyandy » Mon Jan 06, 2014 9:57 pm

Have you re-run the program after you linked it using the command line? I would have expected the numbers on the display to have changed. You need to run the .nxj file created by the command line tools for the output on the screen to match the debug file created by the linker and compiler. The output you have there really does not make sense. Have you looked at the output from the linker in verbose mode (again see the tutorial), that should show you the mapping of class numbers to class names.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3890
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: How to send Integers from an Android-Phone to a NXT?

Postby The_M » Mon Jan 06, 2014 10:30 pm

I did what you said and got a new message from the NXT but just in the console:
Code: Select all
class 116
 at: 306:17
 at: 307:2
 at: 57:16
 at: 56:52
 at: 55:5


But when I try to use the debugtool as before:
Code: Select all
class 116
 at: 306:17
 at: 307:2
 at: 57:16
 at: 56:52
 at: 55:5
C:\Program Files (x86)\LEGO Software\leJOS NXJ\bin>nxjc RobbiRemoted.java
C:\Program Files (x86)\LEGO Software\leJOS NXJ\bin>nxjlink -o RobbiRemoted.nxj -
od RobbiRemoted.nxd RobbiRemoted
C:\Program Files (x86)\LEGO Software\leJOS NXJ\bin>nxjdebugtool -di RobbiRemoted
.nxd -c -m 116 306 17
java.lang.IllegalArgumentException: class number is out of range
        at lejos.pc.tools.NXJDebugTool.doClass(NXJDebugTool.java:129)
        at lejos.pc.tools.NXJDebugTool.start(NXJDebugTool.java:63)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at lejos.pc.tools.ToolStarter.startTool(ToolStarter.java:31)
        at lejos.pc.tools.NXJDebugTool.main(NXJDebugTool.java:14)
C:\Program Files (x86)\LEGO Software\leJOS NXJ\bin>


I doesn't understand that :/ I'm not sure if i performed your advice and the tutorial right...
The_M
New User
 
Posts: 8
Joined: Sun Jan 05, 2014 7:14 pm

Re: How to send Integers from an Android-Phone to a NXT?

Postby gloomyandy » Mon Jan 06, 2014 11:02 pm

The steps should be....
1. Compile and link your program with debug using nxjc and nxjlink (use the command lines you have used already). I would also add -v to the nxjlink command to get a verbose listing and redirect this to a file using > filename.
2. Download the .nxj file from the above. make a note of the values displayed on the screen.
3. Either use the nxjdebugtool to decode them, or if that does not work look up the values manually using the verbose listing created in step 1.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3890
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: How to send Integers from an Android-Phone to a NXT?

Postby The_M » Mon Jan 06, 2014 11:35 pm

I succesfully did step one and i now have a list with classes and methods!

If I understand your second step right, i have to load the .nxj file to my NXT. I did that by using nxjupload as shown in my last post.
When I start it on the NXT, it shows "Remote Console..." on the display. So i typed the following in the console:
Code: Select all
C:\Program Files (x86)\LEGO Software\leJOS NXJ\bin>nxjconsole -di RobbiRemoted.n
xd
Debug attached
Found NXT: NXT 00165315A5B5
Connected to NXT 00165315A5B5
Console open
class 116
 at: 306:17
 at: 307:2
 at: 57:16
 at: 56:52
 at: 55:5

C:\Program Files (x86)\LEGO Software\leJOS NXJ\bin>nxjdebugtool -di RobbiRemoted
.nxd -c -m 116 306 17

The class number 116 refers to:
  java.io.EOFException (EOFException.java)

The method number 306 refers to:
  java.io.DataInputStream.readByte0()I (DataInputStream.java)

PC 17 refers to:
  line 20 in DataInputStream.java


I didn't need the list to decode it, now you see the Exception, thank you very much helping me out to this point!!
I hope we can solve the problem :)
The_M
New User
 
Posts: 8
Joined: Sun Jan 05, 2014 7:14 pm

Re: How to send Integers from an Android-Phone to a NXT?

Postby The_M » Sat Jan 11, 2014 12:02 pm

Good day!
I appreciate your help with debugging my NXT, even if i had to solve the problem with the connection between Android and NXT on my own.

Anyway, for them who are interested in the working code:

NXT-Code:

Code: Select all

NXTConnection btc;
InputStream is;
DataOutputStream dos;
DataInputStream dis;

int recieve()
{   
        int recieved = 0;
   try {
      recieved = dis.readInt());
   } catch (IOException e) {
      e.printStackTrace();
   }
          return recieved;
}   

void connect()
{
        btc = Bluetooth.waitForConnection();
   btc.setIOMode(NXTConnection.RAW);
   is = btc.openInputStream();
   dos = btc.openDataOutputStream();
   dis = new DataInputStream(is);
}


and the Android-App-Code:

Code: Select all

BluetoothAdapter adapter;
BluetoothSocket bSocket;
BluetoothDevice nxt;
NXTConnector con;
OutputStream bOut;
DataOutputStream dbos;

void senden(int toSend)
{
   try {
      dbos.writeInt(toSend);
      dbos.flush();
   } catch (IOException e) {
      e.printStackTrace();
   }
}

void connect()
{
        adapter = BluetoothAdapter.getDefaultAdapter();
   nxt = adapter.getRemoteDevice(nxtAddress);
   try {
      bSocket = nxt.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
      bSocket.connect();
      bOut = bSocket.getOutputStream();
      bIn = bSocket.getInputStream();
      dbos = new DataOutputStream(bOut);
   } catch (IOException e) {
      e.printStackTrace();
   }
}


Greetings from Germany
The_M
The_M
New User
 
Posts: 8
Joined: Sun Jan 05, 2014 7:14 pm


Return to NXJ Software

Who is online

Users browsing this forum: Baidu [Spider] and 3 guests

more stuff