• 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 #include "refbase.h"
43 
44 namespace OHOS { class IRemoteObject; }
45 namespace OHOS {
46 namespace Bluetooth {
47 /**
48  * @brief Represents framework host device basic observer.
49  *
50  * @since 6
51  */
52 class BluetoothHostObserver {
53 public:
54     /**
55      * @brief A destructor used to delete the <b>BluetoothHostObserver</b> instance.
56      *
57      * @since 6
58      */
59     virtual ~BluetoothHostObserver() = default;
60 
61     // common
62     /**
63      * @brief Adapter state change function.
64      *
65      * @param transport Transport type when state change.
66      *        BTTransport::ADAPTER_BREDR : classic;
67      *        BTTransport::ADAPTER_BLE : ble.
68      * @param state Change to the new state.
69      *        BTStateID::STATE_TURNING_ON;
70      *        BTStateID::STATE_TURN_ON;
71      *        BTStateID::STATE_TURNING_OFF;
72      *        BTStateID::STATE_TURN_OFF.
73      * @since 6
74      */
75     virtual void OnStateChanged(const int transport, const int status) = 0;
76 
77     // gap
78     /**
79      * @brief Discovery state changed observer.
80      *
81      * @param status Device discovery status.
82      * @since 6
83      */
84     virtual void OnDiscoveryStateChanged(int status) = 0;
85 
86     /**
87      * @brief Discovery result observer.
88      *
89      * @param device Remote device.
90      * @since 6
91      */
92     virtual void OnDiscoveryResult(const BluetoothRemoteDevice &device) = 0;
93 
94     /**
95      * @brief Pair request observer.
96      *
97      * @param device Remote device.
98      * @since 6
99      */
100     virtual void OnPairRequested(const BluetoothRemoteDevice &device) = 0;
101 
102     /**
103      * @brief Pair confirmed observer.
104      *
105      * @param device Remote device.
106      * @param reqType Pair type.
107      * @param number Paired passkey.
108      * @since 6
109      */
110     virtual void OnPairConfirmed(const BluetoothRemoteDevice &device, int reqType, int number) = 0;
111 
112     /**
113      * @brief Scan mode changed observer.
114      *
115      * @param mode Device scan mode.
116      * @since 6
117      */
118     virtual void OnScanModeChanged(int mode) = 0;
119 
120     /**
121      * @brief Device name changed observer.
122      *
123      * @param deviceName Device name.
124      * @since 6
125      */
126     virtual void OnDeviceNameChanged(const std::string &deviceName) = 0;
127 
128     /**
129      * @brief Device address changed observer.
130      *
131      * @param address Device address.
132      * @since 6
133      */
134     virtual void OnDeviceAddrChanged(const std::string &address) = 0;
135 };
136 
137 /**
138  * @brief Represents remote device observer.
139  *
140  * @since 6
141  */
142 class BluetoothRemoteDeviceObserver {
143 public:
144     /**
145      * @brief A destructor used to delete the <b>BluetoothRemoteDeviceObserver</b> instance.
146      *
147      * @since 6
148      */
149     virtual ~BluetoothRemoteDeviceObserver() = default;
150 
151     /**
152      * @brief Acl state changed observer.
153      *
154      * @param device Remote device.
155      * @param state Remote device acl state.
156      * @param reason Remote device reason.
157      * @since 6
158      */
159     virtual void OnAclStateChanged(const BluetoothRemoteDevice &device, int state, unsigned int reason) = 0;
160 
161     /**
162      * @brief Pair status changed observer.
163      *
164      * @param device Remote device.
165      * @param status Remote device pair status.
166      * @since 6
167      */
168     virtual void OnPairStatusChanged(const BluetoothRemoteDevice &device, int status) = 0;
169 
170     /**
171      * @brief Remote uuid changed observer.
172      *
173      * @param device Remote device.
174      * @param uuids Remote device uuids.
175      * @since 6
176      */
177     virtual void OnRemoteUuidChanged(const BluetoothRemoteDevice &device, const std::vector<ParcelUuid> &uuids) = 0;
178 
179     /**
180      * @brief Remote name changed observer.
181      *
182      * @param device Remote device.
183      * @param deviceName Remote device name.
184      * @since 6
185      */
186     virtual void OnRemoteNameChanged(const BluetoothRemoteDevice &device, const std::string &deviceName) = 0;
187 
188     /**
189      * @brief Remote alias changed observer.
190      *
191      * @param device Remote device.
192      * @param alias Remote device alias.
193      * @since 6
194      */
195     virtual void OnRemoteAliasChanged(const BluetoothRemoteDevice &device, const std::string &alias) = 0;
196 
197     /**
198      * @brief Remote cod changed observer.
199      *
200      * @param device Remote device.
201      * @param cod Remote device cod.
202      * @since 6
203      */
204     virtual void OnRemoteCodChanged(const BluetoothRemoteDevice &device, const BluetoothDeviceClass &cod) = 0;
205 
206     /**
207      * @brief Remote battery level changed observer.
208      *
209      * @param device Remote device.
210      * @param cod Remote device battery Level.
211      * @since 6
212      */
213     virtual void OnRemoteBatteryLevelChanged(const BluetoothRemoteDevice &device, int batteryLevel) = 0;
214 
215     /**
216      * @brief Remote rssi event observer.
217      *
218      * @param device Remote device.
219      * @param rssi Remote device rssi.
220      * @param status Read status.
221      * @since 6
222      */
223     virtual void OnReadRemoteRssiEvent(const BluetoothRemoteDevice &device, int rssi, int status) = 0;
224 };
225 
226 /**
227  * @brief Represents framework host device.
228  *
229  * @since 6
230  */
231 class BLUETOOTH_API BluetoothHost {
232 public:
233     // common
234     /**
235      * @brief Get default host device.
236      *
237      * @return Returns the singleton instance.
238      * @since 6
239      */
240     static BluetoothHost &GetDefaultHost();
241 
242     /**
243      * @brief Get remote device instance.
244      *
245      * @param addr Remote device address.
246      * @param transport Adapter transport.
247      * @return Returns remote device instance.
248      * @since 6
249      */
250     BluetoothRemoteDevice GetRemoteDevice(const std::string &addr, int transport) const;
251 
252     /**
253      * @brief Register observer.
254      *
255      * @param observer Class BluetoothHostObserver pointer to register observer.
256      * @since 6
257      */
258     void RegisterObserver(std::shared_ptr<BluetoothHostObserver> observer);
259 
260     /**
261      * @brief Deregister observer.
262      *
263      * @param observer Class BluetoothHostObserver pointer to deregister observer.
264      * @since 6
265      */
266     void DeregisterObserver(std::shared_ptr<BluetoothHostObserver> observer);
267 
268     /**
269      * @brief Enable classic.
270      *
271      * @return Returns <b>true</b> if the operation is accepted;
272      *         returns <b>false</b> if the operation is rejected.
273      * @since 6
274      */
275     int EnableBt();
276 
277     /**
278      * @brief Disable classic.
279      *
280      * @return Returns <b>true</b> if the operation is accepted;
281      *         returns <b>false</b> if the operation is rejected.
282      * @since 6
283      */
284     int DisableBt();
285 
286     /**
287      * @brief Get classic enable/disable state.
288      *
289      * @return 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() const;
297 
298     /**
299      * @brief Get classic enable/disable state.
300      *
301      * @param Returns classic enable/disable state.
302      *         BTStateID::STATE_TURNING_ON;
303      *         BTStateID::STATE_TURN_ON;
304      *         BTStateID::STATE_TURNING_OFF;
305      *         BTStateID::STATE_TURN_OFF.
306      * @since 6
307      */
308     int GetBtState(int &state) const;
309 
310     /**
311      * @brief Disable ble.
312      *
313      * @return Returns <b>true</b> if the operation is accepted;
314      *         returns <b>false</b> if the operation is rejected.
315      * @since 6
316      */
317     int DisableBle();
318 
319     /**
320      * @brief Enable ble.
321      *
322      * @return Returns <b>true</b> if the operation is accepted;
323      *         returns <b>false</b> if the operation is rejected.
324      * @since 6
325      */
326     int EnableBle();
327 
328     /**
329      * @brief Get br/edr enable/disable state.
330      *
331      * @return Returns <b>true</b> if br is enabled;
332      *         returns <b>false</b> if br is not enabled.
333      * @since 6
334      */
335     bool IsBrEnabled() const;
336 
337     /**
338      * @brief Get ble enable/disable state.
339      *
340      * @return Returns <b>true</b> if ble is enabled;
341      *         returns <b>false</b> if ble is not enabled.
342      * @since 6
343      */
344     bool IsBleEnabled() const;
345 
346     /**
347      * @brief Factory reset bluetooth service.
348      *
349      * @return Returns <b>true</b> if the operation is successful;
350      *         returns <b>false</b> if the operation fails.
351      * @since 6
352      */
353     int BluetoothFactoryReset();
354 
355     /**
356      * @brief Get profile service ID list.
357      *
358      * @return Returns vector of enabled profile services ID.
359      * @since 6
360      */
361     std::vector<uint32_t> GetProfileList() const;
362 
363     /**
364      * @brief Get max audio connected devices number.
365      *
366      * @return Returns max device number that audio can connect.
367      * @since 6
368      */
369     int GetMaxNumConnectedAudioDevices() 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() const;
382 
383     /**
384      * @brief Get bluetooth connects state.
385      *
386      * @return Returns bluetooth connects state.
387      *         BTConnectState::CONNECTING;
388      *         BTConnectState::CONNECTED;
389      *         BTConnectState::DISCONNECTING;
390      *         BTConnectState::DISCONNECTED.
391      * @since 6
392      */
393     int GetBtConnectionState(int &state) const;
394 
395     /**
396      * @brief Get profile service connect state.
397      *
398      * @param profileID Profile service ID.
399      * @return Returns connect state for designated profile service.
400      *         BTConnectState::CONNECTING;
401      *         BTConnectState::CONNECTED;
402      *         BTConnectState::DISCONNECTING;
403      *         BTConnectState::DISCONNECTED.
404      * @since 6
405      */
406     int GetBtProfileConnState(uint32_t profileId, int &state) const;
407 
408     /**
409      * @brief Get local device supported uuids.
410      *
411      * @param[out] Vector which use to return support uuids.
412      * @since 6
413      */
414     void GetLocalSupportedUuids(std::vector<ParcelUuid> &uuids);
415 
416     /**
417      * @brief Start adapter manager, passthrough only.
418      *
419      * @return Returns <b>true</b> if the operation is successful;
420      *         returns <b>false</b> if the operation fails.
421      * @since 6
422      */
423     bool Start();
424 
425     /**
426      * @brief Stop adapter manager, passthrough only.
427      *
428      * @since 6
429      */
430     void Stop();
431 
432     // gap
433     /**
434      * @brief Get local device class.
435      *
436      * @return Returns local device class.
437      * @since 6
438      */
439     BluetoothDeviceClass GetLocalDeviceClass() const;
440 
441     /**
442      * @brief Set local device class.
443      *
444      * @param deviceClass Device class.
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 SetLocalDeviceClass(const BluetoothDeviceClass &deviceClass);
450 
451     /**
452      * @brief Get local device address.
453      *
454      * @param addr local address.
455      * @return Returns {@link BT_NO_ERROR} if the operation is successful;
456      *         returns an error code defined in {@link BtErrCode} otherwise.
457      * @since 6
458      */
459     int GetLocalAddress(std::string &addr) const;
460 
461     /**
462      * @brief Get local device name.
463      *
464      * @return Returns local device name.
465      * @since 6
466      */
467     std::string GetLocalName() const;
468 
469     /**
470      * @brief Get local device name.
471      *
472      * @return Returns local device name.
473      * @since 6
474      */
475     int GetLocalName(std::string &name) const;
476 
477     /**
478      * @brief Set local device name.
479      *
480      * @param name Device name.
481      * @return Returns <b>true</b> if the operation is successful;
482      *         returns <b>false</b> if the operation fails.
483      * @since 6
484      */
485     int SetLocalName(const std::string &name);
486 
487     /**
488      * @brief Get device scan mode.
489      *
490      * @return Returns bluetooth scan mode.
491      * @since 6
492      */
493     int GetBtScanMode(int32_t &scanMode) const;
494 
495     /**
496      * @brief Set device scan mode.
497      *
498      * @param mode Scan mode.
499      * @param duration Scan time.
500      * @return Returns <b>true</b> if the operation is successful;
501      *         returns <b>false</b> if the operation fails.
502      * @since 6
503      */
504     int SetBtScanMode(int mode, int duration);
505 
506     /**
507      * @brief Get local device bondable mode.
508      *
509      * @param transport Adapter transport.
510      * @return Returns local device bondable mode.
511      * @since 6
512      */
513     int GetBondableMode(int transport) const;
514 
515     /**
516      * @brief Set local device bondable mode.
517      *
518      * @param transport Adapter transport.
519      * @param mode Device bondable mode.
520      * @return Returns <b>true</b> if the operation is successful;
521      *         returns <b>false</b> if the operation fails.
522      * @since 6
523      */
524     bool SetBondableMode(int transport, int mode);
525 
526     /**
527      * @brief Get device address.
528      * @return Returns <b>true</b> if the operation is successful;
529      *         returns <b>false</b> if the operation fails.
530      * @since 6
531      */
532     int StartBtDiscovery();
533 
534     /**
535      * @brief Cancel device discovery.
536      *
537      * @return Returns <b>true</b> if the operation is successful;
538      *         returns <b>false</b> if the operation fails.
539      * @since 6
540      */
541     int CancelBtDiscovery();
542 
543     /**
544      * @brief Check if device is discovering.
545      *
546      * @return Returns <b>BT_NO_ERROR</b> if the operation is successful;
547      *         returns <b>false</b> if device is not discovering.
548      * @since 6
549      */
550     int IsBtDiscovering(bool &isDisCovering, int transport = BT_TRANSPORT_BREDR) const;
551 
552     /**
553      * @brief Get device discovery end time.
554      *
555      * @return Returns device discovery end time.
556      * @since 6
557      */
558     long GetBtDiscoveryEndMillis() const;
559 
560     /**
561      * @brief Get paired devices.
562      *
563      * @param transport Adapter transport.
564      * @return Returns paired devices vector.
565      * @since 6
566      */
567     int32_t GetPairedDevices(int transport, std::vector<BluetoothRemoteDevice> &pairedDevices) const;
568 
569     /**
570      * @brief Remove pair.
571      *
572      * @param device Remote device address.
573      * @return Returns <b>true</b> if the operation is successful;
574      *         returns <b>false</b> if the operation fails.
575      * @since 6
576      */
577     int32_t RemovePair(const BluetoothRemoteDevice &device);
578 
579     /**
580      * @brief Remove all pairs.
581      *
582      * @return Returns <b>true</b> if the operation is successful;
583      *         returns <b>false</b> if the operation fails.
584      * @since 6
585      */
586     bool RemoveAllPairs();
587 
588     /**
589      * @brief Check if bluetooth address is valid.
590      *
591      * @param addr Bluetooth address.
592      * @return Returns <b>true</b> if bluetooth address is valid;
593      *         returns <b>false</b> if bluetooth address is not valid.
594      * @since 6
595      */
596     static bool IsValidBluetoothAddr(const std::string &addr);
597 
598     /**
599      * @brief Register remote device observer.
600      *
601      * @param observer Class BluetoothRemoteDeviceObserver pointer to register observer.
602      * @return Returns <b>true</b> if the operation is successful;
603      *         returns <b>false</b> if the operation fails.
604      * @since 6
605      */
606     void RegisterRemoteDeviceObserver(std::shared_ptr<BluetoothRemoteDeviceObserver> observer);
607 
608     /**
609      * @brief Deregister remote device observer.
610      *
611      * @param observer Class BluetoothRemoteDeviceObserver pointer to deregister observer.
612      * @return Returns <b>true</b> if the operation is successful;
613      *         returns <b>false</b> if the operation fails.
614      * @since 6
615      */
616     void DeregisterRemoteDeviceObserver(std::shared_ptr<BluetoothRemoteDeviceObserver> observer);
617 
618     /**
619      * @brief Get max advertising data length.
620      *
621      * @return Returns max advertising data length.
622      * @since 6
623      */
624     int GetBleMaxAdvertisingDataLength() const;
625 
626     void LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject);
627 
628     void LoadSystemAbilityFail();
629 
630     void OnRemoveBluetoothSystemAbility();
631 
632     /**
633      * @brief Get local profile uuids.
634      *
635      * @return Returns local profile uuids.
636      * @since 10
637      */
638     int32_t GetLocalProfileUuids(std::vector<std::string> &uuids);
639 
640     /**
641     * @brief Set fast scan enable or disable.
642     * @param isEnable set fast scan status flag.
643     * @return Returns <b>true</b> if the operation is successful;
644     *         returns <b>false</b> if the operation fails.
645     */
646     int SetFastScan(bool isEnable);
647 
648     /**
649     * @brief Get the random address of a device.
650     * If the address carried in the bluetooth interface is not obtained from the bluetooth,
651     * the interface needs to be used for address translation.
652     * @param realAddr real address.
653     * @param[out] randomAddr random address.
654     * @return Returns {@link BT_NO_ERROR} if get random address success;
655     * returns an error code defined in {@link BtErrCode} otherwise.
656     */
657     int GetRandomAddress(const std::string &realAddr, std::string &randomAddr) const;
658 
659     /**
660     * @brief Connects all allowed bluetooth profiles between the local and remote device.
661     *
662     * @param remoteAddr remote device addr.
663     * @return Returns {@link BT_NO_ERROR} if the operation is successful;
664     *         returns an error code defined in {@link BtErrCode} otherwise.
665     * @since 11
666     */
667     int ConnectAllowedProfiles(const std::string &remoteAddr) const;
668 
669     /**
670     * @brief Disconnects all allowed bluetooth profiles between the local and remote device.
671     *
672     * @param remoteAddr remote device addr.
673     * @return Returns {@link BT_NO_ERROR} if the operation is successful;
674     *         returns an error code defined in {@link BtErrCode} otherwise.
675     * @since 11
676     */
677     int DisconnectAllowedProfiles(const std::string &remoteAddr) const;
678 
679 private:
680     /**
681      * @brief A constructor used to create a <b>BluetoothHost</b> instance.
682      *
683      * @since 6
684      */
685     BluetoothHost();
686 
687     /**
688      * @brief A destructor used to delete the <b>BluetoothHost</b> instance.
689      *
690      * @since 6
691      */
692     ~BluetoothHost();
693 
694     int CountEnableTimes(bool enable);
695 
696     /**
697     * @brief Check whether bluetooth is prohibited by EDM.
698     *
699     * @return Returns <b>true</b> if bluetooth is prohibited, returns <b>false</b> otherwise.
700     * @since 11
701     */
702     bool IsBtProhibitedByEdm(void);
703 
704     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothHost);
705     BLUETOOTH_DECLARE_IMPL();
706 };
707 } // namespace Bluetooth
708 } // namespace OHOS
709 
710 #endif  // BLUETOOTH_HOST_H
711