Subversion Repositories FlightCtrl

Rev

Rev 181 | Blame | Last modification | View Log | RSS feed

/*********************************************************************************************************************************
 *                                                                                                                                *
 * searches 4 Bluetooth Devices                                                                                                   *
 *                                                                                                                                *
 * Author:        Marcus -LiGi- Bueschleb                                                                                         *
 * Project-Start: 9/2007                                                                                                          *

 * Mailto:        ligi@smart4mobile.de                                                                                            *
 * Licence:       Creative Commons / Non Commercial                                                                               *
 * Big Up:        Holger&Ingo                                                                                                     *
 *********************************************************************************************************************************/


import javax.bluetooth.*;

public class BTSearcher
    implements DiscoveryListener
               
{
    private LocalDevice m_LclDevice = null;    
    private DiscoveryAgent m_DscrAgent=null;



    public boolean searching=true;
    public boolean error=false;
    public String  err_log="none";


    public final int MAX_DEVICES=10;


    public int remote_device_count=0;  
    public String[] remote_device_name;
    public String[] remote_device_mac;


    public void search_again()
    {
        try
            {
                remote_device_count=0;          
                searching=true;
                m_DscrAgent.startInquiry(DiscoveryAgent.GIAC,this);
            }
        catch (BluetoothStateException ex)
            {
                error=true;
                err_log+="Problem in searching the blue tooth devices\n" + ex;
               
            }
       

    }

    public BTSearcher()
    {

        remote_device_name=new String[MAX_DEVICES];
        remote_device_mac=new String[MAX_DEVICES];


        try
            {
                //First get the local device and obtain the discovery agent.
                m_LclDevice = LocalDevice.getLocalDevice();
                m_DscrAgent=  m_LclDevice.getDiscoveryAgent();
     
            }
        catch (BluetoothStateException ex)
            {
                error=true;
                err_log+="Problem in searching the blue tooth devices\n" + ex;
               
            }
        search_again();
    }


    public void inquiryCompleted(int transID) {


        searching=false;
    }


    //Called when device is found during inquiry
    public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod)
    {
        try
            {
                if (remote_device_count!=(MAX_DEVICES-1))
                    {

                        remote_device_name[remote_device_count]=btDevice.getFriendlyName(true);
                        remote_device_mac[remote_device_count]=btDevice.getBluetoothAddress();
                        remote_device_count++;
                    }
            }
        catch (Exception e)
            {
                err_log+=("Device Discovered Error: " + e);    
            }

    }


    public void serviceSearchCompleted(int transID, int respCode)
    {   }

    public void servicesDiscovered(int transID, ServiceRecord[] records)
    {    }



}