• 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 bluetooth host, including observer and common functions.
21  *
22  * @since 6
23  */
24 
25 /**
26  * @file bluetooth_host.h
27  *
28  * @brief Framework bluetooth host interface.
29  *
30  * @since 6
31  */
32 
33 #ifndef BLUETOOTH_HOST_H
34 #define BLUETOOTH_HOST_H
35 
36 #include <string>
37 
38 #include "bluetooth_def.h"
39 #include "bluetooth_types.h"
40 #include "bluetooth_remote_device.h"
41 #include "bluetooth_device_class.h"
42 
43 namespace OHOS {
44 namespace Bluetooth {
45 /**
46  * @brief Represents framework host device basic observer.
47  *
48  * @since 6
49  */
50 class BluetoothHostObserver {
51 public:
52     /**
53      * @brief A destructor used to delete the <b>BluetoothHostObserver</b> instance.
54      *
55      * @since 6
56      */
57     virtual ~BluetoothHostObserver() = default;
58 
59     // common
60     /**
61      * @brief Adapter state change function.
62      *
63      * @param transport Transport type when state change.
64      *        BTTransport::ADAPTER_BREDR : classic;
65      *        BTTransport::ADAPTER_BLE : ble.
66      * @param state Change to the new state.
67      *        BTStateID::STATE_TURNING_ON;
68      *        BTStateID::STATE_TURN_ON;
69      *        BTStateID::STATE_TURNING_OFF;
70      *        BTStateID::STATE_TURN_OFF.
71      * @since 6
72      */
73     virtual void OnStateChanged(const int transport, const int status) = 0;
74 
75     // gap
76     /**
77      * @brief Discovery state changed observer.
78      *
79      * @param status Device discovery status.
80      * @since 6
81      */
82     virtual void OnDiscoveryStateChanged(int status) = 0;
83 
84     /**
85      * @brief Discovery result observer.
86      *
87      * @param device Remote device.
88      * @since 6
89      */
90     virtual void OnDiscoveryResult(const BluetoothRemoteDevice &device) = 0;
91 
92     /**
93      * @brief Pair request observer.
94      *
95      * @param device Remote device.
96      * @since 6
97      */
98     virtual void OnPairRequested(const BluetoothRemoteDevice &device) = 0;
99 
100     /**
101      * @brief Pair confirmed observer.
102      *
103      * @param device Remote device.
104      * @param reqType Pair type.
105      * @param number Paired passkey.
106      * @since 6
107      */
108     virtual void OnPairConfirmed(const BluetoothRemoteDevice &device, int reqType, int number) = 0;
109 
110     /**
111      * @brief Scan mode changed observer.
112      *
113      * @param mode Device scan mode.
114      * @since 6
115      */
116     virtual void OnScanModeChanged(int mode) = 0;
117 
118     /**
119      * @brief Device name changed observer.
120      *
121      * @param deviceName Device name.
122      * @since 6
123      */
124     virtual void OnDeviceNameChanged(const std::string &deviceName) = 0;
125 
126     /**
127      * @brief Device address changed observer.
128      *
129      * @param address Device address.
130      * @since 6
131      */
132     virtual void OnDeviceAddrChanged(const std::string &address) = 0;
133 };
134 
135 /**
136  * @brief Represents remote device observer.
137  *
138  * @since 6
139  */
140 class BluetoothRemoteDeviceObserver {
141 public:
142     /**
143      * @brief A destructor used to delete the <b>BluetoothRemoteDeviceObserver</b> instance.
144      *
145      * @since 6
146      */
147     virtual ~BluetoothRemoteDeviceObserver() = default;
148 
149     /**
150      * @brief Pair status changed observer.
151      *
152      * @param device Remote device.
153      * @param status Remote device pair status.
154      * @since 6
155      */
156     virtual void OnPairStatusChanged(const BluetoothRemoteDevice &device, int status) = 0;
157 
158     /**
159      * @brief Remote uuid changed observer.
160      *
161      * @param device Remote device.
162      * @param uuids Remote device uuids.
163      * @since 6
164      */
165     virtual void OnRemoteUuidChanged(const BluetoothRemoteDevice &device, const std::vector<ParcelUuid> &uuids) = 0;
166 
167     /**
168      * @brief Remote name changed observer.
169      *
170      * @param device Remote device.
171      * @param deviceName Remote device name.
172      * @since 6
173      */
174     virtual void OnRemoteNameChanged(const BluetoothRemoteDevice &device, const std::string &deviceName) = 0;
175 
176     /**
177      * @brief Remote alias changed observer.
178      *
179      * @param device Remote device.
180      * @param alias Remote device alias.
181      * @since 6
182      */
183     virtual void OnRemoteAliasChanged(const BluetoothRemoteDevice &device, const std::string &alias) = 0;
184 
185     /**
186      * @brief Remote cod changed observer.
187      *
188      * @param device Remote device.
189      * @param cod Remote device cod.
190      * @since 6
191      */
192     virtual void OnRemoteCodChanged(const BluetoothRemoteDevice &device, const BluetoothDeviceClass &cod) = 0;
193 
194     /**
195      * @brief Remote battery level changed observer.
196      *
197      * @param device Remote device.
198      * @param cod Remote device battery Level.
199      * @since 6
200      */
201     virtual void OnRemoteBatteryLevelChanged(const BluetoothRemoteDevice &device, int batteryLevel) = 0;
202 
203     /**
204      * @brief Remote rssi event observer.
205      *
206      * @param device Remote device.
207      * @param rssi Remote device rssi.
208      * @param status Read status.
209      * @since 6
210      */
211     virtual void OnReadRemoteRssiEvent(const BluetoothRemoteDevice &device, int rssi, int status) = 0;
212 };
213 
214 /**
215  * @brief Represents framework host device.
216  *
217  * @since 6
218  */
219 class BLUETOOTH_API BluetoothHost {
220 public:
221     // common
222     /**
223      * @brief Get default host device.
224      *
225      * @return Returns the singleton instance.
226      * @since 6
227      */
228     static BluetoothHost &GetDefaultHost();
229 
230     /**
231      * @brief Get remote device instance.
232      *
233      * @param addr Remote device address.
234      * @param transport Adapter transport.
235      * @return Returns remote device instance.
236      * @since 6
237      */
238     BluetoothRemoteDevice GetRemoteDevice(const std::string &addr, int transport) const;
239 
240     /**
241      * @brief Register observer.
242      *
243      * @param observer Class BluetoothHostObserver pointer to register observer.
244      * @since 6
245      */
246     void RegisterObserver(BluetoothHostObserver &observer);
247 
248     /**
249      * @brief Deregister observer.
250      *
251      * @param observer Class BluetoothHostObserver pointer to deregister observer.
252      * @since 6
253      */
254     void DeregisterObserver(BluetoothHostObserver &observer);
255 
256     /**
257      * @brief Enable classic.
258      *
259      * @return Returns <b>true</b> if the operation is accepted;
260      *         returns <b>false</b> if the operation is rejected.
261      * @since 6
262      */
263     bool EnableBt();
264 
265     /**
266      * @brief Disable classic.
267      *
268      * @return Returns <b>true</b> if the operation is accepted;
269      *         returns <b>false</b> if the operation is rejected.
270      * @since 6
271      */
272     bool DisableBt();
273 
274     /**
275      * @brief Get classic enable/disable state.
276      *
277      * @return Returns classic enable/disable state.
278      *         BTStateID::STATE_TURNING_ON;
279      *         BTStateID::STATE_TURN_ON;
280      *         BTStateID::STATE_TURNING_OFF;
281      *         BTStateID::STATE_TURN_OFF.
282      * @since 6
283      */
284     int GetBtState() const;
285 
286     /**
287      * @brief Disable ble.
288      *
289      * @return Returns <b>true</b> if the operation is accepted;
290      *         returns <b>false</b> if the operation is rejected.
291      * @since 6
292      */
293     bool DisableBle();
294 
295     /**
296      * @brief Enable ble.
297      *
298      * @return Returns <b>true</b> if the operation is accepted;
299      *         returns <b>false</b> if the operation is rejected.
300      * @since 6
301      */
302     bool EnableBle();
303 
304     /**
305      * @brief Get ble enable/disable state.
306      *
307      * @return Returns <b>true</b> if ble is enabled;
308      *         returns <b>false</b> if ble is not enabled.
309      * @since 6
310      */
311     bool IsBleEnabled() const;
312 
313     /**
314      * @brief Factory reset bluetooth service.
315      *
316      * @return Returns <b>true</b> if the operation is successful;
317      *         returns <b>false</b> if the operation fails.
318      * @since 6
319      */
320     bool BluetoothFactoryReset();
321 
322     /**
323      * @brief Get profile service ID list.
324      *
325      * @return Returns vector of enabled profile services ID.
326      * @since 6
327      */
328     std::vector<uint32_t> GetProfileList() const;
329 
330     /**
331      * @brief Get max audio connected devices number.
332      *
333      * @return Returns max device number that audio can connect.
334      * @since 6
335      */
336     int GetMaxNumConnectedAudioDevices() const;
337 
338     /**
339      * @brief Get bluetooth connects state.
340      *
341      * @return Returns bluetooth connects state.
342      *         BTConnectState::CONNECTING;
343      *         BTConnectState::CONNECTED;
344      *         BTConnectState::DISCONNECTING;
345      *         BTConnectState::DISCONNECTED.
346      * @since 6
347      */
348     int GetBtConnectionState() const;
349 
350     /**
351      * @brief Get profile service connect state.
352      *
353      * @param profileID Profile service ID.
354      * @return Returns connect state for designated profile service.
355      *         BTConnectState::CONNECTING;
356      *         BTConnectState::CONNECTED;
357      *         BTConnectState::DISCONNECTING;
358      *         BTConnectState::DISCONNECTED.
359      * @since 6
360      */
361     int GetBtProfileConnState(uint32_t profileId) const;
362 
363     /**
364      * @brief Get local device supported uuids.
365      *
366      * @param[out] Vector which use to return support uuids.
367      * @since 6
368      */
369     void GetLocalSupportedUuids(std::vector<ParcelUuid> &uuids);
370 
371     /**
372      * @brief Start adapter manager, passthrough only.
373      *
374      * @return Returns <b>true</b> if the operation is successful;
375      *         returns <b>false</b> if the operation fails.
376      * @since 6
377      */
378     bool Start();
379 
380     /**
381      * @brief Stop adapter manager, passthrough only.
382      *
383      * @since 6
384      */
385     void Stop();
386 
387     // gap
388     /**
389      * @brief Get local device class.
390      *
391      * @return Returns local device class.
392      * @since 6
393      */
394     BluetoothDeviceClass GetLocalDeviceClass() const;
395 
396     /**
397      * @brief Set local device class.
398      *
399      * @param deviceClass Device class.
400      * @return Returns <b>true</b> if the operation is successful;
401      *         returns <b>false</b> if the operation fails.
402      * @since 6
403      */
404     bool SetLocalDeviceClass(const BluetoothDeviceClass &deviceClass);
405 
406     /**
407      * @brief Get local device address.
408      *
409      * @return Returns local device address.
410      * @since 6
411      */
412     std::string GetLocalAddress() const;
413 
414     /**
415      * @brief Get local device name.
416      *
417      * @return Returns local device name.
418      * @since 6
419      */
420     std::string GetLocalName() const;
421 
422     /**
423      * @brief Set local device name.
424      *
425      * @param name Device name.
426      * @return Returns <b>true</b> if the operation is successful;
427      *         returns <b>false</b> if the operation fails.
428      * @since 6
429      */
430     bool SetLocalName(const std::string &name);
431 
432     /**
433      * @brief Get device scan mode.
434      *
435      * @return Returns bluetooth scan mode.
436      * @since 6
437      */
438     int GetBtScanMode() const;
439 
440     /**
441      * @brief Set device scan mode.
442      *
443      * @param mode Scan mode.
444      * @param duration Scan time.
445      * @return Returns <b>true</b> if the operation is successful;
446      *         returns <b>false</b> if the operation fails.
447      * @since 6
448      */
449     bool SetBtScanMode(int mode, int duration);
450 
451     /**
452      * @brief Get local device bondable mode.
453      *
454      * @param transport Adapter transport.
455      * @return Returns local device bondable mode.
456      * @since 6
457      */
458     int GetBondableMode(int transport) const;
459 
460     /**
461      * @brief Set local device bondable mode.
462      *
463      * @param transport Adapter transport.
464      * @param mode Device bondable mode.
465      * @return Returns <b>true</b> if the operation is successful;
466      *         returns <b>false</b> if the operation fails.
467      * @since 6
468      */
469     bool SetBondableMode(int transport, int mode);
470 
471     /**
472      * @brief Get device address.
473      * @return Returns <b>true</b> if the operation is successful;
474      *         returns <b>false</b> if the operation fails.
475      * @since 6
476      */
477     bool StartBtDiscovery();
478 
479     /**
480      * @brief Cancel device discovery.
481      *
482      * @return Returns <b>true</b> if the operation is successful;
483      *         returns <b>false</b> if the operation fails.
484      * @since 6
485      */
486     bool CancelBtDiscovery();
487 
488     /**
489      * @brief Check if device is discovering.
490      *
491      * @return Returns <b>true</b> if device is discovering;
492      *         returns <b>false</b> if device is not discovering.
493      * @since 6
494      */
495     bool IsBtDiscovering(int transport = BT_TRANSPORT_BREDR) const;
496 
497     /**
498      * @brief Get device discovery end time.
499      *
500      * @return Returns device discovery end time.
501      * @since 6
502      */
503     long GetBtDiscoveryEndMillis() const;
504 
505     /**
506      * @brief Get paired devices.
507      *
508      * @param transport Adapter transport.
509      * @return Returns paired devices vector.
510      * @since 6
511      */
512     std::vector<BluetoothRemoteDevice> GetPairedDevices(int transport) const;
513 
514     /**
515      * @brief Remove pair.
516      *
517      * @param device Remote device address.
518      * @return Returns <b>true</b> if the operation is successful;
519      *         returns <b>false</b> if the operation fails.
520      * @since 6
521      */
522     bool RemovePair(const BluetoothRemoteDevice &device);
523 
524     /**
525      * @brief Remove all pairs.
526      *
527      * @return Returns <b>true</b> if the operation is successful;
528      *         returns <b>false</b> if the operation fails.
529      * @since 6
530      */
531     bool RemoveAllPairs();
532 
533     /**
534      * @brief Check if bluetooth address is valid.
535      *
536      * @param addr Bluetooth address.
537      * @return Returns <b>true</b> if bluetooth address is valid;
538      *         returns <b>false</b> if bluetooth address is not valid.
539      * @since 6
540      */
541     static bool IsValidBluetoothAddr(const std::string &addr);
542 
543     /**
544      * @brief Register remote device observer.
545      *
546      * @param observer Class BluetoothRemoteDeviceObserver pointer to register observer.
547      * @return Returns <b>true</b> if the operation is successful;
548      *         returns <b>false</b> if the operation fails.
549      * @since 6
550      */
551     void RegisterRemoteDeviceObserver(BluetoothRemoteDeviceObserver &observer);
552 
553     /**
554      * @brief Deregister remote device observer.
555      *
556      * @param observer Class BluetoothRemoteDeviceObserver pointer to deregister observer.
557      * @return Returns <b>true</b> if the operation is successful;
558      *         returns <b>false</b> if the operation fails.
559      * @since 6
560      */
561     void DeregisterRemoteDeviceObserver(BluetoothRemoteDeviceObserver &observer);
562 
563     /**
564      * @brief Get max advertising data length.
565      *
566      * @return Returns max advertising data length.
567      * @since 6
568      */
569     int GetBleMaxAdvertisingDataLength() const;
570 
571 private:
572     static BluetoothHost hostAdapter_;
573 
574     /**
575      * @brief A constructor used to create a <b>BluetoothHost</b> instance.
576      *
577      * @since 6
578      */
579     BluetoothHost();
580 
581     /**
582      * @brief A destructor used to delete the <b>BluetoothHost</b> instance.
583      *
584      * @since 6
585      */
586     ~BluetoothHost();
587 
588     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothHost);
589     BLUETOOTH_DECLARE_IMPL();
590 };
591 } // namespace Bluetooth
592 } // namespace OHOS
593 
594 #endif  // BLUETOOTH_HOST_H
595