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