Subversion Repositories FlightCtrl

Rev

Rev 181 | Go to most recent revision | Details | Compare with Previous | 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
 
208 ligi 31
 
181 ligi 32
    public int remote_device_count=0;  
33
    public String[] remote_device_name;
34
    public String[] remote_device_mac;
35
 
36
 
208 ligi 37
    public void search_again()
38
    {
39
        try
40
            {
41
                remote_device_count=0;          
42
                searching=true;
43
                m_DscrAgent.startInquiry(DiscoveryAgent.GIAC,this);
44
            }
45
        catch (BluetoothStateException ex)
46
            {
47
                error=true;
48
                err_log+="Problem in searching the blue tooth devices\n" + ex;
49
 
50
            }
51
 
52
 
53
    }
54
 
181 ligi 55
    public BTSearcher()
56
    {
57
 
58
        remote_device_name=new String[MAX_DEVICES];
59
        remote_device_mac=new String[MAX_DEVICES];
60
 
208 ligi 61
 
181 ligi 62
        try
63
            {
64
                //First get the local device and obtain the discovery agent. 
65
                m_LclDevice = LocalDevice.getLocalDevice();
66
                m_DscrAgent=  m_LclDevice.getDiscoveryAgent();
67
 
68
            }
69
        catch (BluetoothStateException ex)
70
            {
71
                error=true;
72
                err_log+="Problem in searching the blue tooth devices\n" + ex;
73
 
74
            }
208 ligi 75
        search_again();
181 ligi 76
    }
77
 
78
 
79
    public void inquiryCompleted(int transID) {
80
 
81
 
82
        searching=false;
83
    }
84
 
85
 
86
    //Called when device is found during inquiry 
87
    public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod)
88
    {
89
        try
90
            {
91
                if (remote_device_count!=(MAX_DEVICES-1))
92
                    {
208 ligi 93
 
94
                        remote_device_name[remote_device_count]=btDevice.getFriendlyName(true);
95
                        remote_device_mac[remote_device_count]=btDevice.getBluetoothAddress();
181 ligi 96
                        remote_device_count++;
97
                    }
98
            }
99
        catch (Exception e)
100
            {
101
                err_log+=("Device Discovered Error: " + e);    
102
            }
103
 
104
    }
105
 
106
 
107
    public void serviceSearchCompleted(int transID, int respCode)
108
    {   }
109
 
110
    public void servicesDiscovered(int transID, ServiceRecord[] records)
111
    {    }
112
 
113
 
114
 
115
}