1 /* 2 * Copyright (C) 2025 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 BLUETOOTH_SCAN_RESULT_CALLBACK_NAPI_H 17 #define BLUETOOTH_SCAN_RESULT_CALLBACK_NAPI_H 18 19 #include "iremote_stub.h" 20 #include "napi/native_api.h" 21 #include "uv.h" 22 23 #include "constant_definition.h" 24 #include "location_log.h" 25 #include "ibluetooth_scan_result_callback.h" 26 #include "bluetooth_scan_result.h" 27 28 namespace OHOS { 29 namespace Location { 30 bool FindBlueToothCallback(napi_ref cb); 31 void DeleteBlueToothCallback(napi_ref cb); 32 class BluetoothScanResultCallbackNapi : public IRemoteStub<IBluetoothScanResultCallback> { 33 public: 34 BluetoothScanResultCallbackNapi(); 35 virtual ~BluetoothScanResultCallbackNapi(); 36 virtual int OnRemoteRequest(uint32_t code, 37 MessageParcel& data, MessageParcel& reply, MessageOption& option) override; 38 39 void DoSendWork(uv_loop_s *&loop, uv_work_t *&work); 40 void OnBluetoothScanResultChange(const std::unique_ptr<BluetoothScanResult>& bluetoothScanResult) override; 41 void DeleteHandler(); 42 bool FindCallback(); 43 napi_ref GetHandleCb(); 44 void SetHandleCb(const napi_ref& handlerCb); 45 napi_env GetEnv(); 46 void SetEnv(const napi_env& env); 47 48 template <typename T> InitContext(T * context)49 bool InitContext(T* context) 50 { 51 if (context == nullptr) { 52 LBSLOGE(LOCATOR_CALLBACK, "context == nullptr."); 53 return false; 54 } 55 context->env = env_; 56 context->callback[SUCCESS_CALLBACK] = handlerCb_; 57 return true; 58 } 59 private: 60 napi_env env_; 61 napi_ref handlerCb_; 62 std::mutex mutex_; 63 }; 64 } // namespace Location 65 } // namespace OHOS 66 #endif // BLUETOOTH_SCAN_RESULT_CALLBACK_NAPI_H 67