Kallisto Linux API
Library for interacting with Kallisto devices
bluetooth_adapter.hpp
Go to the documentation of this file.
1 
42 #ifndef BLUETOOTH_ADAPTER_HPP_
43 #define BLUETOOTH_ADAPTER_HPP_
44 
45 #include <map>
46 #include <vector>
47 #include <memory>
48 #include <chrono>
49 
50 #include "../../util/error/kallisto_error.hpp"
51 #include "../device/bluetooth_device.hpp"
52 
53 namespace kallisto {
54 namespace bluetooth {
55 
56 class BluetoothAdapterNative; //Forward declaration of the native implementation
57 
63 class BluetoothAdapter {
64 public:
68  class Listener {
69  protected:
70  virtual ~Listener(){}
71 
72  public:
76  enum class Error {
77  USER_REJECTED,
78  STACK_ERROR,
79  ALREADY_ENABLED
80  };
81 
86  virtual void onAdapterEnabled(BluetoothAdapter& adapter) = 0;
87 
93 
98  virtual void onAdapterDisabled(BluetoothAdapter& adapter) = 0;
99  };
100 
104  class ScanListener {
105  protected:
106  virtual ~ScanListener(){}
107 
108  public:
112  class ScanRecord {
113  public:
117  const std::chrono::nanoseconds timestamp;
118 
122  const std::map<std::string, std::vector<uint8_t>> service_data;
123 
127  const std::map<uint16_t, std::vector<uint8_t>> manufacturer_data;
128 
132  const int rssi;
133 
138  ScanRecord() : timestamp(std::chrono::nanoseconds(0)), rssi(0) {}
139 
144  ScanRecord(std::chrono::nanoseconds _timestamp, std::map<std::string, std::vector<uint8_t>> _service_data, std::map<uint16_t, std::vector<uint8_t>> _manufacturer_data, int _rssi):
145  timestamp(_timestamp), service_data(_service_data), manufacturer_data(_manufacturer_data), rssi(_rssi){}
146  };
147 
151  virtual void onScanStart() = 0;
152 
156  virtual void onScanStartFailed() = 0;
157 
162  virtual void onScanStop(std::vector<BluetoothDevice*> devices_found) = 0;
163 
169  virtual void onScan(BluetoothDevice& device, const ScanRecord record) = 0;
170  };
171 
172 private:
173  //Holds the native implementation
174  std::unique_ptr<BluetoothAdapterNative> m_native_implementation;
175 
176  //Adapter ID
177  std::string m_id;
178 
179 public:
183  BluetoothAdapter(std::string id);
184 
189 
193  BluetoothAdapter(BluetoothAdapter const&) = delete;
194  void operator=(BluetoothAdapter const&) = delete;
195 
200 
204  BluetoothAdapter& operator= (BluetoothAdapter&& other);
205 
210  std::string getId() const {
211  return m_id;
212  }
213 
219  error::KallistoDetailedResult enable();
220 
225  error::KallistoDetailedResult disable();
226 
231  bool isEnabled();
232 
237  const std::vector<BluetoothDevice*> getDevices();
238 
243  BluetoothDevice* getDevice(std::string address);
244 
250  error::KallistoDetailedResult registerListener(BluetoothAdapter::Listener& listener);
251 
255  void unregisterListener();
256 
261  bool isScanning();
262 
267  error::KallistoDetailedResult startScan(BluetoothAdapter::ScanListener& listener);
268 
273  error::KallistoDetailedResult stopScan();
274 
275  bool operator== (const BluetoothAdapter &other) const {
276  return (this->getId() == other.getId());
277  }
278 
279  bool operator!= (const BluetoothAdapter &other) const {
280  return !(*this == other);
281  }
282 };
283 
284 } /* namespace bluetooth */
285 } /* namespace kallisto */
286 
287 #endif /* BLUETOOTH_ADAPTER_HPP_ */
BluetoothDevice * getDevice(std::string address)
Definition: bluetooth_adapter.hpp:52
virtual void onAdapterEnabled(BluetoothAdapter &adapter)=0
Error
Definition: bluetooth_adapter.hpp:75
const std::vector< BluetoothDevice * > getDevices()
error::KallistoDetailedResult registerListener(BluetoothAdapter::Listener &listener)
error::KallistoDetailedResult startScan(BluetoothAdapter::ScanListener &listener)
error::KallistoDetailedResult stopScan()
virtual void onAdapterEnableFailed(BluetoothAdapter &adapter, BluetoothAdapter::Listener::Error error)=0
error::KallistoDetailedResult disable()
virtual void onAdapterDisabled(BluetoothAdapter &adapter)=0
error::KallistoDetailedResult enable()
std::string getId() const
Definition: bluetooth_adapter.hpp:209