1/* 2 * Copyright (c) 2022-2023 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 prompt from '@ohos.promptAction' 17import wifi from '@ohos.wifiManager' 18import Logger from '../model/Logger' 19 20const TAG: string = 'WiFiModel' 21 22export type WifiType = { 23 ssid: string, 24 bssid: string, 25 securityType: wifi.WifiSecurityType, 26 rssi: number, 27 band: number, 28 frequency: number, 29 timestamp: number 30} 31 32export class WifiModel { 33 async getScanInfos(): Promise<Array<WifiType>> { 34 Logger.log(TAG, 'scanWifi begin') 35 let wifiList: Array<WifiType> = [] 36 let result: Array<wifi.WifiScanInfo> = [] 37 try { 38 result = await wifi.getScanResults() 39 } catch (err) { 40 Logger.log(TAG, `scan info err: ${JSON.stringify(err)}`) 41 return wifiList 42 } 43 Logger.log(TAG, `scan info call back: ${result.length}`) 44 for (var i = 0; i < result.length; ++i) { 45 wifiList.push({ 46 ssid: result[i].ssid, 47 bssid: result[i].bssid, 48 securityType: result[i].securityType, 49 rssi: result[i].rssi, 50 band: result[i].band, 51 frequency: result[i].frequency, 52 timestamp: result[i].timestamp 53 }) 54 } 55 return wifiList 56 } 57 58 connectNetwork(scanInfo: wifi.WifiScanInfo, psw) { 59 prompt.showToast({ message: 'connecting', duration: 5000 }) 60 Logger.debug(TAG, `connectNetwork bssid=${scanInfo.bssid}`) 61 // 这里因为api问题,需要声明为any,已提单 62 let deviceConfig: any = { 63 ssid: scanInfo.ssid, 64 bssid: scanInfo.bssid, 65 preSharedKey: psw, 66 isHiddenSsid: false, 67 securityType: scanInfo.securityType 68 } 69 try { 70 wifi.connectToDevice(deviceConfig) 71 Logger.debug(TAG, `connectToDevice success`) 72 } catch (err) { 73 Logger.debug(TAG, `connectToDevice fail err is ${JSON.stringify(err)}`) 74 } 75 try { 76 wifi.addDeviceConfig(deviceConfig) 77 } catch (err) { 78 Logger.debug(TAG, `addDeviceConfig fail err is ${JSON.stringify(err)}`) 79 } 80 } 81 82 resolveIP(ip) { 83 let address: string = ip.toString() 84 if (address === '0') { 85 return '00:00:000:000' 86 } 87 address.substring(0, 2) 88 return `${address.substring(0, 2)}:${address.substring(2, 4)}:${address.substring(4, 7)}:${address.substring(7, 10)}` 89 } 90 91 getIpInfo() { 92 let ipInfoList = [] 93 let ipInfo = wifi.getIpInfo() 94 Logger.info(`${TAG} getIpInfo=${JSON.stringify(ipInfo)}`) 95 ipInfoList.push({ key: $r('app.string.ip_address'), value: this.resolveIP(ipInfo.ipAddress) }) 96 ipInfoList.push({ key: $r('app.string.gate_way'), value: this.resolveIP(ipInfo.gateway) }) 97 ipInfoList.push({ key: $r('app.string.net_mask'), value: this.resolveIP(ipInfo.netmask) }) 98 ipInfoList.push({ key: $r('app.string.primary_dns'), value: this.resolveIP(ipInfo.primaryDns) }) 99 ipInfoList.push({ key: $r('app.string.second_dns'), value: this.resolveIP(ipInfo.secondDns) }) 100 ipInfoList.push({ key: $r('app.string.server_ip'), value: this.resolveIP(ipInfo.serverIp) }) 101 ipInfoList.push({ key: $r('app.string.lease_duration'), value: ipInfo.leaseDuration.toString() }) 102 return ipInfoList 103 } 104 105 getCountryCode() { 106 let countryCodeList = [] 107 let countryCode = wifi.getCountryCode() 108 countryCodeList.push({ key: $r('app.string.country_code'), value: countryCode }) 109 return countryCodeList 110 } 111 112 getFeatureSupport() { 113 let featureSupportedList = [] 114 featureSupportedList.push({ 115 key: $r('app.string.infrastructure_feature'), 116 value: wifi.isFeatureSupported(0x0001).toString() 117 }) 118 featureSupportedList.push({ key: $r('app.string.ghz_feature'), value: wifi.isFeatureSupported(0x0002).toString() }) 119 featureSupportedList.push({ 120 key: $r('app.string.gas_anqp_feature'), 121 value: wifi.isFeatureSupported(0x0004).toString() 122 }) 123 featureSupportedList.push({ key: $r('app.string.wifi_direct'), value: wifi.isFeatureSupported(0x0008).toString() }) 124 featureSupportedList.push({ key: $r('app.string.soft_ap'), value: wifi.isFeatureSupported(0x0010).toString() }) 125 featureSupportedList.push({ key: $r('app.string.wifi_aware'), value: wifi.isFeatureSupported(0x0040).toString() }) 126 return featureSupportedList 127 } 128}