• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @addtogroup Bluetooth
18  * @{
19  *
20  * @brief Defines central manager, including scan filter and settings, and common functions.
21  *
22  * @since 6
23  */
24 
25 /**
26  * @file bluetooth_ble_central_manager.h
27  *
28  * @brief Central manager common functions.
29  *
30  * @since 6
31  */
32 
33 #ifndef BLUETOOTH_BLE_CENTRAL_MANAGER_H
34 #define BLUETOOTH_BLE_CENTRAL_MANAGER_H
35 
36 #include "bluetooth_def.h"
37 #include "bluetooth_types.h"
38 #include "bluetooth_remote_device.h"
39 
40 namespace OHOS {
41 namespace Bluetooth {
42 /**
43  * @brief Represents scan result.
44  *
45  * @since 6
46  */
47 class BLUETOOTH_API BleScanResult {
48 public:
49     /**
50      * @brief A constructor used to create a <b>BleScanResult</b> instance.
51      *
52      * @since 6
53      */
54     BleScanResult();
55 
56     /**
57      * @brief A destructor used to delete the <b>BleScanResult</b> instance.
58      *
59      * @since 6
60      */
61     ~BleScanResult();
62 
63     /**
64      * @brief Get service uuids.
65      *
66      * @return Returns service uuids.
67      * @since 6
68      */
69     std::vector<UUID> GetServiceUuids() const;
70 
71     /**
72      * @brief Get manufacture data.
73      *
74      * @return Returns manufacture data.
75      * @since 6
76      */
77     std::map<uint16_t, std::string> GetManufacturerData() const;
78 
79     /**
80      * @brief Get service data.
81      *
82      * @return Returns service data.
83      * @since 6
84      */
85     std::map<UUID, std::string> GetServiceData() const;
86 
87     /**
88      * @brief Get peripheral device.
89      *
90      * @return Returns peripheral device pointer.
91      * @since 6
92      */
93     BluetoothRemoteDevice GetPeripheralDevice() const;
94 
95     /**
96      * @brief Get peer device rssi.
97      *
98      * @return Returns peer device rssi.
99      * @since 6
100      */
101     int8_t GetRssi() const;
102 
103     /**
104      * @brief Check if device is connctable.
105      *
106      * @return Returns <b>true</b> if device is connctable;
107      *         returns <b>false</b> if device is not connctable.
108      * @since 6
109      */
110     bool IsConnectable() const;
111 
112     /**
113      * @brief Get advertiser flag.
114      *
115      * @return Returns advertiser flag.
116      * @since 6
117      */
118     uint8_t GetAdvertiseFlag() const;
119 
120     /**
121      * @brief Get payload.
122      *
123      * @return Returns payload.
124      * @since 6
125      */
126     std::vector<uint8_t> GetPayload() const;
127     /**
128      * @brief Add manufacture data.
129      *
130      * @param manufacturerId Manufacture Id which addad data.
131      * @param data Manufacture data
132      * @since 6
133      */
134 
135     void AddManufacturerData(uint16_t manufacturerId, const std::string &data);
136 
137     /**
138      * @brief Add service data.
139      *
140      * @param uuid Uuid of service data.
141      * @param serviceData Service data.
142      * @since 6
143      */
144     void AddServiceData(const UUID &uuid, const std::string &serviceData);
145 
146     /**
147      * @brief Add service uuid.
148      *
149      * @param serviceUuid Service uuid.
150      * @since 6
151      */
152     void AddServiceUuid(const UUID &serviceUuid);
153 
154     /**
155      * @brief Add data to payload.
156      *
157      * @param payload Added payload.
158      * @since 6
159      */
160     void SetPayload(std::string payload);
161 
162     /**
163      * @brief Set peripheral device.
164      *
165      * @param device Remote device.
166      * @since 6
167      */
168     void SetPeripheralDevice(const BluetoothRemoteDevice &device);
169 
170     /**
171      * @brief Set peer device rssi.
172      *
173      * @param rssi Peer device rssi.
174      * @since 6
175      */
176     void SetRssi(int8_t rssi);
177 
178     /**
179      * @brief Set connectable.
180      *
181      * @param connectable Whether it is connectable.
182      * @since 6
183      */
184     void SetConnectable(bool connectable);
185 
186     /**
187      * @brief Set advertiser flag.
188      *
189      * @param flag Advertiser flag.
190      * @since 6
191      */
192     void SetAdvertiseFlag(uint8_t flag);
193 
194     void SetName(const std::string &name);
195     std::string GetName(void);
196 
197 private:
198     std::vector<UUID> serviceUuids_ {};
199     std::map<uint16_t, std::string> manufacturerSpecificData_ {};
200     std::map<UUID, std::string> serviceData_ {};
201     BluetoothRemoteDevice peripheralDevice_ {};
202     int8_t rssi_ {};
203     bool connectable_ {};
204     uint8_t advertiseFlag_ {};
205     std::vector<uint8_t> payload_ {};
206     std::string name_ {};
207 };
208 /**
209  * @brief Represents central manager callback.
210  *
211  * @since 6
212  */
213 class BleCentralManagerCallback {
214 public:
215     /**
216      * @brief A destructor used to delete the <b>BleCentralManagerCallback</b> instance.
217      *
218      * @since 6
219      */
220     virtual ~BleCentralManagerCallback() = default;
221 
222     /**
223      * @brief Scan result callback.
224      *
225      * @param result Scan result.
226      * @since 6
227      */
228     virtual void OnScanCallback(const BleScanResult &result) = 0;
229 
230     /**
231      * @brief Batch scan results event callback.
232      *
233      * @param results Scan results.
234      * @since 6
235      */
236     virtual void OnBleBatchScanResultsEvent(const std::vector<BleScanResult> &results) = 0;
237 
238     /**
239      * @brief Start or Stop scan event callback.
240      *
241      * @param resultCode Scan result code.
242      * @param isStartScan true->start scan, false->stop scan.
243      * @since 6
244      */
245     virtual void OnStartOrStopScanEvent(int resultCode, bool isStartScan) = 0;
246 };
247 
248 /**
249  * @brief Represents Scan settings.
250  *
251  * @since 6
252  */
253 class BLUETOOTH_API BleScanSettings {
254 public:
255     /**
256      * @brief A constructor used to create a <b>BleScanSettings</b> instance.
257      *
258      * @since 6
259      */
260     BleScanSettings();
261 
262     /**
263      * @brief A destructor used to delete the <b>BleScanSettings</b> instance.
264      *
265      * @since 6
266      */
267     ~BleScanSettings();
268 
269     /**
270      * @brief Set repport delay time.
271      *
272      * @param reportDelayMillis Repport delay time.
273      * @since 6
274      */
275     void SetReportDelay(long reportDelayMillis = 0);
276 
277     /**
278      * @brief Get repport delay time.
279      *
280      * @return Returns Repport delay time.
281      * @since 6
282      */
283     long GetReportDelayMillisValue() const;
284 
285     /**
286      * @brief Set scan mode.
287      *
288      * @param scanMode Scan mode.
289      * @return Returns <b>true</b> if set scanMode successfully;
290      *         returns <b>false</b> if set scanMode failed.
291      * @since 6
292      */
293     int SetScanMode(int scanMode);
294 
295     /**
296      * @brief Get scan mode.
297      *
298      * @return Scan mode.
299      * @since 6
300      */
301     int GetScanMode() const;
302 
303     /**
304      * @brief Set legacy flag.
305      *
306      * @param legacy Legacy value.
307      * @since 6
308      */
309     void SetLegacy(bool legacy);
310 
311     /**
312      * @brief Get legacy flag.
313      *
314      * @return Legacy flag.
315      * @since 6
316      */
317     bool GetLegacy() const;
318 
319     /**
320      * @brief Set phy value.
321      *
322      * @param phy Phy value.
323      * @since 6
324      */
325     void SetPhy(int phy);
326 
327     /**
328      * @brief Get phy value.
329      *
330      * @return Phy value.
331      * @since 6
332      */
333     int GetPhy() const;
334 
335 private:
336     long reportDelayMillis_ = 0;
337     int scanMode_ = SCAN_MODE_LOW_POWER;
338     bool legacy_ = true;
339     int phy_ = PHY_LE_ALL_SUPPORTED;
340 };
341 
342 /**
343  * @brief Represents Scan filter.
344  *
345  */
346 class BLUETOOTH_API BleScanFilter {
347 public:
348     /**
349      * @brief A constructor used to create a <b>BleScanFilter</b> instance.
350      *
351      */
352     BleScanFilter();
353 
354     /**
355      * @brief A destructor used to delete the <b>BleScanFilter</b> instance.
356      *
357      */
358     ~BleScanFilter();
359 
360     void SetDeviceId(std::string deviceId);
361 
362     std::string GetDeviceId() const;
363 
364     void SetName(std::string name);
365 
366     std::string GetName() const;
367 
368     void SetServiceUuid(const UUID &uuid);
369 
370     bool HasServiceUuid();
371 
372     UUID GetServiceUuid() const;
373 
374     void SetServiceUuidMask(const UUID &serviceUuidMask);
375 
376     bool HasServiceUuidMask();
377 
378     UUID GetServiceUuidMask() const;
379 
380     void SetServiceSolicitationUuid(const UUID &serviceSolicitationUuid);
381 
382     bool HasSolicitationUuid();
383 
384     UUID GetServiceSolicitationUuid() const;
385 
386     void SetServiceSolicitationUuidMask(const UUID &erviceSolicitationUuidMask);
387 
388     bool HasSolicitationUuidMask();
389 
390     UUID GetServiceSolicitationUuidMask() const;
391 
392     void SetServiceData(std::vector<uint8_t> serviceData);
393 
394     std::vector<uint8_t> GetServiceData() const;
395 
396     void SetServiceDataMask(std::vector<uint8_t> serviceDataMask);
397 
398     std::vector<uint8_t> GetServiceDataMask() const;
399 
400     void SetManufacturerId(uint16_t manufacturerId);
401 
402     uint16_t GetManufacturerId() const;
403 
404     void SetManufactureData(std::vector<uint8_t> manufactureData);
405 
406     std::vector<uint8_t> GetManufactureData() const;
407 
408     void SetManufactureDataMask(std::vector<uint8_t> manufactureDataMask);
409 
410     std::vector<uint8_t> GetManufactureDataMask() const;
411 
412     private:
413         std::string deviceId_;
414         std::string name_;
415 
416         UUID serviceUuid_;
417         UUID serviceUuidMask_;
418         UUID serviceSolicitationUuid_;
419         UUID serviceSolicitationUuidMask_;
420         bool hasServiceUuid_ = false;
421         bool hasServiceUuidMask_ = false;
422         bool hasSolicitationUuid_ = false;
423         bool hasSolicitationUuidMask_ = false;
424 
425         std::vector<uint8_t> serviceData_;
426         std::vector<uint8_t> serviceDataMask_;
427 
428         uint16_t manufacturerId_ = 0;
429         std::vector<uint8_t> manufactureData_;
430         std::vector<uint8_t> manufactureDataMask_;
431 };
432 
433 /**
434  * @brief Represents central manager.
435  *
436  * @since 6
437  */
438 class BLUETOOTH_API BleCentralManager {
439 public:
440     /**
441      * @brief A constructor used to create a <b>BleCentralManager</b> instance.
442      *
443      * @param cllback Central manager callback to create an <b>BleCentralManagerCallback</b> instance.
444      * @since 6
445      */
446     explicit BleCentralManager(BleCentralManagerCallback &callback);
447 
448     /**
449      * @brief A destructor used to delete the <b>BleCentralManager</b> instance.
450      *
451      * @since 6
452      */
453     ~BleCentralManager();
454 
455     /**
456      * @brief Start scan.
457      *
458      * @since 6
459      */
460     int StartScan();
461 
462     /**
463      * @brief Start scan.
464      *
465      * @param settings Scan settings.
466      * @since 6
467      */
468     int StartScan(const BleScanSettings &settings);
469 
470     /**
471      * @brief Stop scan.
472      *
473      * @since 6
474      */
475     int StopScan();
476 
477     /**
478      * @brief Config scan filter.
479      *
480      */
481     int ConfigScanFilter(const std::vector<BleScanFilter> &filter);
482 
483 private:
484     BleCentralManagerCallback *callback_ = nullptr;
485     int clientId_ = 0;
486 
487     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BleCentralManager);
488     BLUETOOTH_DECLARE_IMPL();
489 };
490 } // namespace Bluetooth
491 } // namespace OHOS
492 #endif