• 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(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(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     bool 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      * @return Returns local device address.
455      * @since 6
456      */
457     std::string GetLocalAddress() const;
458 
459     /**
460      * @brief Get local device name.
461      *
462      * @return Returns local device name.
463      * @since 6
464      */
465     std::string GetLocalName() const;
466 
467     /**
468      * @brief Get local device name.
469      *
470      * @return Returns local device name.
471      * @since 6
472      */
473     int GetLocalName(std::string &name) const;
474 
475     /**
476      * @brief Set local device name.
477      *
478      * @param name Device name.
479      * @return Returns <b>true</b> if the operation is successful;
480      *         returns <b>false</b> if the operation fails.
481      * @since 6
482      */
483     int SetLocalName(const std::string &name);
484 
485     /**
486      * @brief Get device scan mode.
487      *
488      * @return Returns bluetooth scan mode.
489      * @since 6
490      */
491     int GetBtScanMode(int32_t &scanMode) const;
492 
493     /**
494      * @brief Set device scan mode.
495      *
496      * @param mode Scan mode.
497      * @param duration Scan time.
498      * @return Returns <b>true</b> if the operation is successful;
499      *         returns <b>false</b> if the operation fails.
500      * @since 6
501      */
502     int SetBtScanMode(int mode, int duration);
503 
504     /**
505      * @brief Get local device bondable mode.
506      *
507      * @param transport Adapter transport.
508      * @return Returns local device bondable mode.
509      * @since 6
510      */
511     int GetBondableMode(int transport) const;
512 
513     /**
514      * @brief Set local device bondable mode.
515      *
516      * @param transport Adapter transport.
517      * @param mode Device bondable mode.
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 SetBondableMode(int transport, int mode);
523 
524     /**
525      * @brief Get device address.
526      * @return Returns <b>true</b> if the operation is successful;
527      *         returns <b>false</b> if the operation fails.
528      * @since 6
529      */
530     int StartBtDiscovery();
531 
532     /**
533      * @brief Cancel device discovery.
534      *
535      * @return Returns <b>true</b> if the operation is successful;
536      *         returns <b>false</b> if the operation fails.
537      * @since 6
538      */
539     int CancelBtDiscovery();
540 
541     /**
542      * @brief Check if device is discovering.
543      *
544      * @return Returns <b>true</b> if device is discovering;
545      *         returns <b>false</b> if device is not discovering.
546      * @since 6
547      */
548     bool IsBtDiscovering(int transport = BT_TRANSPORT_BREDR) const;
549 
550     /**
551      * @brief Get device discovery end time.
552      *
553      * @return Returns device discovery end time.
554      * @since 6
555      */
556     long GetBtDiscoveryEndMillis() const;
557 
558     /**
559      * @brief Get paired devices.
560      *
561      * @param transport Adapter transport.
562      * @return Returns paired devices vector.
563      * @since 6
564      */
565     int32_t GetPairedDevices(int transport, std::vector<BluetoothRemoteDevice> &pairedDevices) const;
566 
567     /**
568      * @brief Remove pair.
569      *
570      * @param device Remote device address.
571      * @return Returns <b>true</b> if the operation is successful;
572      *         returns <b>false</b> if the operation fails.
573      * @since 6
574      */
575     int32_t RemovePair(const BluetoothRemoteDevice &device);
576 
577     /**
578      * @brief Remove all pairs.
579      *
580      * @return Returns <b>true</b> if the operation is successful;
581      *         returns <b>false</b> if the operation fails.
582      * @since 6
583      */
584     bool RemoveAllPairs();
585 
586     /**
587      * @brief Check if bluetooth address is valid.
588      *
589      * @param addr Bluetooth address.
590      * @return Returns <b>true</b> if bluetooth address is valid;
591      *         returns <b>false</b> if bluetooth address is not valid.
592      * @since 6
593      */
594     static bool IsValidBluetoothAddr(const std::string &addr);
595 
596     /**
597      * @brief Register remote device observer.
598      *
599      * @param observer Class BluetoothRemoteDeviceObserver pointer to register observer.
600      * @return Returns <b>true</b> if the operation is successful;
601      *         returns <b>false</b> if the operation fails.
602      * @since 6
603      */
604     void RegisterRemoteDeviceObserver(std::shared_ptr<BluetoothRemoteDeviceObserver> observer);
605 
606     /**
607      * @brief Deregister remote device observer.
608      *
609      * @param observer Class BluetoothRemoteDeviceObserver pointer to deregister observer.
610      * @return Returns <b>true</b> if the operation is successful;
611      *         returns <b>false</b> if the operation fails.
612      * @since 6
613      */
614     void DeregisterRemoteDeviceObserver(BluetoothRemoteDeviceObserver &observer);
615 
616     /**
617      * @brief Get max advertising data length.
618      *
619      * @return Returns max advertising data length.
620      * @since 6
621      */
622     int GetBleMaxAdvertisingDataLength() const;
623 
624     void LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject);
625 
626     void LoadSystemAbilityFail();
627 
628     void Init();
629 
630     /**
631      * @brief Get remote devicec profile uuids.
632      *
633      * @param address Bluetooth address.
634      * @return Returns remote devicec uuids.
635      * @since 10
636      */
637     int32_t GetDeviceUuids(const std::string &address, std::vector<std::string> &uuids);
638 
639     /**
640      * @brief Get local profile uuids.
641      *
642      * @return Returns local profile uuids.
643      * @since 10
644      */
645     int32_t GetLocalProfileUuids(std::vector<std::string> &uuids);
646 
647     /**
648     * @brief Set fast scan enable or disable.
649     * @param isEnable set fast scan status flag.
650     * @return Returns <b>true</b> if the operation is successful;
651     *         returns <b>false</b> if the operation fails.
652     */
653     int SetFastScan(bool isEnable);
654 
655     /**
656     * @brief Get the random address of a device.
657     * If the address carried in the bluetooth interface is not obtained from the bluetooth,
658     * the interface needs to be used for address translation.
659     * @param realAddr real address.
660     * @param[out] randomAddr random address.
661     * @return Returns {@link BT_NO_ERROR} if get random address success;
662     * returns an error code defined in {@link BtErrCode} otherwise.
663     */
664     int GetRandomAddress(const std::string &realAddr, std::string &randomAddr) const;
665 
666 private:
667     /**
668      * @brief A constructor used to create a <b>BluetoothHost</b> instance.
669      *
670      * @since 6
671      */
672     BluetoothHost();
673 
674     /**
675      * @brief A destructor used to delete the <b>BluetoothHost</b> instance.
676      *
677      * @since 6
678      */
679     ~BluetoothHost();
680 
681     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothHost);
682     BLUETOOTH_DECLARE_IMPL();
683 };
684 } // namespace Bluetooth
685 } // namespace OHOS
686 
687 #endif  // BLUETOOTH_HOST_H
688