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 17import hilog from '../model/Logger'; 18import router from '@ohos.router' 19import wifi from '@ohos.wifiManager'; 20import promptAction from '@ohos.promptAction' 21 22const TAG = 'p2pSetting' 23 24@Entry 25@Component 26struct p2pSetting { 27 @State deviceAddress: string = ''; 28 @State netId: number = - 2; 29 @State passphrase: string = ''; 30 @State groupName: string = '' 31 @State goBand: number = 0; 32 33 aboutToAppear() { 34 AppStorage.setOrCreate('deviceAddress' , this.deviceAddress) 35 AppStorage.setOrCreate('netId' , this.netId) 36 AppStorage.setOrCreate('passphrase' , this.passphrase) 37 AppStorage.setOrCreate('groupName' , this.groupName) 38 AppStorage.setOrCreate('goBand' , this.goBand) 39 } 40 41 42 async createGroup() { 43 try { 44 let deviceInfo = await wifi.getP2pLocalDevice() 45 let config:wifi.WifiP2PConfig = { 46 deviceAddress: deviceInfo.deviceAddress, 47 netId: this.netId, 48 passphrase: this.passphrase, 49 groupName: this.groupName, 50 goBand: this.goBand, 51 } 52 hilog.info(`deviceAddress: ${config.deviceAddress}, netId: ${config.netId}, pwd: ${config.passphrase}, gpname: ${config.groupName}, goBand: ${config.goBand}`) 53 wifi.createGroup(config) 54 promptAction.showToast({ message : 'createGroup success' }) 55 } catch (e) { 56 hilog.info(TAG, `createGroup Error: ${JSON.stringify(e)}`) 57 } 58 } 59 60 build() { 61 Column() { 62 Row() { 63 Text('deviceAddress:').fontSize(16).width(120) 64 TextInput({ text : this.deviceAddress , placeholder : 'input peripheral deviceId.' }) 65 .fontSize('15vp') 66 .onChange((strInput: string) => { 67 this.deviceAddress = strInput; 68 //判断合法性 69 if ( strInput.length >= 1 ) { 70 AppStorage.setOrCreate('deviceAddress' , this.deviceAddress); 71 } 72 }) 73 .width('80%') 74 .borderRadius(1) 75 } 76 .backgroundColor($r('app.color.moon')) 77 .padding(5) 78 .justifyContent(FlexAlign.Start) 79 .alignItems(VerticalAlign.Center) 80 81 Column() { 82 Stack().height('0.25vp').backgroundColor('#000000'); 83 Column() { 84 Row() { 85 Text('netId:').fontSize(15).width(60); 86 TextInput({ text : this.netId.toString() , placeholder : '-2' }) 87 .fontSize('15vp') 88 .onChange((strInput: string) => { 89 this.netId = parseInt(strInput); 90 //判断合法性 91 if ( strInput.length >= 1 ) { 92 AppStorage.setOrCreate('netId' , this.netId); 93 } 94 }) 95 .width('80%') 96 .borderRadius(1) 97 } 98 .padding(5) 99 .justifyContent(FlexAlign.Start) 100 .alignItems(VerticalAlign.Center) 101 .backgroundColor($r('app.color.spring')) 102 103 Row() { 104 Text('passphrase:').fontSize(15).width(100); 105 TextInput({ text : this.passphrase , placeholder : 'input passphrase' }) 106 .fontSize('15vp') 107 .onChange((strInput: string) => { 108 this.passphrase = strInput; 109 if ( strInput.length >= 1 ) { 110 AppStorage.setOrCreate('passphrase' , this.passphrase); 111 } 112 }) 113 .width('80%') 114 .borderRadius(1) 115 } 116 .backgroundColor($r('app.color.spring')) 117 .padding(5) 118 .justifyContent(FlexAlign.Start) 119 .alignItems(VerticalAlign.Center) 120 121 Row() { 122 Text('groupName:').fontSize(15).width(100); 123 TextInput({ text : this.groupName , placeholder : 'testGroup' }) 124 .fontSize('15vp') 125 .onChange((strInput: string) => { 126 this.groupName = strInput; 127 if ( strInput.length >= 1 ) { 128 AppStorage.setOrCreate('groupName' , this.groupName); 129 } 130 }) 131 .width('80%') 132 .borderRadius(1) 133 } 134 .backgroundColor($r('app.color.spring')) 135 .padding(5) 136 .justifyContent(FlexAlign.Start) 137 .alignItems(VerticalAlign.Center) 138 139 Row() { 140 Text('goBand:').fontSize(15).width(80); 141 TextInput({ text : this.goBand.toString() , placeholder : '0' }) 142 .fontSize('15vp') 143 .onChange((strInput: string) => { 144 this.goBand = Number(strInput); 145 if ( strInput.length >= 1 ) { 146 AppStorage.setOrCreate('goBand' , this.goBand); 147 } 148 }) 149 .width('80%') 150 .borderRadius(1) 151 } 152 .backgroundColor($r('app.color.spring')) 153 .padding(5) 154 .justifyContent(FlexAlign.Start) 155 .alignItems(VerticalAlign.Center) 156 157 } 158 159 Stack().height('0.25vp').backgroundColor('#000000'); 160 } 161 162 Row() { 163 Button($r('app.string.create_group'))// .dialogButtonStyle() 164 .fontSize('16fp') 165 .onClick(() => { 166 if(this.passphrase === '' || this.groupName === '') { 167 hilog.info(TAG, 'ssid || preSharedKey is null'); 168 promptAction.showToast({ message : 'passphrase or groupName is null' }) 169 return; 170 } 171 this.createGroup(); 172 }) 173 .height('100%') 174 } 175 .width('50%') 176 .height('6%') 177 .justifyContent(FlexAlign.SpaceBetween) 178 } 179 } 180}