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