Subversion Repositories FlightCtrl

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
181 ligi 1
/*********************************************************************************************************************************
2
 *                                                                                                                                *
3
 * searches 4 Bluetooth Devices                                                                                                   *
4
 *                                                                                                                                *
5
 * Author:        Marcus -LiGi- Bueschleb                                                                                         *
6
 * Project-Start: 9/2007                                                                                                          *
7
 
8
 * Mailto:        ligi@smart4mobile.de                                                                                            *
9
 * Licence:       Creative Commons / Non Commercial                                                                               *
10
 * Big Up:        Holger&Ingo                                                                                                     *
11
 *********************************************************************************************************************************/
12
 
13
import javax.bluetooth.*;
14
 
15
public class BTSearcher
16
    implements DiscoveryListener
17
 
18
{
19
    private LocalDevice m_LclDevice = null;    
20
    private DiscoveryAgent m_DscrAgent=null;
21
 
22
 
23
 
24
    public boolean searching=true;
25
    public boolean error=false;
26
    public String  err_log="none";
27
 
28
 
29
    public final int MAX_DEVICES=10;
30
 
31
    public RemoteDevice[] remote_devices;
32
    public int remote_device_count=0;  
33
    public String[] remote_device_name;
34
    public String[] remote_device_mac;
35
 
36
 
37
    public BTSearcher()
38
    {
39
 
40
        remote_devices=new RemoteDevice[MAX_DEVICES];
41
        remote_device_name=new String[MAX_DEVICES];
42
        remote_device_mac=new String[MAX_DEVICES];
43
 
44
        remote_device_count=0;          
45
        try
46
            {
47
                //First get the local device and obtain the discovery agent. 
48
                m_LclDevice = LocalDevice.getLocalDevice();
49
                m_DscrAgent=  m_LclDevice.getDiscoveryAgent();
50
 
51
                m_DscrAgent.startInquiry(DiscoveryAgent.GIAC,this);
52
            }
53
        catch (BluetoothStateException ex)
54
            {
55
                error=true;
56
                err_log+="Problem in searching the blue tooth devices\n" + ex;
57
 
58
            }
59
 
60
    }
61
 
62
 
63
    public void inquiryCompleted(int transID) {
64
 
65
        try {
66
            for(int i=0;i<remote_device_count;i++)
67
                {
68
                    remote_device_name[i]=remote_devices[i].getFriendlyName(true);
69
                    remote_device_mac[i]=remote_devices[i].getBluetoothAddress();
70
 
71
                }
72
        }
73
        catch (Exception e)
74
            {
75
                err_log+="Problem in searching the blue tooth devices";
76
            }  
77
        searching=false;
78
    }
79
 
80
 
81
    //Called when device is found during inquiry 
82
    public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod)
83
    {
84
        try
85
            {
86
                if (remote_device_count!=(MAX_DEVICES-1))
87
                    {
88
                        remote_devices[remote_device_count]=btDevice;
89
                        remote_device_count++;
90
                    }
91
            }
92
        catch (Exception e)
93
            {
94
                err_log+=("Device Discovered Error: " + e);    
95
            }
96
 
97
    }
98
 
99
 
100
    public void serviceSearchCompleted(int transID, int respCode)
101
    {   }
102
 
103
    public void servicesDiscovered(int transID, ServiceRecord[] records)
104
    {    }
105
 
106
 
107
 
108
}