Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
181 | ligi | 1 | /********************************************************************************************************************************* |
2 | * * |
||
3 | * searches 4 Bluetooth Devices * |
||
4 | * * |
||
5 | * Author: Marcus -LiGi- Bueschleb * |
||
6 | * Project-Start: 9/2007 * |
||
7 | |||
8 | * Mailto: ligi@smart4mobile.de * |
||
9 | * Licence: Creative Commons / Non Commercial * |
||
10 | * Big Up: Holger&Ingo * |
||
11 | *********************************************************************************************************************************/ |
||
12 | |||
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 final int MAX_DEVICES=10; |
||
30 | |||
231 | ligi | 31 | public RemoteDevice[] remote_devices; |
181 | ligi | 32 | public int remote_device_count=0; |
33 | public String[] remote_device_name; |
||
34 | public String[] remote_device_mac; |
||
35 | |||
36 | |||
37 | public BTSearcher() |
||
38 | { |
||
39 | |||
231 | ligi | 40 | remote_devices=new RemoteDevice[MAX_DEVICES]; |
181 | ligi | 41 | remote_device_name=new String[MAX_DEVICES]; |
42 | remote_device_mac=new String[MAX_DEVICES]; |
||
43 | |||
231 | ligi | 44 | remote_device_count=0; |
303 | ligi | 45 | |
46 | } |
||
47 | |||
48 | public void search() |
||
49 | { |
||
50 | searching=true; |
||
51 | remote_device_count=0; |
||
181 | ligi | 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 | |||
231 | ligi | 58 | m_DscrAgent.startInquiry(DiscoveryAgent.GIAC,this); |
181 | ligi | 59 | } |
60 | catch (BluetoothStateException ex) |
||
61 | { |
||
62 | error=true; |
||
63 | err_log+="Problem in searching the blue tooth devices\n" + ex; |
||
64 | |||
65 | } |
||
303 | ligi | 66 | |
181 | ligi | 67 | } |
68 | |||
69 | public void inquiryCompleted(int transID) { |
||
70 | |||
231 | ligi | 71 | try { |
72 | for(int i=0;i<remote_device_count;i++) |
||
73 | { |
||
74 | remote_device_name[i]=remote_devices[i].getFriendlyName(true); |
||
75 | remote_device_mac[i]=remote_devices[i].getBluetoothAddress(); |
||
181 | ligi | 76 | |
231 | ligi | 77 | } |
78 | } |
||
79 | catch (Exception e) |
||
80 | { |
||
81 | err_log+="Problem in searching the blue tooth devices"; |
||
82 | } |
||
181 | ligi | 83 | searching=false; |
84 | } |
||
85 | |||
86 | |||
231 | ligi | 87 | public void search_again() |
88 | { |
||
89 | } |
||
90 | |||
181 | ligi | 91 | //Called when device is found during inquiry |
92 | public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) |
||
93 | { |
||
94 | try |
||
95 | { |
||
96 | if (remote_device_count!=(MAX_DEVICES-1)) |
||
97 | { |
||
231 | ligi | 98 | remote_devices[remote_device_count]=btDevice; |
181 | ligi | 99 | remote_device_count++; |
100 | } |
||
101 | } |
||
102 | catch (Exception e) |
||
103 | { |
||
104 | err_log+=("Device Discovered Error: " + e); |
||
105 | } |
||
106 | |||
107 | } |
||
108 | |||
109 | |||
110 | public void serviceSearchCompleted(int transID, int respCode) |
||
111 | { } |
||
112 | |||
113 | public void servicesDiscovered(int transID, ServiceRecord[] records) |
||
114 | { } |
||
115 | |||
116 | |||
117 | |||
118 | } |