forked from kn65op/bluez-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBluezBluetooth.cpp
More file actions
160 lines (139 loc) · 3.16 KB
/
Copy pathBluezBluetooth.cpp
File metadata and controls
160 lines (139 loc) · 3.16 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* File: BluezBluetooth.cpp
* Author: tomko
*
* Created on 26 marzec 2012, 23:21
*/
#include "BluezBluetooth.h"
#include "equalMAC.h"
#include "equalName.h"
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <algorithm>
BluezBluetooth::BluezBluetooth()
{
}
BluezBluetooth::BluezBluetooth(const BluezBluetooth& orig)
{
}
BluezBluetooth::~BluezBluetooth()
{
}
std::list<Device> BluezBluetooth::getDevices()
{
return devices;
}
void BluezBluetooth::scanDevices() throw (BluezBluetooth::BluetoothError)
{
scan();
}
void BluezBluetooth::scan() throw (BluezBluetooth::BluetoothError)
{
devices.clear();
inquiry_info *ii = NULL;
int max_rsp, num_rsp;
int dev_id, sock, len, flags;
int i;
char addr[19] = {0};
char name[248] = {0};
dev_id = hci_get_route(NULL);
if (dev_id == -1)
{
throw BluetoothError("No local device.");
}
sock = hci_open_dev(dev_id);
if (sock < 0)
{
throw BluetoothError("Cannot open socket");
}
len = 8;
max_rsp = 255;
flags = IREQ_CACHE_FLUSH;
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) throw BluetoothError("hci_inquiry");
for (i = 0; i < num_rsp; i++)
{
ba2str(&(ii + i)->bdaddr, addr);
memset(name, 0, sizeof (name));
if (hci_read_remote_name(sock, &(ii + i)->bdaddr, sizeof (name), name, 0) < 0)
{
strcpy(name, "[unknown]");
}
devices.push_back(Device(addr, name));
}
free(ii);
close(sock);
}
Device BluezBluetooth::getDeviceByMAC(std::string MAC) throw (BluezBluetooth::NotFound)
{
std::list<Device>::iterator found = findByMAC(MAC);
if (found == devices.end())
{
throw BluezBluetooth::NotFound();
}
return *found;
}
Device BluezBluetooth::getDeviceByName(std::string name) throw (BluezBluetooth::NotFound)
{
std::list<Device>::iterator found = findByName(name);
if (found == devices.end())
{
throw BluezBluetooth::NotFound();
}
return *found;
}
std::list<Device*> BluezBluetooth::getDevicesPointers()
{
std::list<Device*> ret;
std::list<Device>::iterator it, end;
for (it = devices.begin(), end = devices.end(); it != end; ++it)
{
ret.push_back(new Device(*it));
}
}
bool BluezBluetooth::deleteByMAC(std::string MAC)
{
std::list<Device>::iterator it = findByMAC(MAC);
if (it != devices.end())
{
devices.erase(it);
return true;
}
return false;
}
bool BluezBluetooth::deleteByName(std::string name)
{
std::list<Device>::iterator it = findByName(name);
if (it != devices.end())
{
devices.erase(it);
return true;
}
return false;
}
std::list<Device>::iterator BluezBluetooth::findByMAC(std::string MAC)
{
return find_if(devices.begin(), devices.end(), EqalMAC(MAC));
}
std::list<Device>::iterator BluezBluetooth::findByName(std::string name)
{
return find_if(devices.begin(), devices.end(), EqalName(name));
}
bool BluezBluetooth::isDeviceOn()
{
if (hci_get_route(NULL) == -1)
{
return false;
}
return true;
}
int BluezBluetooth::size()
{
return devices.size();
}