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 16 #include "bluetooth_ble_common.h" 17 #include "bluetooth_ble_impl.h" 18 19 namespace OHOS { 20 namespace CJSystemapi { 21 namespace CJBluetoothBle { 22 FfiBluetoothBleCentralManagerCallback()23FfiBluetoothBleCentralManagerCallback::FfiBluetoothBleCentralManagerCallback() 24 { 25 } 26 GetInstance(void)27FfiBluetoothBleCentralManagerCallback &FfiBluetoothBleCentralManagerCallback::GetInstance(void) 28 { 29 static FfiBluetoothBleCentralManagerCallback instance; 30 return instance; 31 } 32 RegisterBLEDeviceFindFunc(std::function<void (CArrScanResult)> cjCallback)33void FfiBluetoothBleCentralManagerCallback::RegisterBLEDeviceFindFunc(std::function<void(CArrScanResult)> cjCallback) 34 { 35 bleDeviceFindFunc = cjCallback; 36 } 37 OnScanCallback(const BleScanResult & result)38void FfiBluetoothBleCentralManagerCallback::OnScanCallback(const BleScanResult &result) 39 { 40 if (bleDeviceFindFunc == nullptr) { 41 return; 42 } 43 CArrScanResult outResults{}; 44 outResults.size = 1; 45 NativeScanResult *resultValue = static_cast<NativeScanResult *>(malloc(sizeof(NativeScanResult) * outResults.size)); 46 if (resultValue == nullptr) { 47 return; 48 } 49 for (int i = 0; i < outResults.size; i++) { 50 BleScanResult bleScanResult = result; 51 NativeScanResult nativeResult{}; 52 nativeResult.deviceId = MallocCString(bleScanResult.GetPeripheralDevice().GetDeviceAddr()); 53 nativeResult.rssi = bleScanResult.GetRssi(); 54 nativeResult.data = Convert2CArrUI8(bleScanResult.GetPayload()); 55 nativeResult.deviceName = MallocCString(bleScanResult.GetName()); 56 nativeResult.connectable = bleScanResult.IsConnectable(); 57 resultValue[i] = nativeResult; 58 } 59 outResults.head = resultValue; 60 bleDeviceFindFunc(outResults); 61 62 for (int i = 0; i < outResults.size; i++) { 63 NativeScanResult nativeResult = outResults.head[i]; 64 free(nativeResult.deviceId); 65 free(nativeResult.data.head); 66 free(nativeResult.deviceName); 67 nativeResult.deviceId = nullptr; 68 nativeResult.data.head = nullptr; 69 nativeResult.deviceName = nullptr; 70 } 71 free(resultValue); 72 resultValue = nullptr; 73 return; 74 } 75 76 } // namespace CJBluetoothBle 77 } // namespace CJSystemapi 78 } // namespace OHOS