Subversion Repositories Projects

Rev

Rev 382 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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