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 <algorithm>
17 #include <string>
18
19 #include "bluetooth_dialog.h"
20 #include "classic/classic_defs.h"
21 #include "dialog_pair.h"
22 #include "double_wrapper.h"
23 #include "int_wrapper.h"
24 #include "ipc_skeleton.h"
25 #include "json/json.h"
26 #include "json/writer.h"
27 #include "log.h"
28 #include "raw_address.h"
29 #include "refbase.h"
30 #include "string_wrapper.h"
31
32 namespace OHOS {
33 namespace bluetooth {
34 constexpr int32_t PINCODE_SIZE = 6;
RequestBluetoothPairDialog(const RawAddress & device,int reqType,int number)35 bool DialogPair::RequestBluetoothPairDialog(const RawAddress& device, int reqType, int number)
36 {
37 HILOGI("Pair dialog");
38 const std::string abilityName = "BluetoothPairDialog";
39 const std::string bundleName = "com.ohos.settings";
40 std::string connectStr = DialogPair::BuildStartCommand(device, reqType, number);
41 if (!BluetoothDialog::DialogConnectExtension(connectStr, bundleName, abilityName)) {
42 HILOGE("failed to connect dialog.");
43 return false;
44 }
45 return true;
46 }
47
BuildStartCommand(const RawAddress & device,int reqType,int number)48 std::string DialogPair::BuildStartCommand(const RawAddress& device, int reqType, int number)
49 {
50 Json::Value root;
51 Json::FastWriter writer;
52 std::string uiType = "sysDialog/common";
53 std::string pinNum = std::to_string(number);
54 while (pinNum.size() < PINCODE_SIZE) {
55 pinNum = "0" + pinNum;
56 }
57 root["ability.want.params.uiExtensionType"] = uiType;
58 root["deviceId"] = device.GetAddress();
59 root["pinCode"] = pinNum;
60 root["pinType"] = reqType;
61
62 HILOGI("DialogPair::BuildStartCommand address:, pinCode: %{public}d, pinType: : %{public}d ", number, reqType);
63 std::string cmdData = writer.write(root);
64 return cmdData;
65 }
66
67 } // namespace bluetooth
68 } // namespace OHOS