You Are Here:

Community: Developer Discussion Boards

#1 Old deviceDiscovered Not Discovering - 2007-07-21, 20:33

Join Date: Feb 2007
Posts: 29
adamzieba
Offline
Registered User
I am trying to connect to a bluetooth Nokia ld3w GPS. I cannot seem to even discover the device.

I have 2 classes GPS (which is a midlet) and GPSConnect
What is the best way to debug when using bluetooth as an input method (its a bit hard to when I can only really debug on the phone (E50) ). Im gonna go buy a bluetooth dongle asap, is there anything specific I should not buy .

GPS Midlet
Code:
package Finder;
import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;


public class GPS extends MIDlet implements CommandListener

{
    
    public GPS() throws IOException {
        initialize();
    }
   // private Command UpdateGPSListCommand ;
    private Command GPSListUpdateCommand ;
    protected static List GPSListReturnDevice ; 
    private Command exitCommand;                     
    private Command okCommandStartDeviceSearch;
    private Image image1;
    private Command itemCommand1;
    private Command updateGPSListCommand;                   
    //construct a GPSConnect object to establish a connection with the device. 
    private GPSConnect dC = new GPSConnect(); //dC = deviceConnect
                     


   public void commandAction(Command command, Displayable displayable) 
   {
        if (displayable == GPSListReturnDevice) 
        {
            if (command == exitCommand) 
            {
                exitMIDlet();
            }
        }
    
        // if the user has selected to start a search for new devices.
        if (command == okCommandStartDeviceSearch)
        {
            discoverDevices();
        }
    
  
    //if we are selecting the Friendly Device name
        if(command == GPSListReturnDevice.SELECT_COMMAND) 
        {
            //then start searching for services for the selected device (GPSList..getSelectedIndex() is the index to be sent to the method startServiceSearch in order to retrieve the bluetooth address.)
            dC.startServiceSearch(GPSListReturnDevice.getSelectedIndex());
        }
   
   } 
    
    

   /** This method initializes UI of the application.                        
    */
   private void initialize() {                      
             getDisplay().setCurrent(get_GPSListReturnDevice());  
                      
        // Insert post-init code here
   }                     
    
    /**
     * This method should return an instance of the display.
     */
    public Display getDisplay() {                         
        return Display.getDisplay(this);
    }                        
    
    /**
     * This method should exit the midlet.
     */
    public void exitMIDlet() {                         
        getDisplay().setCurrent(null);
        destroyApp(true);
        notifyDestroyed();
    }                        
    /** This method returns instance for exitCommand component and should be called instead of accessing exitCommand field directly.                        
     * @return Instance for exitCommand component
     */
    public Command get_exitCommand() {
        if (exitCommand == null) {                      
            // Insert pre-init code here
            exitCommand = new Command("Exit", Command.EXIT, 1);                      
            // Insert post-init code here
        }                      
        return exitCommand;
    }                    
 
    /** This method returns instance for okCommandStartDeviceSearch component and should be called instead of accessing okCommandStartDeviceSearch field directly.                         
     * @return Instance for okCommandStartDeviceSearch component
     */
    public Command get_okCommandStartDeviceSearch() {
        if (okCommandStartDeviceSearch == null) {                       
            // Insert pre-init code here
            okCommandStartDeviceSearch = new Command("Start", Command.OK, 1);                       
            // Insert post-init code here
        }                       
        return okCommandStartDeviceSearch;
    }                     
 
    /** This method returns instance for image1 component and should be called instead of accessing image1 field directly.                         
     * @return Instance for image1 component
     */
    public Image get_image1() {
        if (image1 == null) {                       
            // Insert pre-init code here
            try {                        
                image1 = Image.createImage((String) null);
            } catch (java.io.IOException exception) {
                exception.printStackTrace();
            }                      
            // Insert post-init code here
        }                       
        return image1;
    }                     
    /** This method returns instance for itemCommand1 component and should be called instead of accessing itemCommand1 field directly.                         
     * @return Instance for itemCommand1 component
     */
    public Command get_itemCommand1() {
        if (itemCommand1 == null) {                       
            // Insert pre-init code here
            itemCommand1 = new Command("Item", Command.ITEM, 1);                       
            // Insert post-init code here
        }                       
        return itemCommand1;
    }                     

    /** This method returns instance for updateGPSListCommand component and should be called instead of accessing updateGPSListCommand field directly.                         
     * @return Instance for updateGPSListCommand component
     */
    public Command get_updateGPSListCommand() {
        if (updateGPSListCommand == null) {                       
            // Insert pre-init code here
            updateGPSListCommand = new Command("Command", Command.OK, 1);                       
            // Insert post-init code here
        }                       
        return updateGPSListCommand;
    }                     
     
    
  public List get_GPSListReturnDevice() 
  {
      if (GPSListReturnDevice == null) 
      {                      
            // Insert pre-init code here
            GPSListReturnDevice = new List("Devices", Choice.IMPLICIT, new String[0], new Image[0]);                       
            GPSListReturnDevice.addCommand(get_exitCommand());
            GPSListReturnDevice.setCommandListener(this);
            GPSListReturnDevice.setSelectedFlags(new boolean[0]);
            GPSListReturnDevice.setSelectCommand(get_okCommandStartDeviceSearch());                     
           // GPSListReturnDevice.setSelectCommand();
        }                      
        return GPSListReturnDevice;
   }                    
    
 
  
    public void startApp() {  
       GPSListReturnDevice.append("start",null); 
    }
  
    public void pauseApp() 
    {
    }
    
    public void destroyApp(boolean unconditional) 
    {
    }
    
    //adds an Item to GPSList 
   
     public static void GPSListAppend(String itemToBeAdded)
    {
       GPSListReturnDevice.append(itemToBeAdded,null);
     
    }

    //method to start discovery of devices
    private void discoverDevices()
    {
       GPSListReturnDevice.deleteAll();
       GPSListReturnDevice.setTitle("discovering Devices.. ");
       dC.inquiryDevice();
    } 
    
    //method to start service discovery of specified device
    private void discoverServices(int service)
    {
    
    }

    

}
GPSConnect
Code:
package Finder;

import javax.bluetooth.UUID;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.bluetooth.*;
import javax.bluetooth.UUID.* ;
import javax.bluetooth.LocalDevice ;
import javax.bluetooth.ServiceRecord ;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryListener;



public class GPSConnect implements DiscoveryListener {
    
    /** Creates a new instance of GPSConnect */
    public GPSConnect() 
    {
       uuidSet = new UUID[1] ;
       uuidSet[0] = L2CAP_UUID; 
       
    }
    

    private UUID L2CAP_UUID = new UUID(0x0100);
    private UUID[] uuidSet ;

 ChoiceGroup        labels;
 Vector             remoteDevices = new Vector();
 LocalDevice        localDevice;
 DiscoveryAgent     discoveryAgent;
 private int        numDevice ;
 private            String serviceUrl;
 private            RemoteDevice remoteDevice;
 private boolean    serviceSearchCompleted;
 private boolean    inquiryCompleted;

 public int getNumdevice(){
 return numDevice ;
 }
 

 public boolean getInquiryCompleted()
 {
    return inquiryCompleted;
 }

public void inquiryDevice()
{
inquiryCompleted = false;

    try
    {
        GPS.GPSListAppend("inquiryDevice");
        localDevice = LocalDevice.getLocalDevice();
        discoveryAgent = localDevice.getDiscoveryAgent();
        discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this); 
        
    }
    
    catch (Exception e)
    {
    }
}


public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) 
{
    try
    {
        String friendlyName = btDevice.getFriendlyName(false);
        
       
        labels.append(friendlyName, null); 
        remoteDevices.addElement(btDevice); 
        numDevice++; // increase number of detected device by 1
        GPS.GPSListAppend(friendlyName);
    }
    catch (Exception e)
    {
    }
}


public void inquiryCompleted(int discType)
{ 
    inquiryCompleted = true;
}

}
Reply With Quote

#2 Old Re: deviceDiscovered Not Discovering - 2007-07-23, 08:56

Join Date: Feb 2007
Posts: 29
adamzieba
Offline
Registered User
ok got it.

the inquiry device method should look like this:

Code:
 public void inquiryDevice()
{
//inquiryCompleted = false;

    try
    {
        GPS.GPSListAppend("friendlyName");
        localDevice = LocalDevice.getLocalDevice();


///THIS LINE IS IMPORTANT
///THIS LINE IS IMPORTANT
        localDevice.setDiscoverable(DiscoveryAgent.GIAC);




        discoveryAgent = localDevice.getDiscoveryAgent();// inquiry bluetooth device in range
       
    
        discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this); 
        
    }
    catch (BluetoothStateException ex) 
    {
            System.err.println("Error in BluetoothUtility.findDevices: " + ex.toString());
            ex.printStackTrace();
    }
}
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
Discovering Bluetooth Devices mottaisami Mobile Java General 1 2007-03-23 10:57
Discovering services on 6230i mattaylor20 Mobile Java Networking & Messaging & Security 2 2006-07-17 23:48
Problem in discovering Bluetooth devices anummiah Mobile Java Networking & Messaging & Security 0 2005-08-15 14:24
Discovering devices yourself HershD Bluetooth Technology 12 2003-11-01 07:40
Discovering BT devices with HostResolver: Timeout question tvollmer Bluetooth Technology 2 2003-10-20 10:37

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia