Subversion Repositories Projects

Rev

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

Rev Author Line No. Line
206 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
//package org.ligi.dubwise;
12
//#if bluetooth=="on"
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 void log(String err_str)
30
    {
31
 
32
        err_log+=err_str;
33
        System.out.println(err_str);
34
    }
35
 
36
    public final int MAX_DEVICES=10;
37
 
38
    public RemoteDevice[] remote_devices;
39
    public int remote_device_count=0;  
40
    public String[] remote_device_name;
41
    public String[] remote_device_mac;
42
 
43
 
44
    public BTSearcher()
45
    {
46
 
47
        remote_devices=new RemoteDevice[MAX_DEVICES];
48
        remote_device_name=new String[MAX_DEVICES];
49
        remote_device_mac=new String[MAX_DEVICES];
50
 
51
        remote_device_count=0;          
52
 
53
    }
54
 
55
    public void search()
56
    {
57
        searching=true;
58
        remote_device_count=0;          
59
        try
60
            {
61
                //First get the local device and obtain the discovery agent. 
62
                m_LclDevice = LocalDevice.getLocalDevice();
63
                m_DscrAgent=  m_LclDevice.getDiscoveryAgent();
64
 
65
                m_DscrAgent.startInquiry(DiscoveryAgent.GIAC,this);
66
            }
67
        catch (BluetoothStateException ex)
68
            {
69
                error=true;
70
                log("Problem in searching the blue tooth devices\n" + ex);
71
 
72
            }
73
 
74
    }
75
 
76
    public void inquiryCompleted(int transID) {
77
 
78
        try {
79
            log("search complete with " + remote_device_count + " devices");
80
            for(int i=0;i<remote_device_count;i++)
81
                {
82
                    log("#" + i + " -> addr: " + remote_devices[i].getBluetoothAddress());
83
                    remote_device_mac[i]=remote_devices[i].getBluetoothAddress();
84
 
85
                    remote_device_name[i]=remote_devices[i].getBluetoothAddress();
86
                    try {
87
                        log("#" + i + "name:" + remote_devices[i].getFriendlyName(true));
88
                        remote_device_name[i]=remote_devices[i].getFriendlyName(true);
89
                    }
90
                    catch (Exception e)
91
                        {
92
                            log("Problem getting name of BT-Device( -> taking mac as name): " + e);
93
                        }      
94
 
95
 
96
                }
97
        }
98
        catch (Exception e)
99
            {
100
                log("Problem in searching the blue tooth devices" + e);
101
            }  
102
        searching=false;
103
    }
104
 
105
 
106
    public void search_again()
107
    {
108
    }
109
 
110
    //Called when device is found during inquiry 
111
    public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod)
112
    {
113
        log("found device ");  
114
        try
115
            {
116
                if (remote_device_count!=(MAX_DEVICES-1))
117
                    {
118
                        remote_devices[remote_device_count]=btDevice;
119
                        remote_device_count++;
120
                    }
121
            }
122
        catch (Exception e)
123
            {
124
                log("Device Discovered Error: " + e);  
125
            }
126
 
127
    }
128
 
129
 
130
    public void serviceSearchCompleted(int transID, int respCode)
131
    {   }
132
 
133
    public void servicesDiscovered(int transID, ServiceRecord[] records)
134
    {    }
135
 
136
 
137
 
138
}
139
 
140
//#endif