Mobile Phone »» iCommand »» Lego NXT, is it possible?

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

Moderators: 99jonathan, roger, imaqine

Mobile Phone »» iCommand »» Lego NXT, is it possible?

Postby quack » Thu Oct 11, 2007 8:58 pm

is it possible to create a program that allows he user to control the Lego NXT with a mobile phone using iCommand and lejos? If so where should I start learning?
quack
Novice
 
Posts: 26
Joined: Sun Jul 08, 2007 9:03 pm

Postby lawrie » Fri Oct 12, 2007 6:19 pm

Here is an example Java midlet that runs on my Nokia phone. It returns the battery value from leJOS NXJ, but should work with the standard Lego firmware as well.

Code: Select all
import java.util.Vector;
import javax.bluetooth.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.io.*;
import java.io.IOException;
import java.io.*;


public class BlueDisc extends MIDlet implements CommandListener,
                    DiscoveryListener {
   
    private LocalDevice local = null;
    private DiscoveryAgent agent = null;
   
    private Vector devicesFound = null;
    private ServiceRecord[] servicesFound = null;
    private Alert a;
    StreamConnection con;

    public void startApp() {
       
        /*  Add your MIDlet specific code here.
         *  You probably want to show the user
         *  a welcome screen.
         *  The call to doDeviceDiscovery() is
         *  here for the example's sake. You
         *  should call doDeviceDiscovery() when
         *  the user actively asks for it.
         */
         
        //infoAlert("Discovering Services");

       doDeviceDiscovery();
    }
   
    public void pauseApp() {
    }
   
    public void destroyApp(boolean unconditional) {
    }
   
    public void commandAction(Command c, Displayable d) {
       
    }

    private void infoAlert(String msg) {
         a = new Alert("Info",msg, null, AlertType.INFO);
         a.setTimeout(Alert.FOREVER);
         Display.getDisplay(this).setCurrent(a);
    }

   
    public void deviceDiscovered(RemoteDevice remoteDevice,
                  DeviceClass deviceClass) {

        infoAlert("Device discovered: " + deviceClass.getMajorDeviceClass());
       
        devicesFound.addElement(remoteDevice);
    }
   
    public void inquiryCompleted(int param) {
       
        /*We should give the user an alert based on the
         *inquiry status code
         */
       
        // infoAlert("Inquiry Completed");

        switch (param) {
           
            case DiscoveryListener.INQUIRY_COMPLETED:

                //Inquiry completed normally, add appropriate code here
           
                // infoAlert("Inquiry Completed Normally");

                for(int i=0;i<devicesFound.size();i++) {

                  RemoteDevice rd = (RemoteDevice) devicesFound.elementAt(i);

                  //try {
                      //infoAlert("Searching for " + rd.getFriendlyName(false));
                  //} catch (IOException ioe) {}

                  doServiceSearch(rd);
                }

                break;
               
            case DiscoveryListener.INQUIRY_ERROR:

                //Error during inquiry, add appropriate code here

                infoAlert("Inquiry Completed with error");

                break;
               
            case DiscoveryListener.INQUIRY_TERMINATED:

                /*
       * Inquiry terminated, caused by agent.cancelInquiry()
       * Add appropriate code here
       */

                infoAlert("Inquiry Terminated");
               
                break;
        }
    }
   
    public void serviceSearchCompleted(int transID, int respCode) {
   
            switch(respCode) {
           
            case DiscoveryListener.SERVICE_SEARCH_COMPLETED:

              /*
       * Service search completed successfully
       * Add appropriate code here
       */

                 //infoAlert("Service completed OK");

                break;
               
            case DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE:

      // device not reachable, add appropriate code here

                infoAlert("Device not reachable");
            
                break;
               
            case DiscoveryListener.SERVICE_SEARCH_ERROR:

      // Error during service search, add appropriate code here

               infoAlert("Service Search Error");
               
                break;
               
            case DiscoveryListener.SERVICE_SEARCH_NO_RECORDS:

      // No records found, add appropriate code here

                infoAlert("No Services");
               
                break;
               
            case DiscoveryListener.SERVICE_SEARCH_TERMINATED:
           
      /*
       * Search terminated, caused by agent.cancelServiceSearch(..)
       * Add appropriate code here
       */

                infoAlert("Service Search Terminated");
             
                break;
        }
   
    }
   
    public void servicesDiscovered(int transID,
               ServiceRecord[] serviceRecord) {
                            
       //Services discovered, keep a reference to the ServiceRecord array

        //infoAlert("Services Discovered");
       
       servicesFound = serviceRecord;

        String url = serviceRecord[0].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);

        //infoAlert(url);
       
        try{

            byte[] b = new byte[4];
            byte[]  r = new byte[7];

            b[0] = 2;
            b[1] = 0;
            b[2] = 0;
            b[3] = 0x0B;


            con = (StreamConnection) Connector.open(url);

           
            OutputStream os = con.openOutputStream();
            os.write(b);
            os.flush();

            InputStream is = con.openInputStream();
            is.read(r);

            infoAlert("Battery: " + ((r[5] & 0xFF) + ((r[6] & 0xFF) << 8)));
           
            is.close();
            os.close();

         }
         catch(IOException e){
            infoAlert("" + e.getMessage());
         }

    }
   
    private void doDeviceDiscovery() {
       
        try {
            local = LocalDevice.getLocalDevice();
        }catch (BluetoothStateException bse) {
           
            // Error handling code here

            infoAlert("No local device");
        }
       
        agent = local.getDiscoveryAgent();

        // infoAlert((agent == null ? "No agent" : "Got agent"));
       
        devicesFound = new Vector();
       
        try {
           
            if(!agent.startInquiry(DiscoveryAgent.GIAC,this)) {
               
                //Inquiry not started, error handling code here

                infoAlert("Inqiry not started");
            }
        }catch(BluetoothStateException bse) {
           
            //Error handling code here

            infoAlert("Inquiry Exception");
        }
    }
   
    private void doServiceSearch(RemoteDevice device) {
       
       /*
        * Service search will always give the default attributes:
        * ServiceRecordHandle (0x0000), ServiceClassIDList (0x0001),
        * ServiceRecordState (0x0002), ServiceID (0x0003) and
        * ProtocolDescriptorList (0x004).
        *
        * We want additional attributes, ServiceName (0x100),
        * ServiceDescription (0x101) and ProviderName (0x102).
        *
        * These hex-values must be supplied through an int array
        */
       
       int[] attributes = {0x100,0x101,0x102};
       
       /*
        * Supplying UUIDs in an UUID array enables searching for specific
        * services. PublicBrowseRoot (0x1002) is used in this example. This
        * will return any services that are public browsable. When searching
        * for a specific service, the service's UUID should be supplied here.
        */
       
       UUID[] uuids = new UUID[1];
       //uuids[0] = new UUID(0x1002);
        uuids[0] = new UUID("1101",true); // Serial Port
       
       try {
         agent.searchServices(attributes,uuids,device,this);
      } catch (BluetoothStateException e) {

                      infoAlert("Service Search Exception");

                 infoAlert( "" + e);
         // Error handling code here
      }
    }
}

lawrie
leJOS Team Member
 
Posts: 921
Joined: Mon Feb 05, 2007 1:27 pm

Postby quack » Sat Oct 13, 2007 12:04 pm

but to run that program the NXT must have Lego firmware installed because of bluetooth recognition, am I right?

I was wondering if it was possible to make my own application for NXT to start bluetooth and make NXT bluetooth device available for my Nokia and then control it with my Nokia application...


(sorry for my english)
quack
Novice
 
Posts: 26
Joined: Sun Jul 08, 2007 9:03 pm

Postby lawrie » Sat Oct 13, 2007 9:10 pm

No, the leJOS NXJ menu supports Bluetooth commands including the Lego Communications Protocol that the standard firmware supports. The program I attached runs with the NXT running the leJOS NXJ menu.

If you run a program such as the example BTReceive on the NXT, it will accept connections and receive data. If you use Java streams, you need to add 2-byte headers (LSB, MSB) to packets that you send.
lawrie
leJOS Team Member
 
Posts: 921
Joined: Mon Feb 05, 2007 1:27 pm

Postby quack » Sun Oct 14, 2007 4:42 pm

I didn't undestand the part of the example code you gave where the Nokia gets the battery values from NXT... (all those strange bytes)

how and where can I learn more about that process? which values can I get from lego NXT and which ones can I send to it so that it could preform specific actions?

thanks a lot for your help and patience
quack
Novice
 
Posts: 26
Joined: Sun Jul 08, 2007 9:03 pm

Postby lawrie » Sun Oct 14, 2007 6:49 pm

The strange bytes are in the Lego documents LEGO MINDSTORMS NXT Communication Protocol and Direct Command that are in the the Bluetooth Development Kit and the Hardware Development Kit that can be downloaded from the the NXT'reme section of the official LEGO Mindstorms site. leJOS implements a lot but not all of these commands - see the lejos.nxt.LCP class.
lawrie
leJOS Team Member
 
Posts: 921
Joined: Mon Feb 05, 2007 1:27 pm

Postby quack » Sun Oct 14, 2007 9:50 pm

thanks a lot
quack
Novice
 
Posts: 26
Joined: Sun Jul 08, 2007 9:03 pm


Return to NXJ Software

Who is online

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

more stuff