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