views:

380

answers:

3

I develop simple j2me bluetooth client and have problem with bluetooth device search. Function startInquiry nothing found.

Client : nokia 5220 Server : my pc with bluetooth adapter

All bluetooth devices is on.

/* * To change this template, choose Tools | Templates * and open the template in the editor. */

import javax.microedition.midlet.*;
import javax.bluetooth.*;
import java.util.Vector;
import javax.microedition.lcdui.*;

/**
 * @author Администратор
 */
public class Midlet extends MIDlet implements DiscoveryListener
{
    private static Vector vecDevices=new Vector();
    private static String connectionURL=null;
    private LocalDevice localDevice;
    private DiscoveryAgent agent;
    private RemoteDevice remoteDevice;
    private RemoteDevice[] devList;
    private Display display;
    private Form form;

    public void startApp() {
        display = Display.getDisplay(this);
        form = new Form( "Client" );
        try {
            localDevice = LocalDevice.getLocalDevice();
        } catch( BluetoothStateException e ) {
            e.printStackTrace();
        }

        form.append("Address: "+localDevice.getBluetoothAddress()+"\n\n");
        form.append("Name: "+localDevice.getFriendlyName()+"\n\n");

        try {
            agent = localDevice.getLocalDevice().getDiscoveryAgent();

            form.append("Starting device inquiry... \n\n");

            boolean si = agent.startInquiry(DiscoveryAgent.GIAC,  this);
            if ( si ) {
                form.append("true");
            } else {
                form.append("false");
            }
        } catch( BluetoothStateException e ) {
        }

        int deviceCount = vecDevices.size();
        if(deviceCount <= 0){
            form.append("No Devices Found .");
        }
        else{
            //print bluetooth device addresses and names in the format [ No. address (name) ]
            form.append("Bluetooth Devices: ");
            for (int i = 0; i < deviceCount; i++) {
                remoteDevice=(RemoteDevice)vecDevices.elementAt(i);
                form.append( remoteDevice.getBluetoothAddress() );
            }
        }

            display.setCurrent(form);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

        public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
              //add the device to the vector
              if(!vecDevices.contains(btDevice)){
                  vecDevices.addElement(btDevice);
              }
        }
        public void inquiryCompleted(int discType)
        {
        }


          //implement this method since services are not being discovered
          public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
              if(servRecord!=null && servRecord.length>0){
                  connectionURL=servRecord[0].getConnectionURL(0,false);
              }
          }
          //implement this method since services are not being discovered
          public void serviceSearchCompleted(int transID, int respCode) {
          }
}
+1  A: 

Not sure what the exact problem is, but you definitely don't want to be doing this in your midlet's startApp() method. This is a system lifecycle method, and should return quickly, but scanning for bluetooth devices will block it for a long time. Your startApp() method is tying up the device's resources which it could need for doing the actual scanning!

Refactor, so your device scanning is done in a new thread, then see what happens.

funkybro
A: 

You say all devices are on - but also check if all devices are discoverable.

I've made this mistake before myself!

Lookup the method LocalDevice.setDiscoverable() if you want to toggle between modes programatically.

David Johnson
A: 

You seem to have misunderstood how the Bluetooth API works. The startInquiry method only starts the device discovery process and returns immediately afterwards, leaving the discovery running in the background. When devices are discovered, you get a callback of the deviceDiscovered method for each of them, and when the discovery process has completed, you get a callback of the inquiryCompleted method. So you need to move the accessing of the vecDevices member and the form manipulation from startApp to inquiryCompleted to be able to actually show the discovered information.

jk