1 /* 2 * Copyright (C) 2023 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 #ifndef NAPI_BLUETOOTH_CONNECTION_OBSERVER_H 17 #define NAPI_BLUETOOTH_CONNECTION_OBSERVER_H 18 19 #include "bluetooth_host.h" 20 #include "napi_bluetooth_utils.h" 21 22 namespace OHOS { 23 namespace Bluetooth { 24 class NapiBluetoothConnectionObserver : public BluetoothHostObserver { 25 public: 26 NapiBluetoothConnectionObserver() = default; 27 ~NapiBluetoothConnectionObserver() override = default; 28 OnStateChanged(const int transport,const int status)29 void OnStateChanged(const int transport, const int status) override{}; 30 void OnDiscoveryStateChanged(int status) override; 31 void OnDiscoveryResult(const BluetoothRemoteDevice &device) override; 32 void OnPairRequested(const BluetoothRemoteDevice &device) override; 33 void OnPairConfirmed(const BluetoothRemoteDevice &device, int reqType, int number) override; 34 void OnScanModeChanged(int mode) override; 35 void OnDeviceNameChanged(const std::string &deviceName) override; 36 void OnDeviceAddrChanged(const std::string &address) override; 37 38 void RegisterCallback(const std::string &callbackName, const std::shared_ptr<BluetoothCallbackInfo> &callback); 39 void DeRegisterCallback(const std::string &callbackName); 40 std::shared_ptr<BluetoothCallbackInfo> GetCallback(const std::string &callbackName); 41 42 private: 43 void DealBredrPairComfirmed(const std::string &addr, const int reqType, const int number); 44 void DealBlePairComfirmed(const std::string &addr, const int reqType, const int number); 45 void OnPairConfirmedCallBack(const std::shared_ptr<PairConfirmedCallBackInfo> &pairConfirmInfo); 46 47 void UvQueueWorkOnDiscoveryResult(uv_work_t *work, std::shared_ptr<BluetoothRemoteDevice> &device); 48 void UvQueueWorkOnPairConfirmedCallBack( 49 uv_work_t *work, const std::shared_ptr<PairConfirmedCallBackInfo> &pairConfirmInfo); 50 51 private: 52 std::mutex callbacksMapLock_; 53 std::unordered_map<std::string, std::shared_ptr<BluetoothCallbackInfo>> callbacks_; 54 }; 55 } // namespace Bluetooth 56 } // namespace OHOS 57 #endif // NAPI_BLUETOOTH_HOST_OBSERVER_H 58