Rev 303 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 303 | Rev 403 | ||
---|---|---|---|
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 | * Project-Start: 9/2007 * |
- | |
7 | 6 | * |
|
8 | * Mailto: ligi@smart4mobile.de * |
- | |
9 | * Licence: Creative Commons / Non Commercial * |
7 | * see README for further Infos |
10 | * Big Up: Holger&Ingo * |
- | |
- | 8 | * |
|
11 | *********************************************************************************************************************************/ |
9 | ****************************************/ |
12 | 10 | ||
13 | import javax.bluetooth.*; |
11 | import javax.bluetooth.*; |
14 | 12 | ||
15 | public class BTSearcher |
13 | public class BTSearcher |
16 | implements DiscoveryListener |
14 | implements DiscoveryListener |
17 | 15 | ||
18 | { |
16 | { |
19 | private LocalDevice m_LclDevice = null; |
17 | private LocalDevice m_LclDevice = null; |
20 | private DiscoveryAgent m_DscrAgent=null; |
18 | private DiscoveryAgent m_DscrAgent=null; |
21 | 19 | ||
22 | 20 | ||
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 final int MAX_DEVICES=10; |
27 | public final int MAX_DEVICES=10; |
30 | 28 | ||
31 | public RemoteDevice[] remote_devices; |
29 | public RemoteDevice[] remote_devices; |
32 | public int remote_device_count=0; |
30 | public int remote_device_count=0; |
33 | public String[] remote_device_name; |
31 | public String[] remote_device_name; |
34 | public String[] remote_device_mac; |
32 | public String[] remote_device_mac; |
35 | 33 | ||
36 | 34 | ||
37 | public BTSearcher() |
35 | public BTSearcher() |
38 | { |
36 | { |
39 | 37 | ||
40 | remote_devices=new RemoteDevice[MAX_DEVICES]; |
38 | remote_devices=new RemoteDevice[MAX_DEVICES]; |
41 | remote_device_name=new String[MAX_DEVICES]; |
39 | remote_device_name=new String[MAX_DEVICES]; |
42 | remote_device_mac=new String[MAX_DEVICES]; |
40 | remote_device_mac=new String[MAX_DEVICES]; |
43 | 41 | ||
44 | remote_device_count=0; |
42 | remote_device_count=0; |
45 | 43 | ||
46 | } |
44 | } |
47 | 45 | ||
48 | public void search() |
46 | public void search() |
49 | { |
47 | { |
50 | searching=true; |
48 | searching=true; |
51 | remote_device_count=0; |
49 | remote_device_count=0; |
52 | try |
50 | try |
53 | { |
51 | { |
54 | //First get the local device and obtain the discovery agent. |
52 | //First get the local device and obtain the discovery agent. |
55 | m_LclDevice = LocalDevice.getLocalDevice(); |
53 | m_LclDevice = LocalDevice.getLocalDevice(); |
56 | m_DscrAgent= m_LclDevice.getDiscoveryAgent(); |
54 | m_DscrAgent= m_LclDevice.getDiscoveryAgent(); |
57 | 55 | ||
58 | m_DscrAgent.startInquiry(DiscoveryAgent.GIAC,this); |
56 | m_DscrAgent.startInquiry(DiscoveryAgent.GIAC,this); |
59 | } |
57 | } |
60 | catch (BluetoothStateException ex) |
58 | catch (BluetoothStateException ex) |
61 | { |
59 | { |
62 | error=true; |
60 | error=true; |
63 | err_log+="Problem in searching the blue tooth devices\n" + ex; |
61 | err_log+="Problem in searching the blue tooth devices\n" + ex; |
64 | 62 | ||
65 | } |
63 | } |
66 | 64 | ||
67 | } |
65 | } |
68 | 66 | ||
69 | public void inquiryCompleted(int transID) { |
67 | public void inquiryCompleted(int transID) { |
70 | 68 | ||
71 | try { |
69 | try { |
72 | for(int i=0;i<remote_device_count;i++) |
70 | for(int i=0;i<remote_device_count;i++) |
73 | { |
71 | { |
74 | remote_device_name[i]=remote_devices[i].getFriendlyName(true); |
72 | remote_device_name[i]=remote_devices[i].getFriendlyName(true); |
75 | remote_device_mac[i]=remote_devices[i].getBluetoothAddress(); |
73 | remote_device_mac[i]=remote_devices[i].getBluetoothAddress(); |
76 | 74 | ||
77 | } |
75 | } |
78 | } |
76 | } |
79 | catch (Exception e) |
77 | catch (Exception e) |
80 | { |
78 | { |
81 | err_log+="Problem in searching the blue tooth devices"; |
79 | err_log+="Problem in searching the blue tooth devices"; |
82 | } |
80 | } |
83 | searching=false; |
81 | searching=false; |
84 | } |
82 | } |
85 | 83 | ||
86 | 84 | ||
87 | public void search_again() |
85 | public void search_again() |
88 | { |
86 | { |
89 | } |
87 | } |
90 | 88 | ||
91 | //Called when device is found during inquiry |
89 | //Called when device is found during inquiry |
92 | public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) |
90 | public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) |
93 | { |
91 | { |
94 | try |
92 | try |
95 | { |
93 | { |
96 | if (remote_device_count!=(MAX_DEVICES-1)) |
94 | if (remote_device_count!=(MAX_DEVICES-1)) |
97 | { |
95 | { |
98 | remote_devices[remote_device_count]=btDevice; |
96 | remote_devices[remote_device_count]=btDevice; |
99 | remote_device_count++; |
97 | remote_device_count++; |
100 | } |
98 | } |
101 | } |
99 | } |
102 | catch (Exception e) |
100 | catch (Exception e) |
103 | { |
101 | { |
104 | err_log+=("Device Discovered Error: " + e); |
102 | err_log+=("Device Discovered Error: " + e); |
105 | } |
103 | } |
106 | 104 | ||
107 | } |
105 | } |
108 | 106 | ||
109 | 107 | ||
110 | public void serviceSearchCompleted(int transID, int respCode) |
108 | public void serviceSearchCompleted(int transID, int respCode) |
111 | { } |
109 | { } |
112 | 110 | ||
113 | public void servicesDiscovered(int transID, ServiceRecord[] records) |
111 | public void servicesDiscovered(int transID, ServiceRecord[] records) |
114 | { } |
112 | { } |
115 | 113 | ||
116 | 114 | ||
117 | 115 | ||
118 | } |
116 | } |
119 | 117 |