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