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 16import hilog from '../model/Logger' 17import { WifiView } from '../component/WifiView' 18import wifi from '@ohos.wifiManager' 19 20const TAG = 'AvailableWiFi' 21let self: AvailableWifi | null = null; 22@Entry 23@Component 24export struct AvailableWifi { 25 @State wifilist: Array<wifi.WifiScanInfo> = [] 26 @State wifilisttest: Array<wifi.WifiScanInfo> = [] 27 @State isSwitchOn: boolean = false; 28 private linkedInfo: wifi.WifiLinkedInfo | null = null; 29 @State isLinked: boolean = false; 30 31 // 扫描wifi 32 async getScanInfos() { 33 // 不停地扫描wifi 34 let result: wifi.WifiScanInfo[] = await wifi.getScanInfoList(); 35 36 if (this.isSwitchOn) { 37 setTimeout(async () => { 38 this.wifilist = result 39 await this.getScanInfos(); 40 }, 3000) 41 } 42 } 43 44 addListener() { 45 // 连接状态改变时,修改连接信息 46 wifi.on('wifiConnectionChange', async state => { 47 hilog.info(TAG, `wifiConnectionChange: ${state}`); 48 await this.getLinkedInfo(); 49 }) 50 // wifi状态改变时,先清空wifi列表,然后判断是否是开启状态,如果是就扫描 51 wifi.on('wifiStateChange', state => { 52 hilog.info(TAG, `wifiStateLisener state: ${state}`); 53 if (state === 1) { // 1: wifi is enable, 0:wifi is disable 54 this.isSwitchOn = true; 55 this.getScanInfos(); 56 } 57 if (state === 0) { // 1: wifi is enable, 0:wifi is disable 58 this.isSwitchOn = false; 59 } 60 }) 61 } 62 63 async getLinkedInfo() { 64 try { 65 let wifiLinkedInfo = await wifi.getLinkedInfo(); 66 if (wifiLinkedInfo === null || wifiLinkedInfo.bssid === '') { 67 this.isLinked = false; 68 this.linkedInfo = null; 69 return; 70 } 71 this.isLinked = true; 72 this.linkedInfo = wifiLinkedInfo; 73 } catch (err) { 74 hilog.info(`getLinkedInfo failed err is ${JSON.stringify(err)}`); 75 } 76 } 77 78 aboutToAppear() { 79 if (wifi.isWifiActive()) { 80 hilog.info(TAG, 'wifi is active'); 81 this.isSwitchOn = true; 82 this.getScanInfos(); 83 this.getLinkedInfo(); 84 } 85 hilog.info(TAG, 'wifi is disabled'); 86 // 启动监听 87 this.addListener(); 88 } 89 90 aboutToDisappear() { 91 wifi.off('wifiConnectionChange'); 92 wifi.off('wifiStateChange'); 93 } 94 95 build() { 96 Column() { 97 Row() { 98 Text($r('app.string.wlan')) 99 .fontSize(22) 100 .fontWeight(FontWeight.Bold) 101 .height(40) 102 Column() { 103 Toggle({ type: ToggleType.Switch, isOn: this.isSwitchOn }) 104 .id('switch') 105 .onChange((isOn: boolean) => { 106 hilog.info(`wifi swtich is: ${isOn}`); 107 this.wifilist = this.wifilisttest 108 }) 109 } 110 } 111 .width('100%') 112 .padding({ left: 16, right: 16 }) 113 114 List({ space: 5 }) { 115 ForEach(this.isSwitchOn ? this.wifilist : this.wifilisttest, (wifiItem: wifi.WifiScanInfo) => { 116 ListItem() { 117 WifiView({ wifi: wifiItem }) 118 } 119 }, (wifiItem: wifi.WifiScanInfo) => JSON.stringify(wifiItem)); 120 } 121 } 122 .width('100%') 123 .height('100%') 124 .backgroundColor($r('app.color.index_bg')) 125 } 126}