1 /* 2 * Copyright (c) 2024 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 #ifndef BLUETOOTH_ACCESS_IMPL_H 16 #define BLUETOOTH_ACCESS_IMPL_H 17 18 #include "bluetooth_host.h" 19 #include "napi_bluetooth_utils.h" 20 21 #include <cstdint> 22 23 namespace OHOS { 24 namespace CJSystemapi { 25 namespace CJBluetoothAccess { 26 using Bluetooth::BluetoothHostObserver; 27 using Bluetooth::BluetoothRemoteDevice; 28 using Bluetooth::BluetoothState; 29 30 class AccessImpl { 31 public: 32 AccessImpl() = default; 33 ~AccessImpl() = default; 34 35 static void EnableBluetooth(int32_t* errCode); 36 static void DisableBluetooth(int32_t* errCode); 37 static int32_t GetState(int32_t* errCode); 38 static void RegisterAccessObserver(int32_t callbackType, void (*callback)(), int32_t* errCode); 39 }; 40 41 class CjBluetoothAccessObserver : public BluetoothHostObserver { 42 public: 43 CjBluetoothAccessObserver(); 44 ~CjBluetoothAccessObserver() override = default; 45 46 void OnStateChanged(const int transport, const int status) override; 47 void OnDiscoveryStateChanged(int status) override{}; 48 void OnDiscoveryResult( 49 const BluetoothRemoteDevice &device, int rssi, const std::string deviceName, int deviceClass) override{}; 50 void OnPairRequested(const BluetoothRemoteDevice &device) override{}; 51 void OnPairConfirmed(const BluetoothRemoteDevice &device, int reqType, int number) override{}; 52 void OnScanModeChanged(int mode) override{}; 53 void OnDeviceNameChanged(const std::string &deviceName) override{}; 54 void OnDeviceAddrChanged(const std::string &address) override{}; 55 56 void RegisterStateChangeFunc(std::function<void(int32_t)> cjCallback); 57 58 private: 59 bool DealStateChange(const int transport, const int status, BluetoothState &state); 60 void GetBrStateByStatus(const int status, BluetoothState &state, bool &isCallback); 61 void GetBleStateByStatus(const int status, BluetoothState &state); 62 std::function<void(int32_t)> stateChangeFunc{nullptr}; 63 }; 64 } // namespace BluetoothAccess 65 } // namespace CJSystemapi 66 } // namespace OHOS 67 68 #endif // BLUETOOTH_ACCESS_IMPL_H