• 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 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     int 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 Get classic enable/disable state.
288      *
289      * @param Returns classic enable/disable state.
290      *         BTStateID::STATE_TURNING_ON;
291      *         BTStateID::STATE_TURN_ON;
292      *         BTStateID::STATE_TURNING_OFF;
293      *         BTStateID::STATE_TURN_OFF.
294      * @since 6
295      */
296     int GetBtState(int &state) const;
297 
298     /**
299      * @brief Disable ble.
300      *
301      * @return Returns <b>true</b> if the operation is accepted;
302      *         returns <b>false</b> if the operation is rejected.
303      * @since 6
304      */
305     int DisableBle();
306 
307     /**
308      * @brief Enable ble.
309      *
310      * @return Returns <b>true</b> if the operation is accepted;
311      *         returns <b>false</b> if the operation is rejected.
312      * @since 6
313      */
314     int EnableBle();
315 
316     /**
317      * @brief Get br/edr enable/disable state.
318      *
319      * @return Returns <b>true</b> if br is enabled;
320      *         returns <b>false</b> if br is not enabled.
321      * @since 6
322      */
323     bool IsBrEnabled() const;
324 
325     /**
326      * @brief Get ble enable/disable state.
327      *
328      * @return Returns <b>true</b> if ble is enabled;
329      *         returns <b>false</b> if ble is not enabled.
330      * @since 6
331      */
332     bool IsBleEnabled() const;
333 
334     /**
335      * @brief Factory reset bluetooth service.
336      *
337      * @return Returns <b>true</b> if the operation is successful;
338      *         returns <b>false</b> if the operation fails.
339      * @since 6
340      */
341     bool BluetoothFactoryReset();
342 
343     /**
344      * @brief Get profile service ID list.
345      *
346      * @return Returns vector of enabled profile services ID.
347      * @since 6
348      */
349     std::vector<uint32_t> GetProfileList() const;
350 
351     /**
352      * @brief Get max audio connected devices number.
353      *
354      * @return Returns max device number that audio can connect.
355      * @since 6
356      */
357     int GetMaxNumConnectedAudioDevices() const;
358 
359     /**
360      * @brief Get bluetooth connects state.
361      *
362      * @return Returns bluetooth connects state.
363      *         BTConnectState::CONNECTING;
364      *         BTConnectState::CONNECTED;
365      *         BTConnectState::DISCONNECTING;
366      *         BTConnectState::DISCONNECTED.
367      * @since 6
368      */
369     int GetBtConnectionState() const;
370 
371     /**
372      * @brief Get bluetooth connects state.
373      *
374      * @return Returns bluetooth connects state.
375      *         BTConnectState::CONNECTING;
376      *         BTConnectState::CONNECTED;
377      *         BTConnectState::DISCONNECTING;
378      *         BTConnectState::DISCONNECTED.
379      * @since 6
380      */
381     int GetBtConnectionState(int &state) const;
382 
383     /**
384      * @brief Get profile service connect state.
385      *
386      * @param profileID Profile service ID.
387      * @return Returns connect state for designated profile service.
388      *         BTConnectState::CONNECTING;
389      *         BTConnectState::CONNECTED;
390      *         BTConnectState::DISCONNECTING;
391      *         BTConnectState::DISCONNECTED.
392      * @since 6
393      */
394     int GetBtProfileConnState(uint32_t profileId, int &state) const;
395 
396     /**
397      * @brief Get local device supported uuids.
398      *
399      * @param[out] Vector which use to return support uuids.
400      * @since 6
401      */
402     void GetLocalSupportedUuids(std::vector<ParcelUuid> &uuids);
403 
404     /**
405      * @brief Start adapter manager, passthrough only.
406      *
407      * @return Returns <b>true</b> if the operation is successful;
408      *         returns <b>false</b> if the operation fails.
409      * @since 6
410      */
411     bool Start();
412 
413     /**
414      * @brief Stop adapter manager, passthrough only.
415      *
416      * @since 6
417      */
418     void Stop();
419 
420     // gap
421     /**
422      * @brief Get local device class.
423      *
424      * @return Returns local device class.
425      * @since 6
426      */
427     BluetoothDeviceClass GetLocalDeviceClass() const;
428 
429     /**
430      * @brief Set local device class.
431      *
432      * @param deviceClass Device class.
433      * @return Returns <b>true</b> if the operation is successful;
434      *         returns <b>false</b> if the operation fails.
435      * @since 6
436      */
437     bool SetLocalDeviceClass(const BluetoothDeviceClass &deviceClass);
438 
439     /**
440      * @brief Get local device address.
441      *
442      * @return Returns local device address.
443      * @since 6
444      */
445     std::string GetLocalAddress() const;
446 
447     /**
448      * @brief Get local device name.
449      *
450      * @return Returns local device name.
451      * @since 6
452      */
453     std::string GetLocalName() const;
454 
455     /**
456      * @brief Get local device name.
457      *
458      * @return Returns local device name.
459      * @since 6
460      */
461     int GetLocalName(std::string &name) const;
462 
463     /**
464      * @brief Set local device name.
465      *
466      * @param name Device name.
467      * @return Returns <b>true</b> if the operation is successful;
468      *         returns <b>false</b> if the operation fails.
469      * @since 6
470      */
471     int SetLocalName(const std::string &name);
472 
473     /**
474      * @brief Get device scan mode.
475      *
476      * @return Returns bluetooth scan mode.
477      * @since 6
478      */
479     int GetBtScanMode(int32_t &scanMode) const;
480 
481     /**
482      * @brief Set device scan mode.
483      *
484      * @param mode Scan mode.
485      * @param duration Scan time.
486      * @return Returns <b>true</b> if the operation is successful;
487      *         returns <b>false</b> if the operation fails.
488      * @since 6
489      */
490     int SetBtScanMode(int mode, int duration);
491 
492     /**
493      * @brief Get local device bondable mode.
494      *
495      * @param transport Adapter transport.
496      * @return Returns local device bondable mode.
497      * @since 6
498      */
499     int GetBondableMode(int transport) const;
500 
501     /**
502      * @brief Set local device bondable mode.
503      *
504      * @param transport Adapter transport.
505      * @param mode Device bondable mode.
506      * @return Returns <b>true</b> if the operation is successful;
507      *         returns <b>false</b> if the operation fails.
508      * @since 6
509      */
510     bool SetBondableMode(int transport, int mode);
511 
512     /**
513      * @brief Get device address.
514      * @return Returns <b>true</b> if the operation is successful;
515      *         returns <b>false</b> if the operation fails.
516      * @since 6
517      */
518     int StartBtDiscovery();
519 
520     /**
521      * @brief Cancel device discovery.
522      *
523      * @return Returns <b>true</b> if the operation is successful;
524      *         returns <b>false</b> if the operation fails.
525      * @since 6
526      */
527     int CancelBtDiscovery();
528 
529     /**
530      * @brief Check if device is discovering.
531      *
532      * @return Returns <b>true</b> if device is discovering;
533      *         returns <b>false</b> if device is not discovering.
534      * @since 6
535      */
536     bool IsBtDiscovering(int transport = BT_TRANSPORT_BREDR) const;
537 
538     /**
539      * @brief Get device discovery end time.
540      *
541      * @return Returns device discovery end time.
542      * @since 6
543      */
544     long GetBtDiscoveryEndMillis() const;
545 
546     /**
547      * @brief Get paired devices.
548      *
549      * @param transport Adapter transport.
550      * @return Returns paired devices vector.
551      * @since 6
552      */
553     int32_t GetPairedDevices(int transport, std::vector<BluetoothRemoteDevice> &pairedDevices) const;
554 
555     /**
556      * @brief Remove pair.
557      *
558      * @param device Remote device address.
559      * @return Returns <b>true</b> if the operation is successful;
560      *         returns <b>false</b> if the operation fails.
561      * @since 6
562      */
563     int32_t RemovePair(const BluetoothRemoteDevice &device);
564 
565     /**
566      * @brief Remove all pairs.
567      *
568      * @return Returns <b>true</b> if the operation is successful;
569      *         returns <b>false</b> if the operation fails.
570      * @since 6
571      */
572     bool RemoveAllPairs();
573 
574     /**
575      * @brief Check if bluetooth address is valid.
576      *
577      * @param addr Bluetooth address.
578      * @return Returns <b>true</b> if bluetooth address is valid;
579      *         returns <b>false</b> if bluetooth address is not valid.
580      * @since 6
581      */
582     static bool IsValidBluetoothAddr(const std::string &addr);
583 
584     /**
585      * @brief Register remote device observer.
586      *
587      * @param observer Class BluetoothRemoteDeviceObserver pointer to register observer.
588      * @return Returns <b>true</b> if the operation is successful;
589      *         returns <b>false</b> if the operation fails.
590      * @since 6
591      */
592     void RegisterRemoteDeviceObserver(std::shared_ptr<BluetoothRemoteDeviceObserver> observer);
593 
594     /**
595      * @brief Deregister remote device observer.
596      *
597      * @param observer Class BluetoothRemoteDeviceObserver pointer to deregister observer.
598      * @return Returns <b>true</b> if the operation is successful;
599      *         returns <b>false</b> if the operation fails.
600      * @since 6
601      */
602     void DeregisterRemoteDeviceObserver(BluetoothRemoteDeviceObserver &observer);
603 
604     /**
605      * @brief Get max advertising data length.
606      *
607      * @return Returns max advertising data length.
608      * @since 6
609      */
610     int GetBleMaxAdvertisingDataLength() const;
611 
612 private:
613     static BluetoothHost hostAdapter_;
614 
615     /**
616      * @brief A constructor used to create a <b>BluetoothHost</b> instance.
617      *
618      * @since 6
619      */
620     BluetoothHost();
621 
622     /**
623      * @brief A destructor used to delete the <b>BluetoothHost</b> instance.
624      *
625      * @since 6
626      */
627     ~BluetoothHost();
628 
629     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothHost);
630     BLUETOOTH_DECLARE_IMPL();
631 };
632 } // namespace Bluetooth
633 } // namespace OHOS
634 
635 #endif  // BLUETOOTH_HOST_H
636