3 Replies - 1219 Views - Last Post: 11 February 2011 - 06:06 PM

#1 zahraa Ibraheem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-February 11

bluetooth search not stop how can i stop search

Posted 09 February 2011 - 07:58 PM

i need yor help in developing that code

import java.util.Vector;
import javax.bluetooth.UUID;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.RemoteDevice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.*;


public class BlueTooth extends MIDlet implements CommandListener,DiscoveryListener {
    private LocalDevice local=null;
    private DiscoveryAgent agent=null;
    private Vector devicesFound =null;
    private Vector decicesFound ;
    private ServiceRecord[] servicesFound = null;
    private Display display;
    private Command discover;
    private Command exit;
    private List deviceList;



    public BlueTooth(){
        display = Display.getDisplay(this);
        discover = new Command("Discover",Command.SCREEN, 0);
        exit = new Command("Exit",Command.EXIT, 0);

        // Device List
        setDeviceList(new List("Select Device", List.IMPLICIT));
        deviceList.addCommand(exit);
        deviceList.setCommandListener(this);



}
    public void startApp() {
        Form form = new Form("Devices");
        form.addCommand(discover);
        form.addCommand(exit);
        form.setCommandListener(this);
        display.setCurrent(form);
          doDeviceDiscovery();
    }

    public void pauseApp() {
    }

  public void destroyApp(boolean unconditional) {
      
    }

    public void commandAction(Command c, Displayable d) {
        if(c == exit) {
            notifyDestroyed();
            destroyApp(true);
        }

        if(c == discover) {
               doDeviceDiscovery();
        }

    }

    public void deviceDiscovered(RemoteDevice remoteDevice, DeviceClass deviceClass) {
        getDevicesFound().addElement(remoteDevice);
    }

    public void inquiryCompleted(int param) {
        switch(param)
        {
            case DiscoveryListener.INQUIRY_COMPLETED:
                try{
                if(devicesFound.size()>0)
                {
                    doServiceSearch((RemoteDevice)devicesFound.firstElement());
                }
                }
                catch(Exception e){}
                break;

            case DiscoveryListener.INQUIRY_ERROR:
                break;

            case DiscoveryListener.INQUIRY_TERMINATED:
                break;


        }


    }

    public void serviceSearchCompleted(int transID, int respCode) {
switch(respCode)
{
    case DiscoveryListener.SERVICE_SEARCH_COMPLETED:
        break;

    case DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE:
        break;
    case DiscoveryListener.SERVICE_SEARCH_ERROR:
        break;
    case DiscoveryListener.SERVICE_SEARCH_NO_RECORDS:
        break;

    case DiscoveryListener.SERVICE_SEARCH_TERMINATED:
        break;

}
    }

    public void servicesDiscovered(int transID, ServiceRecord[] serviceRecord) {
    servicesFound=serviceRecord;
    }
    private void doDeviceDiscovery()
    {
    /*   Thread t = new Thread(this);
        t.start();

*/
        try{
            local=LocalDevice.getLocalDevice();
        }
        catch(BluetoothStateException bse)
  {
        }

        agent =local.getDiscoveryAgent();
        decicesFound =new Vector();

        try{
            if (!agent.startInquiry(DiscoveryAgent.GIAC, this))
            {
        }
        }
            catch(BluetoothStateException bse)
        {
    }
}

    private void doServiceSearch(RemoteDevice device )
    {
        int [] attributes = {0x100,0x101,0x102};
        UUID[]uuids= new UUID[1];
        uuids[0]=new UUID(0x1002);
        try
        {
            agent.searchServices(attributes, uuids, device, this);

        }
        catch(BluetoothStateException e)
        {

        }
    }


    public void setLocal(LocalDevice local) {
        this.local = local;
    }

    public DiscoveryAgent getAgent() {
        return agent;
    }

    public void setAgent(DiscoveryAgent agent) {
        this.agent = agent;
    }

    public Vector getDevicesFound() {
        return devicesFound;
    }

       public void setDevicesFound(Vector devicesFound) {
        this.devicesFound = devicesFound;
    }


    public void setDeviceList(List deviceList) {
        this.deviceList = deviceList;
    }
}



this code doig search for devices but not stop search how can i stop doing search when find any devise

Edited by Dogstopper: :code:! And moved to Mobile Development.

Is This A Good Question/Topic? 0
  • +

Replies To: bluetooth search not stop how can i stop search

#2 zahraa Ibraheem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-February 11

Re: bluetooth search not stop how can i stop search

Posted 09 February 2011 - 08:29 PM

in this code many hints appear to my in line 38,172,180

in line 38 hints that appear : overridable method call in constructor
-make the class BlueTooth is final
-make the method setDeviceList is final
-make the method setDeviceList is static

-make the method setDeviceList is private




in line 172:
replace with java.util.Collections.unmodifiableCollection(agent)

in line 180:

replace with java.util.Collections.unmodifiableCollection(devicesFound)

in the last 2 hints when i replace
return agent;
with :
replace with java.util.Collections.unmodifiableCollection(agent)

and
return devicesFound;
with : replace with java.util.Collections.unmodifiableCollection(devicesFound)
appear an error which is in import java.util.Collections;
error is can't find symbol
what can i do to right this errors and hints
then how can i stop search to send a file
note : i'm a beginnerin j2me

please help me
Was This Post Helpful? 0
  • +
  • -

#3 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: bluetooth search not stop how can i stop search

Posted 10 February 2011 - 06:40 AM

You have to import the java.util.Collections library to use Collections.unmodifiableCollection.
import java.util.Collections;
import java.util.Collection;


Then your method would look like:
public Collection getDevicesFound() {  

   return return Collections.unmodifiableCollection(devicesFound);  

} 


Also, when declaring a Collection implementation it is better to specify what type of Objects it holds.
so, a Vector of String should be declared as:
Vector<String> svec = new Vector<String>();

Was This Post Helpful? 0
  • +
  • -

#4 zahraa Ibraheem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-February 11

Re: bluetooth search not stop how can i stop search

Posted 11 February 2011 - 06:06 PM

after correcting errors in my code then install jar file in my mobile to search for any device the program not work and not found any device .
how can i update that code to search for actual device ?!
please help me
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1