xref: /haiku/src/bin/bt_discovery.cpp (revision c1990799c00e361eb4b93b77932433fda077c535)
1 /*
2  * Copyright 2008 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 
9 #include <bluetooth/LocalDevice.h>
10 #include <bluetooth/bdaddrUtils.h>
11 
12 #include <bluetooth/DeviceClass.h>
13 #include <bluetooth/DiscoveryAgent.h>
14 #include <bluetooth/DiscoveryListener.h>
15 
16 thread_id mainThread;
17 
18 class simpleDiscoveryListener : public DiscoveryListener {
19 
20 public:
21 
22 simpleDiscoveryListener() : DiscoveryListener()
23 {
24 
25 }
26 
27 
28 void
29 DeviceDiscovered(RemoteDevice* btDevice, DeviceClass cod)
30 {
31 	BString classString;
32 
33 	printf("\t%s: Device %s discovered.\n",__FUNCTION__, bdaddrUtils::ToString(btDevice->GetBluetoothAddress()));
34 
35 	cod.GetServiceClass(classString);
36 	classString << " |";
37 	cod.GetMajorDeviceClass(classString);
38 	classString << " |";
39 	cod.GetMinorDeviceClass(classString);
40 	printf("\t\t%s: \n", classString.String());
41 }
42 
43 
44 void
45 InquiryCompleted(int discType)
46 {
47 
48 	printf("\t%s: Inquiry process has finished ...\n",__FUNCTION__);
49 	(void)send_data(mainThread, discType, NULL, 0);
50 
51 }
52 
53 
54 void
55 InquiryStarted(status_t status)
56 {
57 	printf("\t%s: Inquiry process has started ...\n",__FUNCTION__);
58 }
59 
60 
61 };
62 
63 
64 void
65 DumpInfo(LocalDevice* device)
66 {
67 	DiscoveryAgent* dAgent = device->GetDiscoveryAgent();
68 
69 	if (dAgent == NULL) {
70 		printf("DiscoveryAgent could not be located\n");
71 		return;
72 	}
73 
74 	printf("Discovering for  [LocalDevice] %s\t%s\n\n",
75          (device->GetFriendlyName()).String(),
76           bdaddrUtils::ToString(device->GetBluetoothAddress()));
77 
78 	simpleDiscoveryListener* dListener = new simpleDiscoveryListener();
79 
80 	dAgent->StartInquiry(BT_GIAC, dListener);
81 
82 	thread_id sender;
83 	(void)receive_data(&sender, NULL, 0);
84 
85     printf("Retrieving names ...\n");
86 
87     for (int32 index = 0 ; index < dAgent->RetrieveDevices(0).CountItems(); index++ ) {
88 
89         RemoteDevice* rDevice = dAgent->RetrieveDevices(0).ItemAt(index);
90 	    printf("\t%s \t@ %s ...\n", rDevice->GetFriendlyName(true).String(), bdaddrUtils::ToString(rDevice->GetBluetoothAddress()));
91 
92     }
93 
94 }
95 
96 static status_t
97 LocalDeviceError(status_t status)
98 {
99     fprintf(stderr,"No Device/s found");
100 
101     return status;
102 }
103 
104 
105 int
106 main(int argc, char *argv[])
107 {
108 
109 	mainThread = find_thread(NULL);
110 
111         if (argc == 2) {
112             // device specified
113             LocalDevice* device = LocalDevice::GetLocalDevice(atoi(argv[0]));
114             if (device == NULL)
115                return LocalDeviceError(ENODEV);
116 
117             DumpInfo(device);
118 
119         } else if (argc == 1) {
120             // show all devices
121             LocalDevice* device = NULL;
122 
123             printf("Performing discovery for %ld Bluetooth Local Devices ...\n", LocalDevice::GetLocalDeviceCount());
124 
125             for (uint32 index = 0 ; index < LocalDevice::GetLocalDeviceCount() ; index++) {
126 
127                device = LocalDevice::GetLocalDevice();
128                if (device == NULL) {
129                     LocalDeviceError(ENODEV);
130                     continue;
131                }
132                DumpInfo(device);
133 
134             }
135 
136             return B_OK;
137 
138         } else {
139                 fprintf(stderr,"Usage: bt_dev_info [device]\n");
140                 return B_ERROR;
141         }
142 }
143