• 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 #include <set>
17 #include <vector>
18 
19 #include "bluetooth_def.h"
20 #include "bluetooth_gatt_client.h"
21 #include "bluetooth_gatt_client_proxy.h"
22 #include "bluetooth_gatt_manager.h"
23 #include "bluetooth_host_proxy.h"
24 #include "bluetooth_log.h"
25 #include "i_bluetooth_gatt_client.h"
26 #include "i_bluetooth_host.h"
27 #include "iservice_registry.h"
28 #include "system_ability_definition.h"
29 
30 namespace OHOS {
31 namespace Bluetooth {
32 struct GattManager::impl {
33 public:
implOHOS::Bluetooth::GattManager::impl34     impl()
35     {
36         sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
37         sptr<IRemoteObject> hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
38 
39         sptr<BluetoothHostProxy> hostProxy = new BluetoothHostProxy(hostRemote);
40         sptr<IRemoteObject> remote = hostProxy->GetProfile(PROFILE_GATT_CLIENT);
41 
42         clientProxy_ = new BluetoothGattClientProxy(remote);
43     }
~implOHOS::Bluetooth::GattManager::impl44     ~impl()
45     {}
46     sptr<BluetoothGattClientProxy> clientProxy_;
47 };
48 
GattManager()49 GattManager::GattManager() : pimpl(new GattManager::impl())
50 {}
51 
~GattManager()52 GattManager::~GattManager()
53 {}
54 
GetDevicesByStates(const std::array<int,GATT_CONNECTION_STATE_NUM> & states)55 std::vector<BluetoothRemoteDevice> GattManager::GetDevicesByStates(
56     const std::array<int, GATT_CONNECTION_STATE_NUM> &states)
57 {
58     std::set<int> stateSet;
59     for (auto &state : states) {
60         stateSet.emplace(state);
61     }
62     std::vector<BluetoothRemoteDevice> result;
63     if (pimpl->clientProxy_ != nullptr) {
64         std::vector<BluetoothGattDevice> devices;
65         pimpl->clientProxy_->GetAllDevice(devices);
66         for (auto &item : devices) {
67             if (stateSet.find(item.connectState_) != stateSet.end()) {
68                 result.push_back(BluetoothRemoteDevice(item.addr_.GetAddress(),
69                     item.transport_ == (GATT_TRANSPORT_TYPE_LE ? BT_TRANSPORT_BLE : BT_TRANSPORT_BREDR)));
70             }
71         }
72     }
73     return result;
74 }
75 
GetConnectedDevices()76 std::vector<BluetoothRemoteDevice> GattManager::GetConnectedDevices()
77 {
78     std::vector<BluetoothRemoteDevice> result;
79     if (pimpl->clientProxy_ != nullptr) {
80         std::vector<BluetoothGattDevice> device;
81         pimpl->clientProxy_->GetAllDevice(device);
82         for (auto &item : device) {
83             if (item.connectState_ == static_cast<int>(BTConnectState::CONNECTED)) {
84                 result.push_back(BluetoothRemoteDevice(item.addr_.GetAddress(),
85                     item.transport_ == (GATT_TRANSPORT_TYPE_LE ? BT_TRANSPORT_BLE : BT_TRANSPORT_BREDR)));
86             }
87         }
88     }
89     return result;
90 }
91 }  // namespace Bluetooth
92 }  // namespace OHOS
93