• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 
195 private:
196     std::vector<UUID> serviceUuids_ {};
197     std::map<uint16_t, std::string> manufacturerSpecificData_ {};
198     std::map<UUID, std::string> serviceData_ {};
199     BluetoothRemoteDevice peripheralDevice_ {};
200     int8_t rssi_ {};
201     bool connectable_ {};
202     uint8_t advertiseFlag_ {};
203     std::vector<uint8_t> payload_ {};
204 };
205 /**
206  * @brief Represents central manager callback.
207  *
208  * @since 6
209  */
210 class BleCentralManagerCallback {
211 public:
212     /**
213      * @brief A destructor used to delete the <b>BleCentralManagerCallback</b> instance.
214      *
215      * @since 6
216      */
217     virtual ~BleCentralManagerCallback() = default;
218 
219     /**
220      * @brief Scan result callback.
221      *
222      * @param result Scan result.
223      * @since 6
224      */
225     virtual void OnScanCallback(const BleScanResult &result) = 0;
226 
227     /**
228      * @brief Batch scan results event callback.
229      *
230      * @param results Scan results.
231      * @since 6
232      */
233     virtual void OnBleBatchScanResultsEvent(const std::vector<BleScanResult> &results) = 0;
234 
235     /**
236      * @brief Start scan failed callback.
237      *
238      * @param resultCode Scan result code.
239      * @since 6
240      */
241     virtual void OnStartScanFailed(int resultCode) = 0;
242 };
243 
244 /**
245  * @brief Represents Scan settings.
246  *
247  * @since 6
248  */
249 class BLUETOOTH_API BleScanSettings {
250 public:
251     /**
252      * @brief A constructor used to create a <b>BleScanSettings</b> instance.
253      *
254      * @since 6
255      */
256     BleScanSettings();
257 
258     /**
259      * @brief A destructor used to delete the <b>BleScanSettings</b> instance.
260      *
261      * @since 6
262      */
263     ~BleScanSettings();
264 
265     /**
266      * @brief Set repport delay time.
267      *
268      * @param reportDelayMillis Repport delay time.
269      * @since 6
270      */
271     void SetReportDelay(long reportDelayMillis = 0);
272 
273     /**
274      * @brief Get repport delay time.
275      *
276      * @return Returns Repport delay time.
277      * @since 6
278      */
279     long GetReportDelayMillisValue() const;
280 
281     /**
282      * @brief Set scan mode.
283      *
284      * @param scanMode Scan mode.
285      * @return Returns <b>true</b> if set scanMode successfully;
286      *         returns <b>false</b> if set scanMode failed.
287      * @since 6
288      */
289     int SetScanMode(int scanMode);
290 
291     /**
292      * @brief Get scan mode.
293      *
294      * @return Scan mode.
295      * @since 6
296      */
297     int GetScanMode() const;
298 
299     /**
300      * @brief Set legacy flag.
301      *
302      * @param legacy Legacy value.
303      * @since 6
304      */
305     void SetLegacy(bool legacy);
306 
307     /**
308      * @brief Get legacy flag.
309      *
310      * @return Legacy flag.
311      * @since 6
312      */
313     bool GetLegacy() const;
314 
315     /**
316      * @brief Set phy value.
317      *
318      * @param phy Phy value.
319      * @since 6
320      */
321     void SetPhy(int phy);
322 
323     /**
324      * @brief Get phy value.
325      *
326      * @return Phy value.
327      * @since 6
328      */
329     int GetPhy() const;
330 
331 private:
332     long reportDelayMillis_ = 0;
333     int scanMode_ = SCAN_MODE_LOW_POWER;
334     bool legacy_ = true;
335     int phy_ = PHY_LE_ALL_SUPPORTED;
336 };
337 
338 /**
339  * @brief Represents central manager.
340  *
341  * @since 6
342  */
343 class BLUETOOTH_API BleCentralManager {
344 public:
345     /**
346      * @brief A constructor used to create a <b>BleCentralManager</b> instance.
347      *
348      * @param cllback Central manager callback to create an <b>BleCentralManagerCallback</b> instance.
349      * @since 6
350      */
351     explicit BleCentralManager(BleCentralManagerCallback &callback);
352 
353     /**
354      * @brief A destructor used to delete the <b>BleCentralManager</b> instance.
355      *
356      * @since 6
357      */
358     ~BleCentralManager();
359 
360     /**
361      * @brief Start scan.
362      *
363      * @since 6
364      */
365     void StartScan();
366 
367     /**
368      * @brief Start scan.
369      *
370      * @param settings Scan settings.
371      * @since 6
372      */
373     void StartScan(const BleScanSettings &settings);
374 
375     /**
376      * @brief Stop scan.
377      *
378      * @since 6
379      */
380     void StopScan();
381 
382 private:
383     BleCentralManagerCallback *callback_ = nullptr;
384 
385     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BleCentralManager);
386     BLUETOOTH_DECLARE_IMPL();
387 };
388 } // namespace Bluetooth
389 } // namespace OHOS
390 #endif