Critical Bluetooth Problem

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

Moderators: 99jonathan, roger, imaqine

Critical Bluetooth Problem

Postby Hattusas » Sun Nov 11, 2007 8:54 pm

Hi everyone.
I have been trying to move my BT communication project into my mobile phone.
I seeked and succesfully found my NXT device but the ServiceSearch of javax.bluetooth doesn't find any.
After that I noticed something on my NXT.
NXT can't find any devices when asked for searh from the bluetooth menu of lejos.
I can't figure out why it is so.When I reinstall the lego framework it can both my PC with bluetooth dongle and my mobile phones.

I have the latest beta version of lejos installed in my NXT.

Why can't my NXT find anything? Urgently seeking for help.Thank you.
Today is the first day of rest of your life.
Hattusas
New User
 
Posts: 12
Joined: Sat Oct 06, 2007 11:24 am

Postby lawrie » Mon Nov 12, 2007 11:15 pm

I don't know why your ServiceSearch does not find anything. It may be that you need to pair the NXT first. I can connect to the NXT successfully from my mobile phone using the Lego mobile phone application, or from my own simple midlet, which I have posted on the forums.

The Bluetooth menu on the NXT currently limits the devices searched for to other NXTs (by setting Class of Device), so will not find other devices.
lawrie
leJOS Team Member
 
Posts: 919
Joined: Mon Feb 05, 2007 1:27 pm

Postby Hattusas » Wed Nov 14, 2007 7:34 pm

Lawrie can you send the URL where you had posted your MIDLET sources?
I couldn't find it :roll:

I will have a look at your code and check it works on my NXT or not.

Also one more question:
In your MIDLET application how do you connect to NXT? By using the classes of LEJOS ? Icommand? or javax.bluetooth classes?

Thanx for your help.
Today is the first day of rest of your life.
Hattusas
New User
 
Posts: 12
Joined: Sat Oct 06, 2007 11:24 am

Postby lawrie » Fri Nov 16, 2007 10:37 pm

It uses javax.bluetooth. Here is the source of a very simple midlet:

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: 919
Joined: Mon Feb 05, 2007 1:27 pm


Return to NXJ Software

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 5 guests

more stuff