-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbluetoothpoller.cpp
123 lines (95 loc) · 2.37 KB
/
bluetoothpoller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
Office presence sensor monitoring Bluetooth devices
Copyright (C) 2012-2013 Tuomas Haapala, Nemein <tuomas@nemein.com>
*/
#include "bluetoothpoller.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
BluetoothPoller::BluetoothPoller() : m_socket(0)
{
}
BluetoothPoller::~BluetoothPoller()
{
shutdown();
}
bool BluetoothPoller::init(std::string& address)
{
// open bt socket
int dev_id = hci_get_route(NULL);
m_socket = hci_open_dev( dev_id );
if (dev_id < 0 || m_socket < 0)
{
m_lastErrorString = "Cannot open bluetooth socket";
return false;
}
// get device address
hci_dev_info di;
hci_devinfo(dev_id, &di);
char addr[19] = {0};
ba2str(&di.bdaddr, addr);
address = addr;
m_lastErrorString = "";
return true;
}
void BluetoothPoller::shutdown()
{
close(m_socket);
}
bool BluetoothPoller::scanDevice(std::string BTAddress)
{
char name[248] = {0};
bdaddr_t ba;
str2ba(BTAddress.c_str(), &ba);
int res = hci_read_remote_name(m_socket, &ba, sizeof(name), name, 0);
if (res == -1)
{
return false;
}
else
{
return true;
}
}
bool BluetoothPoller::discoverDevices(std::vector<DiscoveredDevice>& discoveredDevices)
{
int num_rsp = 0;
int dev_id = 0;
char addr[19] = {0};
char name[248] = {0};
int len = 8;
int max_rsp = 255;
int flags = IREQ_CACHE_FLUSH;
inquiry_info *ii = NULL;
ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info));
num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);
if( num_rsp < 0 )
{
m_lastErrorString = "hci_inquiry error";
return false;
}
discoveredDevices.clear();
for (int i = 0; i < num_rsp; i++) {
ba2str(&(ii+i)->bdaddr, addr);
std::string str(addr);
if (hci_read_remote_name(m_socket, &(ii+i)->bdaddr, sizeof(name), name, 0) < 0)
{
strcpy(name, "[unknown]");
}
DiscoveredDevice newDevice;
newDevice.btAddress = addr;
newDevice.name = name;
discoveredDevices.push_back(newDevice);
}
free(ii);
m_lastErrorString = "";
return true;
}
std::string BluetoothPoller::getLastErrorString()
{
return m_lastErrorString;
}