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