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