Subversion Repositories Projects

Rev

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

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