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